部署独立模式:
切换到 sdbadmin 用户
su - sdbadmin
启动 SequoiaDB Shell 控制台
/opt/sequoiadb/bin/sdb
连接到本地的集群管理服务进程 sdbcm
var oma = new Oma("localhost", 11790)
创建独立模式的数据节点
oma.createData(11810, "/opt/sequoiadb/database/standalone/11810")
启动该节点
oma.startNode(11810)
部署集群模式:
切换到 sdbadmin 用户
su - sdbadmin
启动 SequoiaDB Shell 控制台
/opt/sequoiadb/bin/sdb
连接到本地的集群管理服务进程 sdbcm
var oma = new Oma("localhost", 11790)
创建临时协调节点
oma.createCoord(18800, "/opt/sequoiadb/database/coord/18800")
删除临时协调节点 oma.removeCoord(18800)
启动临时协调节点
oma.startNode(18800)
连接临时协调节点:
var db
=
new Sdb
(
"localhost"
,18800
)
创建编目节点组:
db.createCataRG
(
"sdbserver1"
,
11800
,
"/opt/sequoiadb/database/cata/11800"
)
创建编目节点:
var cataRG
=
db.getRG
(
"SYSCatalogGroup"
)
var node1
=
cataRG.createNode
(
"sdbserver2"
,
11800
,
"/opt/sequoiadb/database/cata/11800"
)
var node2
=
cataRG.createNode
(
"sdbserver3"
,
11800
,
"/opt/sequoiadb/database/cata/11800"
)
启动编目节点:
node1.start
(
)
node2.start
(
)
创建数据节点组:
var dataRG
=
db.createRG
(
"datagroup"
)
创建数据节点:
dataRG.createNode
(
"sdbserver1"
,
11820
,
"/opt/sequoiadb/database/data/11820"
)
dataRG.createNode
(
"sdbserver2"
,
11820
,
"/opt/sequoiadb/database/data/11820"
)
dataRG.createNode
(
"sdbserver3"
,
11820
,
"/opt/sequoiadb/database/data/11820"
)
启动数据节点组:
dataRG.start
(
)
创建协调节点组:
var coordRG
=
db.createCoordRG
(
)
创建协调节点:
coordRG.createNode
(
"sdbserver1"
,
11810
,
"/opt/sequoiadb/database/coord/11810"
)
coordRG.createNode
(
"sdbserver2"
,
11810
,
"/opt/sequoiadb/database/coord/11810"
)
coordRG.createNode
(
"sdbserver3"
,
11810
,
"/opt/sequoiadb/database/coord/11810"
)
启动协调节点组:
coordRG.start
(
)
SequoiaDB shell 主要包括 database(db),collectionspace(cs),collection(cl),cursor(cur),replicagroup(rg),node(nd),domain(dm) 这7大级别的操作
1、
启动 shell:
su - sdbadmin
/opt/sequoiadb/bin/sdb
2、连接数据库
var db = new Sdb('localhost',11810)
字段:
数值类型 | 定义 | 比较优先级权值 | 用例 |
整数
|
整数,范围 -2147483648 至 2147483647 |
|
10 |
|
{ "key" : 123 }
|
长整数
|
整数,范围 -9223372036854775808 至 9223372036854775807 如果用户指定的数值无法适用于整数,则 SequoiaDB 自动将 其转化为浮点型
|
10
|
{ "key" : 3000000000 }
|
浮点数
|
浮点数,范围 -1.7E+308 至 1.7E+308
|
10
|
{ "key" : 123.456 } 或 { "key" : 123e+50 }
|
字符串
|
双引号包含的字符串
|
15
|
{ "key" : "value" }
|
对象 ID(OID)
|
十二字节对象 ID
|
35
|
{ "key" : { "$oid" : "123abcd00ef12358902300ef" } }
|
布尔
|
true 或者 false
|
40
|
{ "key" : true } 或 { "key" : false }
|
日期
|
YYYY-MM-DD 的日期形式 范围 1900-01-01 至 9999-12-31
|
45
|
{ "key" : { "$date" : "2012-01-01" } }
|
时间戳
|
YYYY-MM-DD-HH.mm.ss.ffffff 的形式存取 范围 1902-01-01 00:00:00.000000 至 2037-12-31 23:59:59.999999
|
45
|
{ "key" : { "$timestamp" : "2012-01-01-13.14.26.124233" } }
|
二进制数据
|
Base64 形式的二进制数据
|
30
|
{ "key" : { "$binary" : "aGVsbG8gd29ybGQ=", "$type" : "1" } }
|
正则表达式
|
正则表达式
|
50
|
{ "key" : { "$regex" : "^张", "$options" : "i" } }
|
对象
|
嵌套 JSON 文档对象
|
20
|
{ "key" : { "subobj" : "value" } }
|
数组
|
嵌套数组对象
|
25
|
{ "key" : [ "abc", 0, "def" ] }
|
空
|
null
|
5
|
{ "key" : null }
|
1、
Java驱动
SequoiaDB | 数据库实例 | 代表一个单独的数据库连接 |
CollectionSpace | 集合空间实例 | 代表一个单独的集合空间 |
DBCollection | 集合实例 | 代表一个单独的集合 |
DBCursor | 游标实例 | 代表一个查询产生的结果集 |
DBLob | 大对象实例 | 代表一个大对象 |
2、
SQL to SequoiaDB shell to Java
SQL | SequoiaDB shell | Java Driver |
insert into students(a,b) values(1,-1)
|
db.foo.bar.insert({a:1,b:-1})
|
bar.insert("{'a':1,'b':-1}")
|
select a,b from students
|
db.foo.bar.find(null,{a:"",b:""})
|
bar.query("", "{'a':'','b':''}", "", "")
|
select * from students
|
db.foo.bar.find()
|
bar.query()
|
select * from students where age=20
|
db.foo.bar.find({age:20})
|
bar.query("{'age':20}", "", "", "")
|
select * from students where age=20 order by name
|
db.foo.bar.find({age:20}).sort({name:1})
|
bar.query("{'age':20}", "", "{'name':1}", "")
|
select * from students where age > 20 and age < 30
|
db.foo.bar.find({age:{$gt:20,$lt:30}})
|
bar.query("{'age':{'$gt':20,'$lt':30}}", "", "", "")
|
create index testIndex on students(name)
|
db.foo.bar.createIndex("testIndex",{name:1},false)
|
bar.createIndex("testIndex", "{'name':1}", false, false)
|
select * from students limit 20 skip 10
|
db.foo.bar.find().limit(20).skip(10)
|
bar.query("", "", "", "", 10, 20)
|
select count(*) from students where age > 20
|
db.foo.bar.find({age:{$gt:20}}).count()
|
bar.getCount("{'age':{'$gt':20}}")
|
update students set a=a+2 where b=-1
|
db.foo.bar.update({$set:{a:2}},{b:-1})
|
bar.update("{'b':-1}", "{'$inc':{'a':2}}", "")
|
delete from students where a=1
|
db.foo.bar.remove({a:1})
|
bar.delete("{'a':1}")
|
SQL语法
1、创建集合空间
db.execUpdate("create collectionspace foo") //等价于 db.createCS("foo")
2、删除集合空间
db.execUpdate("drop collectionspace foo") //等价于 db.dropCS("foo")
3、创建集合
db.execUpdate("create collection foo.bar") //等价于 db.foo.createCL("bar")
4、删除集合
db.execUpdate("drop collection foo.bar") //等价于 db.foo.dropCL("bar")
5、创建索引
db.execUpdate("create unique index test_index on foo.bar (age desc,name asc)")
6、删除索引
db.execUpdate("drop index test_index on foo.bar") //等价于 db.foo.bar.dropIndex("test_index")
7、返回所有集合空间
db.exec("list collectionspaces")
8、返回所有集合
db.exec("list collections")
9、insert into
db.execUpdate("insert into foo.bar(age,name) values(25,"Tom")")
db.foo.bar.insert({
_id:1,
name:{fist:"Jhon",last:"Black"},
phone:[1853742000,1802321000],
remark:[{
position:"manager",
year:2000
},{
position:"CEO",
year:2012
}]
})
10、select
db.exec("select * from foo.bar")
db.foo.bar.find(
{
"_id": {
"$oid": "53a82aa2c4b970091e000000"
},
"name": "sequoiadb"
});
11、update
db.execUpdate("update foo.bar set age=20 where age < 10")
db.foo.bar.update({$set:{"name.first":"Mike"}},{_id:1})
12、delete
db.execUpdate("delete from foo.bar where age <10")
db.foo.bar.remove({name:"Tom"})
13、transaction
db
.
execUpdate
(
"begin transaction"
)
db
.
execUpdate
(
"commit"
)
14、group by
db.exec("select dept_no,count(emp_no) as 员工总数 from foo.bar group by dept_no")
15、order by
db.exec("select dept_no,count(emp_no) as 员工总数 from foo.bar group by dept_no order by dept_no desc")
16、split by (
按照某个数组字段将记录拆分)
SELECT * FROM foo.bar SPLIT BY c
17、前10条limit
db.exec("select * from foo.bar limit 10")
18、跳过前5条offset
db.exec("select * from foo.bar offset 5")
19、as
集合别名
db.exec("select T1.age,T1.name from foo.bar as T1 where T1.age>10")
字段别名
db.exec("select age as 年龄 from foo.bar where age>10")
结果集别名
db.exec("select T.age,T.name from (select age,name from foo.bar) as T where T.age>10")
20、inner join
db.exec("select E.emp_no,D.dept_name from foo.emp as E inner join foo.dept as D on E.dept_no=D.dept_no")
21、left outer join
有员工信息表 foo.emp 和部门信息表 foo.dept,查询员工号 emp_no 所在的部门名 dept_name:
db.exec("select E.emp_no,D.dept_name from foo.emp as E left outer join foo.dept as D on E.dept_no=D.dept_no where D.dept_no < 4")
22、right outer join
有员工信息表 foo.emp 和部门信息表 foo.dept,查询员工号 emp_no <10 所在的部门名 dept_name:
db.exec("select E.emp_no,D.dept_name from foo.emp as E right outer join foo.dept as D on E.dept_no=D.dept_no where E.emp_no < 10")
23、求和sum
db.exec("select sum(age) as 年龄总和 from foo.bar")
24、计数count
db.exec("select count(age) as 数量 from foo.bar")
25、平均avg
db.exec("select avg(age) as 平均年龄 from foo.bar")
26、最大max
db.exec("select max(age) as 最大年龄 from foo.bar")
27、最小min
db.exec("select min(age) as 最小年龄 from foo.bar")
28、first
选择范围内第一条数据。
db
.
exec
(
"select first(a) as a, b from sample.employee group by b"
)
29、last
选择范围内最后一条数据
db
.
exec
(
"select last(a) as a, b from sample.employee group by b"
)
30、push
将多个值合并为一个数组。
db
.
exec
(
"select a, push(b) as b from sample.employee group by a"
)
30、addtoset
将集合中多条记录中的相同字段的值合并到一个没有重复值的数组中。
db
.
exec
(
"select a, addtoset(b) as b from sample.employee group by a"
)
31、buildtoobj 多个字段合并
SELECT a, buildobj(b, c) AS d FROM foo.bar
32、mergearrayset 多个数组字段合并为不重复字段的数组
SELECT a, MERGEARRAYSET(b) AS b FROM foo.bar GROUP BY a
SQL to SequoiaDB 映射表
Create 和 Alter
下表列出了各种 SQL 语句表级别的操作和在 SequoiaDB 中对应的操作:
SQL 语句 | SequoiaDB 语句 |
create table student (id not null, stu_id varchar(50), age number primary key(id)) |
db.collectionspace.student({stu_id:"01",age:20})
db.collectionspace.createCL("student")
|
alter table student add name varchar(50) | db.collectionspace.student.update({},{$set:{name:"Tom"}}) |
alter table student drop column name | db.collectionspace.student.update({},{$unset:{name:"Tom"}}) |
create index index_stu_id on student (stu_id) | db.collectionspace.student.createIndex("index_stu_id",{stu_id:-1}) |
drop table student | db.collectionspace.dropCL("student") |
Insert
下表给出了各种 SQL 语句在表级上的插入操作和 SequoiaDB 上相应的操作:
SQL 语句 | SequoiaDB 语句 |
insert into student(stu_id,age) values("01",20)
|
db.collectionspace.student.insert({stu_id:"01",age:20})
|
Select
下表给出了各种 SQL 语句在表级上的读操作和 SequoiaDB 上相应的操作:
SQL 语句 | SequoiaDB 语句 |
select * from student
|
db.collectionspace.student.find()
|
select stu_id, age from student
|
db.collectionspace.student.find({},{stu_id:"01",age:20})
|
select * from student where age > 25
|
db.collectionspace.student.find({age:{$gt:25}})
|
select age from student where age = 25 and stu_id= "01"
|
db.collectionspace.student.find({age:25,stu_id="01"},{age:25})
|
select count(*) from student
|
db.collectionspace.student.count()
|
select count(stu_id) from student
|
db.collectionspace.student.count({stu_id:{$exists:1}})
|
Update
下表给出了各种 SQL 语句在表级上的更新操作和 SequoiaDB 上相应的操作:
SQL 语句 | SequoiaDB 语句 |
update student set age = 25 where stu_id = "01" | db.collectionspace.student.update({stu_id:"01"},{$set:{age:25}}) |
update student set age = age + 2 where stu_id = "01" | db.collectionspace.student.update({stu_id:"01"},{$inc:{age:2}}) |
Delete
下表给出了各种 SQL 语句在表级上的删除记录操作和 SequoiaDB 上相应的操作:
SQL 语句 | SequoiaDB 语句 |
delete from student where age = 20 | db.collectionspace.student.remove({age:20}) |
delete from student | db.collectionspace.student.remove() |
关系型数据库和非关系型数据库区别:
1、数据存储方式不同。
关系型数据存储在数据表的行和列中,数据表可以彼此关联协作存储。
非关系型数据通常存储在数据集中,就像文档、键值对或者图结构。
2、扩展方式不同。
SQL数据库是纵向扩展,操作的性能瓶颈可能涉及很多个表,这都需要通过提高计算机性能来克服。虽然SQL数据库有很大扩展空间,但最终肯定会达到纵向扩展的上限。
而非关系型数据存储是分布式的,NoSQL数据库的扩展可以通过给资源池添加更多普通的数据库服务器(节点)来分担负载。
3、对事务性的支持不同。
SQL数据库支持对事务原子性细粒度控制,并且易于回滚事务。
NoSQL数据库一般不支持ACID特性