1. 程式人生 > >voltdb常用命令以及常用概念和常用方法

voltdb常用命令以及常用概念和常用方法

轉自:

1、建立資料庫

   voltdb create;##注意這個命令是初始化資料庫的意思,所以,如果你之前初始化過,再用的話,會將之前檔案刪除的,回到初始狀態。

2、關閉資料庫

   voltadmin shutdown;

3、暫停資料庫

   voltadmin pause ##資料庫拒絕連線請求

4、恢復或者重啟資料庫

   voltdb recover

5、匯入資料庫配置

$ sqlcmd < myschema.sql;

或者 $ sqlcmd 1> file myschema.sql;

或者

$ 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> );

6、建表和主鍵

Example 4.1. DDL Example of a Reservation Schema

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) );

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> ); 

7、分割槽的方法

$ sqlcmd 1> PARTITION TABLE Reservation ON COLUMN FlightID; 2> PARTITION TABLE Customer ON COLUMN CustomerID;

8、檢視資料庫schema

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;

9、修改表結構

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.

9.1 修改列屬性

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

9.2 刪除或者新增列

$ 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 關鍵字可以指定新增列的位置。

9.3新增或者刪除約束性

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;

9.4建立唯一索引

$ 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 為儲存過程新增或者刪除分割槽

$ sqlcmd1> 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';

9.6 為表新增或者刪除分割槽

注意為表新增分割槽必須表裡面沒有資料。

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.  

9.6刪除儲存過程

1. Drop the stored procedure from the database. $ sqlcmd1> 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;

10、VoltTable資料結構型別

包含,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;");

如上查詢返回的資料結構為:

11、Install 儲存過程到資料庫

步驟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;

步驟二、根據載入的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;

11.1關於儲存過程的分割槽原理

假如儲存過程是建立在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;

12、分割槽儲存過程查詢注意事項 Queries in Single-Partitioned Stored Procedures

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列

12、連線voltdb資料庫

12.1連線一個server

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”.)

12.2 多個server

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); }