import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.bson.types.ObjectId;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.Bytes;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.QueryOperators;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.mongodb.util.JSON;
/**
* @Title: MongoDB.java
* @Package
* @Description: TODO(用一句话描述该文件做什么)
* @author 石华才
* @date 2013-1-17 下午05:00:20
*/
/**
* @ClassName: MongoDB
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 石华才
* @date 2013-1-17 下午05:00:20
*/
@SuppressWarnings("deprecation")
public class MongoDB {
private Mongo mg = null;
private DB db;
private DBCollection users;
public static void main(String[] args){
try{
//数据库文件的位置取决于mongo服务启动时指定的位置
Mongo mongo = new Mongo();
//查询所有的Database
for (String name : mongo.getDatabaseNames()) {
System.out.println("dbName: " + name);
}
//连接指定的数据库
DB db = mongo.getDB("copytest");
//查询所有的聚集集合(查询copytest库下的所有表)
for (String name : db.getCollectionNames()) {
System.out.println("collectionName: " + name);
}
//连接指定的表(集合)
DBCollection testCollection = db.getCollection("foo");
DBCursor cur = testCollection.find();
while(cur.hasNext()){
System.out.println(cur.next());
}
System.out.println(cur.count());
System.out.println(cur.getCursorId());
System.out.println(JSON.serialize(cur));
}catch(Exception e){
System.out.println(e);
}
}
@Before
public void init() {
try {
mg = new Mongo();
//mg = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
//获取temp DB;如果默认没有创建,mongodb会自动创建
db = mg.getDB("temp");
//获取users DBCollection;如果默认没有创建,mongodb会自动创建
users = db.getCollection("users");
}
/**
* <b>function:</b> 查询所有数据
* @author hoojo
* @createDate 2011-6-2 下午03:22:40
*/
@Test
public void queryAll() {
print("查询users的所有数据:");
//db游标
DBCursor cur = users.find();
while (cur.hasNext()) {
print(cur.next());
}
}
@Test
public void add() {
//先查询所有数据
queryAll();
print("count: " + users.count());
DBObject user = new BasicDBObject();
user.put("name", "hoojo2");
user.put("age", 242);
users.save(user);//保存,getN()获取影响行数
print(users.save(user).getN());
//扩展字段,随意添加字段,不影响现有数据
user.put("sex", "男");
print(users.save(user).getN());
//添加多条数据,传递Array对象
print(users.insert(user, new BasicDBObject("name", "tom")).getN());
//添加List集合
List<DBObject> list = new ArrayList<DBObject>();
list.add(user);
DBObject user2 = new BasicDBObject("name", "lucy");
user.put("age", 22);
list.add(user2);
//添加List集合
print(users.insert(list).getN());
//查询下数据,看看是否添加成功
print("count: " + users.count());
queryAll();
}
@Test
public void remove() {
queryAll();
print("删除id = 4de73f7acd812d61b4626a77:" + users.remove(new BasicDBObject("_id", new ObjectId("4de73f7acd812d61b4626a77"))).getN());
print("remove age >= 24: " + users.remove(new BasicDBObject("age", new BasicDBObject("$gte", 24))).getN());
}
@Test
public void modify() {
print("修改:" + users.update(new BasicDBObject("_id", new ObjectId("4dde25d06be7c53ffbd70906")), new BasicDBObject("age", 99)).getN());
print("修改:" + users.update(
new BasicDBObject("_id", new ObjectId("4dde2b06feb038463ff09042")),
new BasicDBObject("age", 121),
true,//如果数据库不存在,是否添加
false//多条修改
).getN());
print("修改:" + users.update(
new BasicDBObject("name", "haha"),
new BasicDBObject("name", "dingding"),
true,//如果数据库不存在,是否添加
true//false只修改第一天,true如果有多条就不修改
).getN());
//当数据库不存在就不修改、不添加数据,当多条数据就不修改
//print("修改多条:" + coll.updateMulti(new BasicDBObject("_id", new ObjectId("4dde23616be7c19df07db42c")), new BasicDBObject("name", "199")));
}
//mongoDB不支持联合查询、子查询,这需要我们自己在程序中完成。将查询的结果集在Java查询中进行需要的过滤即可。
@Test
public void query() {
//查询所有
//queryAll();
//查询id = 4de73f7acd812d61b4626a77
print("find id = 4de73f7acd812d61b4626a77: " + users.find(new BasicDBObject("_id", new ObjectId("4de73f7acd812d61b4626a77"))).toArray());
//查询age = 24
print("find age = 24: " + users.find(new BasicDBObject("age", 24)).toArray());
//强制使用某个索引
//users.setHintFields(lst);
//查询age >= 24
print("find age >= 24: " + users.find(new BasicDBObject("age", new BasicDBObject("$gte", 24))).toArray());
print("find age <= 24: " + users.find(new BasicDBObject("age", new BasicDBObject("$lte", 24))).toArray());
print("查询age!=25:" + users.find(new BasicDBObject("age", new BasicDBObject("$ne", 25))).toArray());
print("查询age in 25/26/27:" + users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.IN, new int[] { 25, 26, 27 }))).toArray());
print("查询age not in 25/26/27:" + users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.NIN, new int[] { 25, 26, 27 }))).toArray());
print("查询age exists 排序:" + users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.EXISTS, true))).toArray());
print("只查询age属性:" + users.find(null, new BasicDBObject("age", true)).toArray());
print("只查属性:" + users.find(null, new BasicDBObject("age", true), 0, 2).toArray());
print("只查属性:" + users.find(null, new BasicDBObject("age", true), 0, 2, Bytes.QUERYOPTION_NOTIMEOUT).toArray());
//只查询一条数据,多条去第一条
print("findOne: " + users.findOne());
print("findOne: " + users.findOne(new BasicDBObject("age", 26)));
print("findOne: " + users.findOne(new BasicDBObject("age", 26), new BasicDBObject("name", true)));
//查询修改、删除
print("findAndRemove 查询age=25的数据,并且删除: " + users.findAndRemove(new BasicDBObject("age", 25)));
//查询age=26的数据,并且修改name的值为Abc
print("findAndModify: " + users.findAndModify(new BasicDBObject("age", 26), new BasicDBObject("name", "Abc")));
print("findAndModify: " + users.findAndModify(
new BasicDBObject("age", 28), //查询age=28的数据
new BasicDBObject("name", true), //查询name属性
new BasicDBObject("age", true), //按照age排序
false, //是否删除,true表示删除
new BasicDBObject("name", "Abc"), //修改的值,将name修改成Abc
true,
true));
queryAll();
}
@Test
public void testOthers() {
DBObject user = new BasicDBObject();
user.put("name", "hoojo");
user.put("age", 24);
//JSON 对象转换
print("serialize: " + JSON.serialize(user));
//反序列化
print("parse: " + JSON.parse("{ \"name\" : \"hoojo\" , \"age\" : 24}"));
print("判断temp Collection是否存在: " + db.collectionExists("temp"));
//如果不存在就创建
if (!db.collectionExists("temp")) {
DBObject options = new BasicDBObject();
options.put("size", 20);
options.put("capped", 20);
options.put("max", 20);
print(db.createCollection("account", options));
}
//设置db为只读
db.setReadOnly(true);
//只读不能写入数据
db.getCollection("test").save(user);
}
@After
public void destory() {
if (mg != null)
mg.close();
mg = null;
db = null;
users = null;
System.gc();
}
public void print(Object o) {
System.out.println(o);
}
@Test
public void savePicture(){
try{
//如果没有,自动创建
DB db = mg.getDB("beijingVector");
//DBCollection collection = db.getCollection("beijingVector");
GridFS gridFS= new GridFS(db);
GridFSInputFile gfs = gridFS.createFile(new FileInputStream(new File("E:\\瓦片数据\\北京影像\\L11\\R000002fc\\C0000069b.jpg")));
gfs.put("filename", "aaaaaaaaaaa");
gfs.save();
}catch (Exception e) {
// TODO: handle exception
}
}
@Test
public void getPictureByName(){
try{
//如果没有,自动创建
DB db = mg.getDB("beijingVector");
GridFS gridFS= new GridFS(db);
/*DBObject query = new BasicDBObject("filename", fileName);
GridFSDBFile gridFSDBFile = myFS.findOne(query);
//根据ID查询
DBObject query = new BasicDBObject("_id", id);
GridFSDBFile gridFSDBFile = myFS.findOne(query);*/
GridFSDBFile gridFSDBFile = gridFS.findOne("aaaaaaaaaaa");//直接查询其文件名
gridFSDBFile.writeTo("c:\test.png");
InputStream is = gridFSDBFile.getInputStream();
int buffer_size = 1024;
FileOutputStream fos = new FileOutputStream(new File("C:\\cc.png"));
final byte[] b = new byte[buffer_size];
int read;
while ((read = is.read(b)) != -1) {
fos.write(b, 0, read);
}
is.close();
fos.flush();
fos.close();
if(gridFSDBFile != null)
return;
}catch (Exception e) {
}
}
//以二进制的方式进行保存
@Test
public void savePicByDoc(){
try{
//如果没有,自动创建
DB db = mg.getDB("beijingVector");
DBCollection collection = db.getCollection("beijingVector");
InputStream is = new FileInputStream(new File("E:\\瓦片数据\\北京影像\\L11\\R000002fc\\C0000069b.jpg"));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = is.read(buffer))) {
output.write(buffer, 0, n);
}
byte[] imgByte = output.toByteArray();
DBObject user = new BasicDBObject();
user.put("name", "aa");
user.put("img", imgByte);
collection.save(user);
//批量插入
//collection.insert(list);
}catch (Exception e) {
// TODO: handle exception
}
}
@Test
public void getPicFromByte(){
try{
//如果没有,自动创建
DB db = mg.getDB("beijingVector");
DBCollection collection = db.getCollection("beijingVector");
List<DBObject> list = collection.find(new BasicDBObject("name", "aa")).toArray();
if(list != null&& list.size() > 0){
byte[] b = (byte[]) list.get(0).get("img");
//ByteArrayInputStream bas = new ByteArrayInputStream(b);
FileOutputStream fos = new FileOutputStream(new File("c:\\a.png"));
fos.write(b, 0, b.length);
fos.flush();
fos.close();
}
}catch (Exception e) {
}
}
/**
*
* @Title: ensureIndex
* @Params :
* @Description: TODO(创建索引)
* @return void 返回类型
* @author 石华才
* @throws
*/
@Test
public void ensureIndex(){
try{
//如果没有,自动创建
DB db = mg.getDB("beijingVector");
DBCollection collection = db.getCollection("beijingVector");
//单字段索引
collection.ensureIndex(new BasicDBObject("name", 1), "nameIndex", true);
//复合索引
BasicDBObject obj = new BasicDBObject();
obj.put("Age", 1);//升序
obj.put("Name", -1);//降序
collection.ensureIndex(obj);
List<DBObject> list = collection.getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
}catch (Exception e) {
}
}
/**
* @Title: 配置主从集群(主从复制)
* @Params :
* @Description: TODO(测试时,请不要在默认的local集合中添加数据,否则看不到效果,默认不同步local中的数据)
* @return void 返回类型
* @author 石华才
* @throws
*/
@Test
public void addDataToMaster() throws UnknownHostException{
{
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 1000));
DB db = mongoClient.getDB("testMasterDB");
DBCollection collection = db.getCollection("testMasterCollection");
DBObject dbObject = new BasicDBObject();
dbObject.put("name", "测试用力4");
collection.save(dbObject);
DBCursor cursor = collection.find();
while(cursor.hasNext()){
DBObject obj = cursor.next();
System.out.println(obj.toString());
}
mongoClient.close();
}
//系统默认10s同步一次,故下面代码要在十秒以后才能执行,请直接在mongoVUE中查看效果
{
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 10002));
DB db = mongoClient.getDB("testMasterDB");
DBCollection collection = db.getCollection("testMasterCollection");
DBCursor cursor = collection.find();
while(cursor.hasNext()){
DBObject obj = cursor.next();
System.out.println(obj.toString());
}
mongoClient.close();
}
}
/**
* @Title: 配置副本集(活跃节点与备份节点之间也能自动复制数据)
* @Params :
* @Description: TODO()
* @return void 返回类型
* @author 石华才
* @throws
*/
@Test
public void addDataToReplSet() throws UnknownHostException{
{ //不能连接非活跃节点,原因暂不明,shell只能连接,不能插入数据,报not master错误
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 10001));
//设置Secondary节点可读,在Secondary里面也需要做如下设置
mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
DB db = mongoClient.getDB("testReplSetDB");
DBCollection collection = db.getCollection("testReplSetCollection");
DBObject dbObject = new BasicDBObject();
dbObject.put("name", "etrhgw文替我太re");
collection.save(dbObject);
DBCursor cursor = collection.find();
while(cursor.hasNext()){
DBObject obj = cursor.next();
System.out.println(obj.toString());
}
mongoClient.close();
}
{
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 10002));
mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
DB db = mongoClient.getDB("testReplSetDB");
DBCollection collection = db.getCollection("testReplSetCollection");
DBCursor cursor = collection.find();
while(cursor.hasNext()){
DBObject obj = cursor.next();
System.out.println(obj.toString());
}
mongoClient.close();
}
}
}
MongoDB之Java开发入门
最新推荐文章于 2023-10-07 21:41:13 发布