七:Java编程访问数据库(续)
昨天建立了连接并得到了集合空间和集合,今天实现了集合查询。完整代码如下:
点击(此处)折叠或打开
-
package com.greencloud;
-
-
import org.bson.BSONObject;
-
import org.bson.BasicBSONObject;
-
-
import com.sequoiadb.base.CollectionSpace;
-
import com.sequoiadb.base.DBCollection;
-
import com.sequoiadb.base.DBCursor;
-
import com.sequoiadb.base.Sequoiadb;
-
import com.sequoiadb.exception.BaseException;
-
-
public class SequoiaDbDemo {
-
-
static String CS_NAME = "foo";
-
static String CL_NAME = "bar";
-
static String INDEX_NAME = "bar";
-
-
/**
-
* @param args
-
*/
-
public static void main(String[] args) {
-
// TODO Auto-generated method stub
-
String connectStr = "192.168.8.128:50000";
-
Sequoiadb sdb = null;
-
try {
-
sdb = new Sequoiadb(connectStr, "sdbadmin", "sdbadmin");
-
} catch (BaseException be) {
-
System.out.println(be.toString());
-
be.printStackTrace();
-
System.exit(1);
-
}
-
-
CollectionSpace cs = null;
-
if (sdb.isCollectionSpaceExist(CS_NAME)) {
-
cs = sdb.getCollectionSpace(CS_NAME);
-
System.out.println("Select Collection Space");
-
} else {
-
cs = sdb.createCollectionSpace(CS_NAME);
-
System.out.println("Create Collection Space");
-
}
-
-
DBCollection cl = null;
-
if (cs.isCollectionExist(CL_NAME)) {
-
cl = cs.getCollection(CL_NAME);
-
System.out.println("Select Colleciton");
-
} else {
-
cl = cs.createCollection(CL_NAME);
-
System.out.println("Create Collection");
-
}
-
-
try {
-
BSONObject index = null;
-
DBCursor indexCursor = cl.getIndex(INDEX_NAME);
-
if (indexCursor.hasNext()) {
-
index = indexCursor.getNext();
-
}
-
// result cursor
-
DBCursor dataCursor = null;
-
// 构建相应BSONObject,用于查询,包括:条件(query),域选择(selector),排序规则(orderBy),
-
// 索引使用(index),跳过记录个数(0),返回记录个数(-1:返回所有数据)。
-
// query condition
-
BSONObject query = new BasicBSONObject();
-
BSONObject condition = new BasicBSONObject();
-
condition.put("$et", "wangcc");
-
// condition.put("$lte", 9);
-
query.put("name", condition);
-
// return fields
-
BSONObject selector = new BasicBSONObject();
-
selector.put("_id", null);
-
selector.put("name", null);
-
selector.put("age", 0);
-
// order by ASC(1)/DESC(-1)
-
BSONObject orderBy = new BasicBSONObject();
-
orderBy.put("_id", -1);
-
if (index == null) {
-
dataCursor = cl.query(query, selector, orderBy, null, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", null, 0, -1);
-
else {
-
dataCursor = cl.query(query, selector, orderBy, index, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", index, 0, -1);
-
// 使用Cursor游标,进行查询结果遍历
-
// operate data by cursor
-
while (dataCursor.hasNext()) {
-
System.out.println(dataCursor.getNext());
-
}
-
// get count by match condition
-
long count = cl.getCount(query);
-
System.out.println("Get count by condition: " + query
-
+ ", count = " + count);
-
} catch (BaseException e) {
-
e.printStackTrace();
-
}
-
}
- }
点击(此处)折叠或打开
-
Select Collection Space
-
Select Colleciton
-
{ "_id" : { "$oid" : "52582e1547c0437e0b000001"} , "age" : 36 , "name" : "wangcc"}
-
{ "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 0 , "name" : "wangcc"}
- Get count by condition: { "name" : { "$et" : "wangcc"}}, count = 2
我的Sequoiadb数据库的情况是(通过sdb查询):
点击(此处)折叠或打开
-
> var sdb = new Sdb("localhost",50000)
-
Takes 1.4294179166s.
-
> sdb.exec("select * from foo.bar")
-
{
-
"_id": {
-
"$oid": "52582e0247c0437e0b000000"
-
},
-
"name": "wangcc"
-
}
-
{
-
"_id": {
-
"$oid": "52582e1547c0437e0b000001"
-
},
-
"name": "wangcc",
-
"age": 36,
-
"phone": "123456"
-
}
-
Return 2 row(s).
- Takes 1.4294724560s.
昨天完成了数据库的查询,今天(11月23日)完成数据库的添加。代码是在昨天的基础上修改的,修改一:将查询部分单独提出一个方法叫Query,修改二:查询调价全部查询;代码如下:
点击(此处)折叠或打开
-
package com.greencloud;
-
-
import org.bson.BSONObject;
-
import org.bson.BasicBSONObject;
-
-
import com.sequoiadb.base.CollectionSpace;
-
import com.sequoiadb.base.DBCollection;
-
import com.sequoiadb.base.DBCursor;
-
import com.sequoiadb.base.Sequoiadb;
-
import com.sequoiadb.exception.BaseException;
-
-
public class SequoiaDbDemo {
-
-
static String CS_NAME = "foo";
-
static String CL_NAME = "bar";
-
static String INDEX_NAME = "bar";
-
-
/**
-
* @param args
-
*/
-
public static void main(String[] args) {
-
// TODO Auto-generated method stub
-
String connectStr = "192.168.8.128:50000";
-
Sequoiadb sdb = null;
-
try {
-
sdb = new Sequoiadb(connectStr, "sdbadmin", "sdbadmin");
-
} catch (BaseException be) {
-
System.out.println(be.toString());
-
be.printStackTrace();
-
System.exit(1);
-
}
-
-
CollectionSpace cs = null;
-
if (sdb.isCollectionSpaceExist(CS_NAME)) {
-
cs = sdb.getCollectionSpace(CS_NAME);
-
System.out.println("Select Collection Space");
-
} else {
-
cs = sdb.createCollectionSpace(CS_NAME);
-
System.out.println("Create Collection Space");
-
}
-
-
DBCollection cl = null;
-
if (cs.isCollectionExist(CL_NAME)) {
-
cl = cs.getCollection(CL_NAME);
-
System.out.println("Select Colleciton");
-
} else {
-
cl = cs.createCollection(CL_NAME);
-
System.out.println("Create Collection");
-
}
-
System.out.println("Before Insert");
-
Query(cl);
-
System.out.println("\r\nStart Insert");
-
Insert(cl);
-
System.out.println("\r\nAfter Insert");
-
Query(cl);
-
}
-
-
static void Insert(DBCollection cl) {
-
BSONObject insertor1 = new BasicBSONObject();
-
insertor1.put("name", "wanghg");
-
insertor1.put("age", "32");
-
insertor1.put("sex", "nan");
-
BSONObject insertor2 = new BasicBSONObject();
-
insertor2.put("name", "hao-yu");
-
insertor2.put("age", "26");
-
insertor2.put("jiguan", "benxi");
-
try {
-
cl.insert(insertor1);
-
cl.insert(insertor2);
-
} catch (BaseException be) {
-
System.out.println("Failed to insert chinese record, ErrorType = "
-
+ be.getErrorType());
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
static void Query(DBCollection cl) {
-
try {
-
BSONObject index = null;
-
DBCursor indexCursor = cl.getIndex(INDEX_NAME);
-
if (indexCursor.hasNext()) {
-
index = indexCursor.getNext();
-
}
-
// result cursor
-
DBCursor dataCursor = null;
-
// 构建相应BSONObject,用于查询,包括:条件(query),域选择(selector),排序规则(orderBy),
-
// 索引使用(index),跳过记录个数(0),返回记录个数(-1:返回所有数据)。
-
// query condition
-
BSONObject query = new BasicBSONObject();
-
// 删除查询条件,查询所有记录
-
//BSONObject condition = new BasicBSONObject();
-
//condition.put("$et", "wangcc");
-
// condition.put("$lte", 9);
-
//query.put("name", condition);
-
// return fields
-
BSONObject selector = new BasicBSONObject();
-
selector.put("_id", null);
-
selector.put("name", null);
-
selector.put("age", 0);
-
// order by ASC(1)/DESC(-1)
-
BSONObject orderBy = new BasicBSONObject();
-
orderBy.put("_id", -1);
-
if (index == null) {
-
dataCursor = cl.query(query, selector, orderBy, null, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", null, 0, -1);
-
else {
-
dataCursor = cl.query(query, selector, orderBy, index, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", index, 0, -1);
-
// 使用Cursor游标,进行查询结果遍历
-
// operate data by cursor
-
while (dataCursor.hasNext()) {
-
System.out.println(dataCursor.getNext());
-
}
-
// get count by match condition
-
long count = cl.getCount(query);
-
System.out.println("Get count by condition: " + query
-
+ ", count = " + count);
-
} catch (BaseException e) {
-
e.printStackTrace();
-
}
-
}
- }
点击(此处)折叠或打开
-
Select Collection Space
-
Select Colleciton
-
Before Insert
-
{ "_id" : { "$oid" : "52582e1547c0437e0b000001"} , "age" : 36 , "name" : "wangcc"}
-
{ "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 0 , "name" : "wangcc"}
-
Get count by condition: { }, count = 2
-
-
Start Insert
-
-
After Insert
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb117"} , "age" : "26" , "name" : "hao-yu"}
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb116"} , "age" : "32" , "name" : "wanghg"}
-
{ "_id" : { "$oid" : "52582e1547c0437e0b000001"} , "age" : 36 , "name" : "wangcc"}
-
{ "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 0 , "name" : "wangcc"}
- Get count by condition: { }, count = 4
点击(此处)折叠或打开
-
> sdb.exec("select * from foo.bar")
-
{
-
"_id": {
-
"$oid": "52582e0247c0437e0b000000"
-
},
-
"name": "wangcc"
-
}
-
{
-
"_id": {
-
"$oid": "52582e1547c0437e0b000001"
-
},
-
"name": "wangcc",
-
"age": 36,
-
"phone": "123456"
-
}
-
{
-
"_id": {
-
"$oid": "5267d1f7b5b7332e021fb116"
-
},
-
"name": "wanghg",
-
"age": "32",
-
"sex": "nan"
-
}
-
{
-
"_id": {
-
"$oid": "5267d1f7b5b7332e021fb117"
-
},
-
"name": "hao-yu",
-
"age": "26",
-
"jiguan": "benxi"
-
}
-
Return 4 row(s).
-
Takes 0.2951s.
- >
之前实现了数据库的查询与插入,今天(10月24日)实现数据库的更新操作,源码如下:
点击(此处)折叠或打开
-
package com.greencloud;
-
-
import org.bson.BSONObject;
-
import org.bson.BasicBSONObject;
-
-
import com.sequoiadb.base.CollectionSpace;
-
import com.sequoiadb.base.DBCollection;
-
import com.sequoiadb.base.DBCursor;
-
import com.sequoiadb.base.Sequoiadb;
-
import com.sequoiadb.exception.BaseException;
-
-
public class SequoiaDbDemo {
-
-
static String CS_NAME = "foo";
-
static String CL_NAME = "bar";
-
static String INDEX_NAME = "bar";
-
-
/**
-
* @param args
-
*/
-
public static void main(String[] args) {
-
// TODO Auto-generated method stub
-
String connectStr = "192.168.8.128:50000";
-
Sequoiadb sdb = null;
-
try {
-
sdb = new Sequoiadb(connectStr, "sdbadmin", "sdbadmin");
-
} catch (BaseException be) {
-
System.out.println(be.toString());
-
be.printStackTrace();
-
System.exit(1);
-
}
-
-
CollectionSpace cs = null;
-
if (sdb.isCollectionSpaceExist(CS_NAME)) {
-
cs = sdb.getCollectionSpace(CS_NAME);
-
System.out.println("Select Collection Space");
-
} else {
-
cs = sdb.createCollectionSpace(CS_NAME);
-
System.out.println("Create Collection Space");
-
}
-
-
DBCollection cl = null;
-
if (cs.isCollectionExist(CL_NAME)) {
-
cl = cs.getCollection(CL_NAME);
-
System.out.println("Select Colleciton");
-
} else {
-
cl = cs.createCollection(CL_NAME);
-
System.out.println("Create Collection");
-
}
-
System.out.println("Before Insert");
-
Query(cl);
-
// System.out.println("\r\nStart Insert");
-
// Insert(cl);
-
System.out.println("\r\nStart Update");
-
Update(cl);
-
System.out.println("\r\nAfter Insert");
-
Query(cl);
-
}
-
-
static void Insert(DBCollection cl) {
-
BSONObject insertor1 = new BasicBSONObject();
-
insertor1.put("name", "wanghg");
-
insertor1.put("age", "32");
-
insertor1.put("sex", "nan");
-
BSONObject insertor2 = new BasicBSONObject();
-
insertor2.put("name", "hao-yu");
-
insertor2.put("age", "26");
-
insertor2.put("jiguan", "benxi");
-
try {
-
cl.insert(insertor1);
-
cl.insert(insertor2);
-
} catch (BaseException be) {
-
System.out.println("Failed to insert chinese record, ErrorType = "
-
+ be.getErrorType());
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
static void Update(DBCollection cl) {
-
BSONObject matcher = new BasicBSONObject();
-
BSONObject modifier = new BasicBSONObject();
-
BSONObject m = new BasicBSONObject();
-
matcher.put("name", "wangcc"); // 条件
-
m.put("age", 37); // 更新值
-
modifier.put("$set", m); // $set,设置指定字段值
-
-
// 更新操作,如果没有满足matcher的条件,则插入记录
-
cl.upsert(matcher, modifier, null);
-
DBCursor cursor = cl.query(matcher, null, null, null);
-
if (cursor.hasNext()) {
-
System.out.println(cursor.getNext());
-
}
-
}
-
-
static void Query(DBCollection cl) {
-
try {
-
BSONObject index = null;
-
DBCursor indexCursor = cl.getIndex(INDEX_NAME);
-
if (indexCursor.hasNext()) {
-
index = indexCursor.getNext();
-
}
-
// result cursor
-
DBCursor dataCursor = null;
-
// 构建相应BSONObject,用于查询,包括:条件(query),域选择(selector),排序规则(orderBy),
-
// 索引使用(index),跳过记录个数(0),返回记录个数(-1:返回所有数据)。
-
// query condition
-
BSONObject query = new BasicBSONObject();
-
// 删除查询条件,查询所有记录
-
// BSONObject condition = new BasicBSONObject();
-
// condition.put("$et", "wangcc");
-
// condition.put("$lte", 9);
-
// query.put("name", condition);
-
// return fields
-
BSONObject selector = new BasicBSONObject();
-
selector.put("_id", null);
-
selector.put("name", null);
-
selector.put("age", 0);
-
// order by ASC(1)/DESC(-1)
-
BSONObject orderBy = new BasicBSONObject();
-
orderBy.put("_id", -1);
-
if (index == null) {
-
dataCursor = cl.query(query, selector, orderBy, null, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", null, 0, -1);
-
else {
-
dataCursor = cl.query(query, selector, orderBy, index, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", index, 0, -1);
-
// 使用Cursor游标,进行查询结果遍历
-
// operate data by cursor
-
while (dataCursor.hasNext()) {
-
System.out.println(dataCursor.getNext());
-
}
-
// get count by match condition
-
long count = cl.getCount(query);
-
System.out.println("Get count by condition: " + query
-
+ ", count = " + count);
-
} catch (BaseException e) {
-
e.printStackTrace();
-
}
-
}
- }
点击(此处)折叠或打开
-
Select Collection Space
-
Select Colleciton
-
Before Insert
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb117"} , "age" : "26" , "name" : "hao-yu"}
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb116"} , "age" : "32" , "name" : "wanghg"}
-
{ "_id" : { "$oid" : "52582e1547c0437e0b000001"} , "age" : 36 , "name" : "wangcc"}
-
{ "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 0 , "name" : "wangcc"}
-
Get count by condition: { }, count = 4
-
-
Start Update
-
{ "Age" : 37 , "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 37 , "name" : "wangcc"}
-
-
After Insert
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb117"} , "age" : "26" , "name" : "hao-yu"}
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb116"} , "age" : "32" , "name" : "wanghg"}
-
{ "_id" : { "$oid" : "52582e1547c0437e0b000001"} , "age" : 37 , "name" : "wangcc"}
-
{ "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 37 , "name" : "wangcc"}
- Get count by condition: { }, count = 4
十、Java编程访问数据库(续)
前两天完成了通过Java编写查询,插入和更新,今天(10月25日)完成最后一个操作,删除。源程序如下:
点击(此处)折叠或打开
-
package com.greencloud;
-
-
import org.bson.BSONObject;
-
import org.bson.BasicBSONObject;
-
-
import com.sequoiadb.base.CollectionSpace;
-
import com.sequoiadb.base.DBCollection;
-
import com.sequoiadb.base.DBCursor;
-
import com.sequoiadb.base.Sequoiadb;
-
import com.sequoiadb.exception.BaseException;
-
-
public class SequoiaDbDemo {
-
-
static String CS_NAME = "foo";
-
static String CL_NAME = "bar";
-
static String INDEX_NAME = "bar";
-
-
/**
-
* @param args
-
*/
-
public static void main(String[] args) {
-
// TODO Auto-generated method stub
-
String connectStr = "192.168.8.128:50000";
-
Sequoiadb sdb = null;
-
try {
-
sdb = new Sequoiadb(connectStr, "sdbadmin", "sdbadmin");
-
} catch (BaseException be) {
-
System.out.println(be.toString());
-
be.printStackTrace();
-
System.exit(1);
-
}
-
-
CollectionSpace cs = null;
-
if (sdb.isCollectionSpaceExist(CS_NAME)) {
-
cs = sdb.getCollectionSpace(CS_NAME);
-
System.out.println("Select Collection Space");
-
} else {
-
cs = sdb.createCollectionSpace(CS_NAME);
-
System.out.println("Create Collection Space");
-
}
-
-
DBCollection cl = null;
-
if (cs.isCollectionExist(CL_NAME)) {
-
cl = cs.getCollection(CL_NAME);
-
System.out.println("Select Colleciton");
-
} else {
-
cl = cs.createCollection(CL_NAME);
-
System.out.println("Create Collection");
-
}
-
System.out.println("Before Insert");
-
Query(cl);
-
// System.out.println("\r\nStart Insert");
-
// Insert(cl);
-
// System.out.println("\r\nAfter Insert");
-
// System.out.println("\r\nStart Update");
-
// Update(cl);
-
// System.out.println("\r\nAfter Update");
-
System.out.println("\r\nStart Delete");
-
Delete(cl);
-
System.out.println("\r\nAfter Delete");
-
Query(cl);
-
// CLose Connection
-
sdb.disconnect();
-
}
-
-
static void Insert(DBCollection cl) {
-
BSONObject insertor1 = new BasicBSONObject();
-
insertor1.put("name", "wanghg");
-
insertor1.put("age", "32");
-
insertor1.put("sex", "nan");
-
BSONObject insertor2 = new BasicBSONObject();
-
insertor2.put("name", "hao-yu");
-
insertor2.put("age", "26");
-
insertor2.put("jiguan", "benxi");
-
try {
-
cl.insert(insertor1);
-
cl.insert(insertor2);
-
} catch (BaseException be) {
-
System.out.println("Failed to insert chinese record, ErrorType = "
-
+ be.getErrorType());
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
static void Delete(DBCollection cl) {
-
// 构建相应BSONObject,用于设置删除的条件或者使用相对应BSON的String表示
-
// create the delete condition
-
BSONObject condition = new BasicBSONObject();
-
condition.put("name", "wanghg");
-
try {
-
cl.delete(condition);
-
// if you want to delete all the data in current collection, you can
-
// use like: cl.delete(null)
-
// or
-
// cl.delete("{'name':'wanghg'}");
-
} catch (BaseException e) {
-
System.out
-
.println("Failed to delete data, condition: " + condition);
-
e.printStackTrace();
-
}
-
System.out.println("Delete data successfully");
-
}
-
-
static void Update(DBCollection cl) {
-
BSONObject matcher = new BasicBSONObject();
-
BSONObject modifier = new BasicBSONObject();
-
BSONObject m = new BasicBSONObject();
-
matcher.put("name", "wangcc"); // 条件
-
m.put("age", 37); // 更新值
-
modifier.put("$set", m); // $set,设置指定字段值
-
-
// 更新操作,如果没有满足matcher的条件,则插入记录
-
cl.upsert(matcher, modifier, null);
-
DBCursor cursor = cl.query(matcher, null, null, null);
-
if (cursor.hasNext()) {
-
System.out.println(cursor.getNext());
-
}
-
}
-
-
static void Query(DBCollection cl) {
-
try {
-
BSONObject index = null;
-
DBCursor indexCursor = cl.getIndex(INDEX_NAME);
-
if (indexCursor.hasNext()) {
-
index = indexCursor.getNext();
-
}
-
// result cursor
-
DBCursor dataCursor = null;
-
// 构建相应BSONObject,用于查询,包括:条件(query),域选择(selector),排序规则(orderBy),
-
// 索引使用(index),跳过记录个数(0),返回记录个数(-1:返回所有数据)。
-
// query condition
-
BSONObject query = new BasicBSONObject();
-
// 删除查询条件,查询所有记录
-
// BSONObject condition = new BasicBSONObject();
-
// condition.put("$et", "wangcc");
-
// condition.put("$lte", 9);
-
// query.put("name", condition);
-
// return fields
-
BSONObject selector = new BasicBSONObject();
-
selector.put("_id", null);
-
selector.put("name", null);
-
selector.put("age", 0);
-
// order by ASC(1)/DESC(-1)
-
BSONObject orderBy = new BasicBSONObject();
-
orderBy.put("_id", -1);
-
if (index == null) {
-
dataCursor = cl.query(query, selector, orderBy, null, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", null, 0, -1);
-
else {
-
dataCursor = cl.query(query, selector, orderBy, index, 0, -1);
-
}
-
// or
-
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}",
-
// "{'Id':null,'Age':null}", "{'Id':-1}", index, 0, -1);
-
// 使用Cursor游标,进行查询结果遍历
-
// operate data by cursor
-
while (dataCursor.hasNext()) {
-
System.out.println(dataCursor.getNext());
-
}
-
// get count by match condition
-
long count = cl.getCount(query);
-
System.out.println("Get count by condition: " + query
-
+ ", count = " + count);
-
} catch (BaseException e) {
-
e.printStackTrace();
-
}
-
}
- }
点击(此处)折叠或打开
-
Select Collection Space
-
Select Colleciton
-
Before Insert
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb117"} , "age" : "26" , "name" : "hao-yu"}
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb116"} , "age" : "32" , "name" : "wanghg"}
-
{ "_id" : { "$oid" : "52582e1547c0437e0b000001"} , "age" : 37 , "name" : "wangcc"}
-
{ "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 37 , "name" : "wangcc"}
-
Get count by condition: { }, count = 4
-
-
Start Delete
-
Delete data successfully
-
-
After Delete
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb117"} , "age" : "26" , "name" : "hao-yu"}
-
{ "_id" : { "$oid" : "52582e1547c0437e0b000001"} , "age" : 37 , "name" : "wangcc"}
-
{ "_id" : { "$oid" : "52582e0247c0437e0b000000"} , "age" : 37 , "name" : "wangcc"}
- Get count by condition: { }, count = 3
在随后的几天,将动手讲解SequoiaDB的备份和恢复。
十一、数据库备份sdbexprt
在现在的计算机环境中,由于各种原因,如非法关机,病毒入侵都造成数据损坏是经常碰到的事情,所以数据备份就显得尤为重要了啊。SequoiaDB提供了备份命令来备份你的数据库。这个命令就是sdbexprt。详细的说明如下:
sdbexprt
sdbexprt是一个实用的工具。它可以从SequoiaDB数据库导出一个JSON格式或者CSV格式的数据存储文件。
选项
参数描述
--help,-h 返回基本帮助和用法文本 。
--hostname,-h 从指定主机名的 SequoiaDB 中导出数据。默认情况下 sdbexprt 尝试连接到
本地主机 。
--svcname,-p 指定的端口号。默认情况下 sdbexprt 尝试连接到端口号 50000 的主机 。
--type,-t 指定的导出数据格式。数据格式可以是 CSV ,或是 JSON 。
--output,-o 指定要导出的文件名。注意:不需要写扩展名 。
--delfield,-e 指定字段分隔符。默认是 ', ', JSON 格式无效。
--delrecord,-r 指定记录分隔符。默认是' \n '。
--fieldlist,-f 指定一个或多个字段来导出数据,使用逗号分隔多个字段。
--fieldincluded,-i 指定是否导出字段名,JSON格式无效。
--csname,-c 指定导出数据的集合空间名。
--clname,-l 指定导出数的的集合名。
用法
在下面的例子,sdbexprt从本地数据库端口50000中导出集合空间foo的集合bar的数据,导出类型是
csv,导出文件为contact,导出字段是 field1和field2。
点击(此处)折叠或打开
- sdbexprt –s localhost –p 50000 –t csv –o contace –f field1,field2 –c foo –l bar
错误信息如下:
点击(此处)折叠或打开
-
2013-10-12-04.33.56.361760 Level:ERROR
-
PID:9660 TID:9660
-
Function:getCollection Line:511
-
File:SequoiaDB/engine/pmd/sdbexprt.cpp
-
Message:
-
Failed to get collection foo.bar, rc = -75
-
-
-
2013-10-12-04.34.08.903027 Level:ERROR
-
PID:9662 TID:9662
-
Function:getCollection Line:511
-
File:SequoiaDB/engine/pmd/sdbexprt.cpp
-
Message:
- Failed to get collection bar, rc = -75
点击(此处)折叠或打开
-
> sdb.listCollectionSpaces()
-
{
-
"Name": "foo"
-
}
-
Return 1 row(s).
-
Takes 0.94698s.
-
> sdb.listCollections()
-
{
-
"Name": "foo.bar"
-
}
-
Return 1 row(s).
-
Takes 0.31478s.
- >
11月29日,官方人员给出了解释,是因为1.3版本的导入导出程序有Bug,官方人员给了我最想新的一个版本,经测试,正常。
步骤如下,
将压缩包上传至/opt/sequoiadb/bin目录,然后执行tar -zxvf sdb.tar.gz,解压后会覆盖就的备份恢复命令。
执行过程如下:
点击(此处)折叠或打开
-
root@ubuntu:/opt/sequoiadb/bin# ./sdbexprt --help
-
Command options:
-
--help help
-
-h [ --hostname ] arg database host name
-
-s [ --svcname ] arg database service name
-
-a [ --delchar ] arg string delimiter ( default: " )
-
-e [ --delfield ] arg field delimiter ( default: , )
-
-r [ --delrecord ] arg record delimiter ( default: '\n' )
-
-f [ --fieldlist ] arg field list ( separate by , )
-
-i [ --fieldincluded ] arg include field names ( default: true )
-
-c [ --csname ] arg collection space name
-
-l [ --clname ] arg collection name
-
--file arg database load file name
-
--type arg type of file to load, default: json (json,csv)
-
-
root@ubuntu:/opt/sequoiadb/bin# ./sdbexprt -h localhost -s 50000 --file ./foo.bar.csv --type csv -c foo -l bar
-
Field list is required for CSV file, fields are separated by comma ( ',' )
-
Export Failed
-
-
root@ubuntu:/opt/sequoiadb/bin# ./sdbexprt -h localhost -s 50000 -e'|' -c foo -l
-
bar --file ./exprt.json --type json
-
Export Successfully
-
Detail in log path: sdbexport.log
-
-
root@ubuntu:/opt/sequoiadb/bin# cat exprt.json
-
{ "Age" : 37, "_id" : { "$oid" : "52582e0247c0437e0b000000" }, "age" : 37, "name" : "wangcc" }
-
{ "Age" : 37, "_id" : { "$oid" : "52582e1547c0437e0b000001" }, "age" : 37, "name" : "wangcc", "phone" : "123456" }
-
{ "_id" : { "$oid" : "5267d1f7b5b7332e021fb117" }, "name" : "hao-yu", "age" : "26", "jiguan" : "benxi" }
-
-
root@ubuntu:/opt/sequoiadb/bin# ./sdbexprt -h localhost -s 50000 -f Age,age,name,phone,jiguan -c foo -l bar --file ./exprt.csv --type csv
-
Export Successfully
-
Detail in log path: sdbexport.log
-
-
root@ubuntu:/opt/sequoiadb/bin# cat exprt.csv
-
"Age","age","name","phone","jiguan"
-
37,37,"wangcc",,
-
37,37,"wangcc","123456",
- ,"26","hao-yu",,"benxi
导入命令sdbimprt和导出是相反的过程,导入命令的参数如下:
点击(此处)折叠或打开
-
root@ubuntu:~# cd /opt/sequoiadb/bin
-
root@ubuntu:/opt/sequoiadb/bin# ./sdbimprt --help
-
Command options:
-
--help help
-
-h [ --hostname ] arg database host name
-
-s [ --svcname ] arg database service name
- -a [ --delchar ] arg string delimiter ( default:
正确的导入到处命令的下载参照如下附件:
sdb.part1.rar
sdb.part2.rar
sdb.part3.rar
sdb.part4.rar
sdb.part5.rar
总结:
至此,SequoiaDB数据的安装,配置(独立模式),通过java对数据库进行增删改查以及使用sdb shell进行查询,数据库的导入导出(备份恢复)的测试基本完成,但由于是虚拟机操作,对性能的测试受限制无法进行,同时次发布版本是社区版,企业版本没法获取,如果需要企业版本可以联系contact@sequoiadb.com以获取发行的企业版本,我没有要求企业版。由于社区版的导入导出命令有缺陷,是在官方人员给与我新的版本后测试成功的,如果需要可以从本附件下载或者联系我获取(也可以联系官方获取)。
通过本次测试,让我对NoSQL技术有了一定的了解,对SequoiaDB数据库的使用有了更进一步的掌握,如果附近的朋友有需要帮助的可以联系我,我会给予一定的支持与帮助,共同推进SequoiaDB数据库的良好发展。