voltdb create;##注意這個命令是初始化數據庫的意思,因此,若是你以前初始化過,再用的話,會將以前文件刪除的,回到初始狀態。java
voltadmin shutdown;node
voltadmin pause ##數據庫拒絕鏈接請求sql
voltdb recover數據庫
$ sqlcmd < myschema.sql;數據結構
或者
$ sqlcmd
1> file myschema.sql;app
或者async
$ sqlcmd
1>
2> CREATE TABLE Customer (
3> CustomerID INTEGER UNIQUE NOT NULL,
4> FirstName VARCHAR(15),
5> LastName VARCHAR (15),
6> PRIMARY KEY(CustomerID)
7> );ide
Example 4.1. DDL Example of a Reservation Schemaui
CREATE TABLE Flight (
FlightID INTEGER UNIQUE NOT NULL,
DepartTime TIMESTAMP NOT NULL,
Origin VARCHAR(3) NOT NULL,
Destination VARCHAR(3) NOT NULL,
NumberOfSeats INTEGER NOT NULL,
PRIMARY KEY(FlightID)
);this
CREATE TABLE Reservation (
ReserveID INTEGER NOT NULL,
FlightID INTEGER NOT NULL,
CustomerID INTEGER NOT NULL,
Seat VARCHAR(5) DEFAULT NULL,
Confirmed TINYINT DEFAULT '0'
);
CREATE TABLE Customer (
CustomerID INTEGER UNIQUE NOT NULL,
FirstName VARCHAR(15),
LastName VARCHAR (15),
PRIMARY KEY(CustomerID) ###建表的時候指定主鍵
);
$ sqlcmd ##若是是多個字段聯合主鍵就用下面的命令
1> CREATE TABLE Customer (
2> FirstName VARCHAR(15),
3> LastName VARCHAR (15),
4> CONSTRAINT pkey PRIMARY KEY (FirstName, LastName) ##創建主鍵
5> );
$ sqlcmd
1> PARTITION TABLE Reservation ON COLUMN FlightID;
2> PARTITION TABLE Customer ON COLUMN CustomerID;
You can also use the sqlcmd show directive to see a list of the current database tables and all procedures.
For additional details about the schema, execute the @SystemCatalog system procedure. Use any of the
following arguments to @SystemCatalog to obtain details about a component of the database schema:
• TABLES
• COLUMNS
• INDEXINFO
• PRIMARYKEYS
• PROCEDURES
• PROCEDURECOLUMNS
For example:
$ sqlcmd
1> SHOW TABLES;
2> SHOW PROCEDURES;
3> EXEC @SystemCatalog COLUMNS;
Modifying Tables
After creating a table in a database with CREATE TABLE, you can use ALTER TABLE to make the following types of table changes:
• Altering a Table Column's Data Definition
• Adding and Dropping Table Columns
• Adding and Dropping Table Constraints
To drop an entire table, use the DROP TABLE DDL statement.
You can make the following types of alterations to a table column's data definition:
1> ALTER TABLE Airport ALTER COLUMN Name VARCHAR(25);
2> ALTER TABLE Airport ALTER COLUMN Country SET DEFAULT 'USA';
去除默認值的方法
To remove a default, redefine the column data definition, for example:
ALTER TABLE Airport ALTER COLUMN Country VARCHAR(15);
3> ALTER TABLE Airport ALTER COLUMN Name SET NOT NULL;
The examples
$ sqlcmd
1> ALTER TABLE Airport ADD COLUMN AirportCode VARCHAR(3)
2> BEFORE AirportID;
3> ALTER TABLE Airport DROP COLUMN AirportID;
Note, we recommend that you not define the UNIQUE or ASSUMEUNIQUE constraint directly on a column definition when adding a column or creating a table. If you do, the constraint has no name so you cannot drop the constraint without dropping the entire column. Instead, we recommend you apply UNIQUE or ASSUMEUNIQUE by adding the constraint (see Section 4.6.3.3, 「Adding and Dropping Table Constraints」) or by adding an index with the constraint (see Section 4.6.4, 「Adding and Dropping Indexes」). Defining these constraints this way names the constraint, which makes it easier to drop later if necessary.
建議在建表的時候不要當即加上惟一索引unique,由於這樣惟一索引沒有名稱,而後你必須將列刪除了才能夠去掉索引。
BEFORE column-name — Table columns cannot be reordered but the BEFORE clause allows you to place a new column in a specific position with respect to the existing columns of the table.
before 關鍵字能夠指定添加列的位置。
Adding and Dropping Table Constraints
You cannot alter a table constraint but you can add and drop table constraints. If the table contains existing data, you cannot add UNIQUE, ASSUMEUNIQUE, or PRIMARY KEY constraints.
若是表有數據,將沒法添加約束。
$ sqlcmd
1> ALTER TABLE Airport ADD CONSTRAINT
2> uniquecode UNIQUE (Airportcode);
刪除約束的方法。
ALTER TABLE Airport DROP CONSTRAINT uniquecode;
3> ALTER TABLE Airport ADD PRIMARY KEY (AirportCode);
The examples
刪除主鍵的方法
ALTER TABLE Airport DROP PRIMARY KEY;
$ sqlcmd
1> CREATE INDEX flightTimeIdx ON Flight (departtime);
The CREATE INDEX statement explicitly creates an index. VoltDB creates an index implicitly when you specify the table constraints UNIQUE, PRIMARY KEY, or ASSUMEUNIQUE.
當使用關鍵字UNIQUE和PRIMARY KEY的時候,默認是建了索引的。
9.5 爲存儲過程添加或者刪除分區
$ sqlcmd
1> PARTITION TABLE Airport ON COLUMN Name;
2> CREATE PROCEDURE FindAirportCodeByName AS
3> SELECT TOP 1 AirportCode FROM Airport WHERE Name=?;
4> PARTITION PROCEDURE FindAirportCodeByName
5> ON TABLE Airport COLUMN Name;
刪除存儲過程上的分區的方法是刪除後,從新建。
1> DROP PROCEDURE FindAirportCodeByName;
2> CREATE PROCEDURE FindAirportCodeByName AS
3> SELECT TOP 1 AirportCode FROM Airport WHERE Name=?;
6> CREATE PROCEDURE FindAirportCodeByCity AS
7> SELECT TOP 1 AirportCode FROM Airport WHERE City=?;
The stored procedures are tested with the following sqlcmd directives:
$ sqlcmd
1> exec FindAirportCodeByName 'Logan Airport';
2> exec FindAirportCodeByCity 'Boston';
注意爲表添加分區必須表裏面沒有數據。
Before executing the following steps, save the existing schema so you can easily re-create the table. The VoltDB Management Center provides a view of the existing database schema DDL source, which you can download and save.
$ sqlcmd
1> DROP PROCEDURE FindAirportCodeByName;
2> DROP PROCEDURE FindAirportCodeByCity;
3> DROP TABLE Airport IF EXISTS CASCADE;
4> CREATE TABLE AIRPORT (
5> AIRPORTCODE varchar(3) NOT NULL,
6> NAME varchar(25),
7> CITY varchar(25),
8> COUNTRY varchar(15) DEFAULT 'USA',
9> CONSTRAINT UNIQUECODE UNIQUE (AIRPORTCODE),
10> PRIMARY KEY (AIRPORTCODE)
11> );
12> CREATE PROCEDURE FindAirportCodeByName AS
13> SELECT TOP 1 AirportCode FROM Airport WHERE Name=?;
14> CREATE PROCEDURE FindAirportCodeByCity AS
15> SELECT TOP 1 AirportCode FROM Airport WHERE City=?;
The example is described as follows:
Drop all stored procedures that reference the table. You cannot drop a table if stored procedures reference it. Drop the table. Options you may include are:
• IF EXISTS — Use the IF EXISTS option to avoid command errors if the named table is already removed.
• CASCADE — A table cannot be removed if it has index or view references. You can remove
the references explicitly first or use the CASCADE option to have VoltDB remove the references along with the table.
Re-create the table. By default, a newly created table is a replicated table.
Re-create the stored procedures that access the table. If the stored procedure is implemented with Java and changes are required, modify and reload the code before re-creating the stored procedures.
1. Drop the stored procedure from the database.
$ sqlcmd
1> DROP PROCEDURE GetAirport;
2. Remove the code from the database. If the procedure is implemented with Java, use the sqlcmd remove
classes directive to remove the procedure's class from the database.
2> remove classes myapp.procedures.GetAirport;
包含,rows ,每一個row裏面的列是按照 列名和值 的形式存儲,而且列的數目與查詢條件有關。
public final SQLStmt getressql = new SQLStmt(
"SELECT r.ReserveID, c.FirstName, c.LastName " +
"FROM Reservation AS r, Customer AS c " +
"WHERE r.FlightID=? AND r.CustomerID=c.CustomerID;");
如上查詢返回的數據結構爲:
步驟1 編譯而且加載class文件到數據庫
Compiling, Packaging, and Loading Stored Procedures
The VoltDB stored procedures are written as Java classes, so you compile them using the Java compiler.
Anytime you update your stored procedure code, remember to recompile, package, and reload it into the database using the following steps:
$ javac -classpath "./:/opt/voltdb/voltdb/*" \
-d ./obj \
*.java
$ jar cvf myproc.jar -C obj .
$ sqlcmd
1> load classes myproc.jar;
2> show classes;
步驟2、根據加載的class 聲明存儲過程。
The following DDL statements declare five stored procedures, identifying them by their class name:
$ sqlcmd
1> CREATE PROCEDURE FROM CLASS fadvisor.procedures.LookupFlight;
2> CREATE PROCEDURE FROM CLASS fadvisor.procedures.HowManySeats;
3> CREATE PROCEDURE FROM CLASS fadvisor.procedures.MakeReservation;
假如存儲過程是創建在FlightId上面。
PARTITION PROCEDURE MakeReservation ON TABLE Reservation COLUMN FlightID;
The PARTITION PROCEDURE statement assumes that the partitioning column value is also the first parameter to the stored procedure. Suppose you wish to partition a stored procedure on the third parameter such as the procedure GetCustomerDetails(), where the third parameter is a customer_id. You must specify the partitioning parameter using the PARAMETER clause and an index for the parameter position.
The index is zero-based so the third parameter would be "2" and the PARTITION PROCEDURE statement would be as follows:
對於存儲過程分區來講,默認認爲分表的列也是存儲過程中第一個參數。若是不是這樣子的話,須要在建存儲過程的聲明的時候,須要加上parameter 參數。
PARTITION PROCEDURE GetCustomerDetails
ON TABLE Customer COLUMN CustomerID
PARAMETER 2;
the shared partitioning column.
• The following WHERE constraint is also used: WHERE partitioned-table. identifier=?
In this example, WHERE RESERVATION.FLIGHTID=?
For example, the RESERVATION table can be joined with the FLIGHT table (which is replicated).
However,the RESERVATION table cannot be joined with the CUSTOMER table in a single-partitioned stored procedure because the two tables use different partitioning columns. (CUSTOMER is partitioned on the CUSTOMERID column.)
The following are examples of invalid SQL queries for a single-partitioned stored procedure partitioned
on FLIGHTID:
• INVALID:
SELECT * FROM reservation WHERE reservationid=?
The RESERVATION table is being constrained by a column (RESERVATIONID)
which is not the partitioning column.
查詢沒有放在partition列上面。
• INVALID:
SELECT c.lastname FROM reservation AS r, customer AS c WHERE
r.flightid=? AND c.customerid = r.customerid
The correct partitioning column is being used in the WHERE clause, but the tables are being joined on a different column. As a result, not all CUSTOMER rows are available to the stored procedure since the CUSTOMER table is partitioned on a different column than RESERVATION.
總結,兩表聯合查詢條件,要麼是有一個表示replicated,要麼是兩個表示創建在同一partition列
The first task for the calling program is to create a connection to the VoltDB database. You do this with the following steps:
org.voltdb.client.Client client = null;
ClientConfig config = null;
try {
config = new ClientConfig("username","password");//能夠不須要參數
client = ClientFactory.createClient(config);
client.createConnection("myserver.xyz.net");//server的地址能夠是域名或者IP
//也能夠加上端口號 client.createConnection("myserver.xyz.net",21211);
} catch (java.io.IOException e) {
e.printStackTrace();
System.exit(-1);
}
Define the configuration for your connections. In its simplest form, the ClientConfig class specifies the username and password to use. It is not absolutely necessary to create a client configuration object. For example, if security is not enabled (and therefore a username and password are not needed) a configuration object is not required. But it is a good practice to define the client configuration to ensure the same credentials are used for all connections against a single client. It is also possible to define additional characteristics of the client connections as part of the configuration, such as thetimeout period for procedure invocations or a status listener. (See Section 6.5, 「Handling Errors」.)
You can create the connection to any of the nodes in the database cluster and your stored procedure will be routed appropriately. In fact, you can create connections to multiple nodes on the server and your subsequent requests will be distributed to the various connections.
這個貌似,能夠路由存儲過程執行請求,減小瓶頸。
Multiple connections distribute the stored procedure requests around the cluster, avoiding a bottleneck
where all requests are queued through a single host. This is particularly important when using asynchronous
procedure calls or multiple clients.
try { client = ClientFactory.createClient(); client.createConnection("server1.xyz.net"); client.createConnection("server2.xyz.net"); client.createConnection("server3.xyz.net"); } catch (java.io.IOException e) { e.printStackTrace(); System.exit(-1); }