hadoop学习之MongoDB增删改查Java实现

来源:互联网 发布:免费收支软件 编辑:程序博客网 时间:2024/05/01 21:09

最近看了网上的好多例子,自己简单总结了下,基于Java语言利用junit测试了相关的增删改查的内容,为以后深入学习MongoDB打基础。

1.项目如下:


注意在项目中引入mongo-java-driver的相关jar包

2.测试类MongoDBUtilTest的代码如下:

package com.tao.junit;import org.junit.Test;import com.tao.MongoDBUtil;public class MongoDBUtilTest {@Testpublic void testInsert(){MongoDBUtil.insert("School", "student", "sname", "Mary", "sage", 25);MongoDBUtil.insert("School", "student", "sname", "Bob", "sage", 22);}@Testpublic void testFindAll(){MongoDBUtil.findAll("School", "student");}@Testpublic void testfindOne(){MongoDBUtil.findOne("School", "student", "sname", "Mary", "sage", "_id");}@Testpublic void testUpdate(){MongoDBUtil.update("School", "student", "sname", "Mary", "sage", 18);}@Testpublic void testDelete(){MongoDBUtil.delete("School", "student", "sname", "list");}}
3.具体实现类MongoDBUtil的代码如下:

package com.tao;import java.util.ArrayList;import java.util.List;import org.bson.Document;import com.mongodb.MongoClient;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;import com.mongodb.client.model.Filters;public class MongoDBUtil {//MongoDB无需预定义数据库和集合,在使用的时候会自动创建public static MongoCollection<Document> getCollection(String dbName,String collectionName){//实例化一个mongo客户端,服务器地址:localhost(本地),端口号:2701MongoClient mongoClient = new MongoClient("localhost",27017);//实例化一个mongo数据库MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);//获取数据库中某个集合return mongoDatabase.getCollection(collectionName);}public static void insert(String dbName,String collectionName,String key1,String value1,String key2,int value2){//连接MongoDB,指定连接数据库名,指定连接表名MongoCollection<Document> collection = getCollection(dbName, collectionName);//数据库名:School 集合名:student        //实例化一个文档,文档内容为{sname:'Mary',sage:25},如果还有其他字段,可以继续追加appendDocument doc1 = new Document(key1,value1).append(key2, value2);//实例化一个文档,文档内容为{sname:'Bob',sage:20}Document doc2 = new Document(key1,value1).append(key2, value2);List<Document> documents = new ArrayList<Document>();//将doc1、doc2加入到documents列表中documents.add(doc1);documents.add(doc2);//将documents插入集合collection.insertMany(documents);System.out.println("insert success!");}public static void findAll(String dbName,String collectionName){MongoCollection<Document> collection = getCollection(dbName, collectionName);MongoCursor<Document> cursor = collection.find().iterator();while(cursor.hasNext()){System.out.println(cursor.next().toJson());}}// does not conclude id numberpublic static void findOne(String dbName,String collectionName,String stuName,String stuValue,String stuAge,String stuId){MongoCollection<Document> collection = getCollection(dbName, collectionName);//数据库名:School 集合名:student        //通过游标遍历检索出的文档集合 //MongoCursor<Document>  cursor= collection.find(new Document("sname","Mary")). projection(new Document("sname",1).append("sage",1).append("_id", 0)).iterator();   //find查询条件:sname='Mary'。projection筛选:显示sname和sage,不显示_id(_id默认会显示)        //查询所有数据MongoCursor<Document> cursor = collection.find(new Document(stuName,stuValue)).projection(new Document(stuName,1).append(stuAge, 1).append(stuId, 0)).iterator();while(cursor.hasNext()){System.out.println(cursor.next().toJson());}}public static void update(String dbName,String collectionName,String stuName,String stuValue,String stuAge,int stuAgeValue){MongoCollection<Document> collection = getCollection(dbName, collectionName);//数据库名:School 集合名:student        //更新文档   将文档中sname='Mary'的文档修改为sage=22collection.updateMany(Filters.eq(stuName, stuValue), new Document("$set",new Document(stuAge,stuAgeValue)));System.out.println("update success!");}public static void delete(String dbName,String collectionName,String stuName,String stuValue){//数据库名:School 集合名:student        //删除符合条件的第一个文档//删除所有符合条件的文档          //collection.deleteMany (Filters.eq("sname", "Bob"));getCollection(dbName, collectionName).deleteOne(Filters.eq(stuName, stuValue));System.out.println("delete success!");}}
希望能够与更多的学习者交流,相互促进学习,共同进步。最近,csdn没积分了,把自己实现的代码上传到了csdn下载频道,希望各位下载的话能奉献1积分,谢谢。也希望能帮助刚刚入门hadoop的初学者。代码下载连接地址:点击打开链接。
0 0
原创粉丝点击