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

最近看了网上的好多例子,自己简单总结了下,基于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 {
	
	@Test
	public void testInsert(){
		MongoDBUtil.insert("School", "student", "sname", "Mary", "sage", 25);
		MongoDBUtil.insert("School", "student", "sname", "Bob", "sage", 22);
	}
	
	@Test
	public void testFindAll(){
		MongoDBUtil.findAll("School", "student");
	}
	
	@Test
	public void testfindOne(){
		MongoDBUtil.findOne("School", "student", "sname", "Mary", "sage", "_id");
	}
	
	@Test
	public void testUpdate(){
		MongoDBUtil.update("School", "student", "sname", "Mary", "sage", 18);
	}
	
	@Test
	public 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(本地),端口号:2701
		MongoClient 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},如果还有其他字段,可以继续追加append
		Document 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 number
	public 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=22
		collection.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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值