文档章节

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

Oscarfff
 Oscarfff
发布于 2016/06/10 16:27
字数 2285
阅读 319
收藏 0

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 为存储过程添加或者删除分区

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

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

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

 

© 著作权归作者所有

共有 人打赏支持
Oscarfff
粉丝 72
博文 815
码字总数 96913
作品 0
崇明
后端工程师
MySQL/HandlerSocket和VoltDB:NoSQL的竞争者

一般认为NoSQL数据库在性能方面要优于传统的SQL数据库。但是有两个SQL的解决方案宣布:对于大型系统的高可扩展性需求,SQL仍然是可行的解决方案!这两个SQL解决方案分别是MySQL加NoSQL层插件...

wkh
2014/01/07
0
0
NoSQL数据库的35个应用场景

本文另一地址请见 NoSQL数据库的35个应用场景 本文翻译自 35+ Use Cases For Choosing Your Next NoSQL Database 之前有三篇文章 What The Heck Are You Actually Using NoSQL For?. 101 Que......

可观
2013/02/03
0
9
linux学习总结

总结Linux基础命令 Linux下命令终端下几个常用的热键: [Tab][Tab]:按两次 [tab] 按键,激发命令与文件补全的功能。 Ctrl + c :中断目前程序的按键。 q(或quit):有很多程序在跑的时候(例如...

dhb_oschina
2016/02/18
69
0
what is partitioning the database tables

一、what is partitioning the database tables?什么是数据库表分区呢? 就是对数据库表里面的数据,按照一定的方式,进行逻辑上或者物理上分开。 This section discusses how to partitio...

Oscarfff
2016/06/10
30
0
JavaSE和JavaEE的起步学习路线

Java javaSE部分 1)能够掌握DOS系统常用基本命令; 2)熟练使用eclipse编写java代码; 3)掌握常用数据结构和算法; 4)掌握常用的编程设计模式; 5)熟练使用java语言的常用对象; 6)使用java编...

Jasonisoft
2016/05/18
0
0

没有更多内容

加载失败,请刷新页面

加载更多

arts-week10

Algorithm 905. Sort Array By Parity - LeetCode Review Who’s Afraid of the Big Bad Preloader? 一文读懂前端缓存 一个网络请求3个步骤:请求,处理,响应,而前端缓存主要在请求处响应这两步...

yysue
今天
0
0
00.编译OpenJDK-8u40的整个过程

前言 历经2天的折腾总算把OpenJDK给编译成功了,要说为啥搞这个,还得从面试说起,最近出去面试经常被问到JVM的相关东西,总感觉自己以前学的太浅薄,所以回来就打算深入学习,目标把《深入理...

凌晨一点
今天
5
0
python: 一些关于元组的碎碎念

初始化元组的时候,尤其是元组里面只有一个元素的时候,会出现一些很蛋疼的情况: def checkContentAndType(obj): print(obj) print(type(obj))if __name__=="__main__": tu...

Oh_really
昨天
6
2
jvm crash分析工具

介绍一款非常好用的jvm crash分析工具,当jvm挂掉时,会产生hs_err_pid.log。里面记录了jvm当时的运行状态以及错误信息,但是内容量比较庞大,不好分析。所以我们要借助工具来帮我们。 Cras...

xpbob
昨天
130
0
Qt编写自定义控件属性设计器

以前做.NET开发中,.NET直接就集成了属性设计器,VS不愧是宇宙第一IDE,你能够想到的都给你封装好了,用起来不要太爽!因为项目需要自从全面转Qt开发已经6年有余,在工业控制领域,有一些应用...

飞扬青云
昨天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部