大数据技术之Java操作MongoDB实战
沉沙 2019-03-22 来源 : 阅读 1680 评论 0

摘要:本篇文章探讨了大数据技术之Java操作MongoDB实战,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。

本篇文章探讨了大数据技术之Java操作MongoDB实战,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。

大数据技术之Java操作MongoDB实战

1 准备工作

1.1 开发环境

操作系统:Windows 7

IDE开发工具:MyEclipse 8

数据库:MongoDB 2.4.4

JDK:1.7.0_25

1.2 开发依赖库

mongo-2.10.1.jar

1.3 创建Java工程

新建一个MongoDB工程,导入需要的jar包:

1.4 启动MongoDB

在CMD里面执行如下命令来启动MongoDB:D:\Download\software\mongodb-win32-i386-2.4.4\bin\mongod --dbpath=E:\Mongodb --logpath=E:\Mongodb\logs\mongodb.log –rest

2 Java操作MongoDB

2.1 演示样例

本文将以客户基本信息为例来演示如何在Java程序里面操作MongoDB。

假设客户信息(Customer)包含如下基本字段:名字(name)、年龄(age)、性别(sex)等。

2.2 创建客户信息类

Customer类代码如下:   

          

package com;

           

public class Customer

           

{

           

private String name;

           

private int age;

           

private String sex;

           

public String getName()

           

{

           

return name;

           

}

           

public void setName(String name)

           

{

           

this.name = name;

           

}

           

public int getAge()

           

{

           

return age;

           

}

           

public void setAge(int age)

           

{

           

this.age = age;

           

}

           

public String getSex()

           

{

           

return sex;

           

}

           

public void setSex(String sex)

           

{

           

this.sex = sex;

           

}

           

@Override

           

public String toString()

           

{

           

return "Customer [age=" + age + ", name=" + name + ", sex=" + sex + "]";

           

}

           

}

        

2.3 创建客户信息操作类

客户信息操作类CustomerService.java的代码如下所示:   

          

package com;

           

import java.net.UnknownHostException;

           

import java.util.ArrayList;

           

import java.util.List;

           

import com.mongodb.BasicDBObject;

           

import com.mongodb.DB;

           

import com.mongodb.DBCollection;

           

import com.mongodb.DBCursor;

           

import com.mongodb.DBObject;

           

import com.mongodb.Mongo;

           

public class CustomerService

           

{

           

static final String MONGODB_HOST = "127.0.0.1";

           

static final int MONGODB_PORT = 27017;

           

static final String DB_NAME = "test";

           

static final String COLLECTION_NAME = "users";

           

/**

           

* 获取MongoDB对象,以便进行增删改查操作

           

*

           

* @return MongoDB对象

           

* @throws UnknownHostException

           

*/

           

public static Mongo getMongoDB() throws UnknownHostException

           

{

           

Mongo mongo = new Mongo(CustomerService.MONGODB_HOST,

           

CustomerService.MONGODB_PORT);

           

return mongo;

           

}

           

/**

           

* 添加一条客户信息记录

           

*

           

* @param customer

           

* 客户信息

           

*/

           

public static void addCustomer(Customer customer)

           

{

           

try

           

{

           

Mongo mongo = CustomerService.getMongoDB();

           

DB db = mongo.getDB(CustomerService.DB_NAME);

           

DBCollection dbCollection = db

           

.getCollection(CustomerService.COLLECTION_NAME);

           

DBObject dbObject = new BasicDBObject();

           

dbObject.put("name", customer.getName());

           

dbObject.put("age", customer.getAge());

           

dbObject.put("sex", customer.getSex());

           

dbCollection.insert(dbObject);

           

} catch (UnknownHostException e)

           

{

           

e.printStackTrace();

           

}

           

}

           

/**

           

* 删除一条客户信息

           

*

           

* @param customer

           

* 客户信息

           

*/

           

public static void removeCustomer(Customer customer)

           

{

           

try

           

{

           

Mongo mongo = CustomerService.getMongoDB();

           

DB db = mongo.getDB(CustomerService.DB_NAME);

           

DBCollection dbCollection = db

           

.getCollection(CustomerService.COLLECTION_NAME);

           

DBObject dbObject = new BasicDBObject();

           

dbObject.put("name", customer.getName());

           

dbObject.put("age", customer.getAge());

           

dbObject.put("sex", customer.getSex());

           

dbCollection.remove(dbObject);

           

} catch (UnknownHostException e)

           

{

           

e.printStackTrace();

           

}

           

}

           

/**

           

* 修改客户信息

           

*

           

* @param oldCustomer

           

* 旧的客户信息

           

* @param newCustomer

           

* 新的客户信息

           

*/

           

public static void updateCustomer(Customer oldCustomer, Customer newCustomer)

           

{

           

try

           

{

           

Mongo mongo = CustomerService.getMongoDB();

           

DB db = mongo.getDB(CustomerService.DB_NAME);

           

DBCollection dbCollection = db

           

.getCollection(CustomerService.COLLECTION_NAME);

           

DBObject oldDBObject = new BasicDBObject();

           

DBObject newDBObject = new BasicDBObject();

           

oldDBObject.put("name", oldCustomer.getName());

           

oldDBObject.put("age", oldCustomer.getAge());

           

oldDBObject.put("sex", oldCustomer.getSex());

           

newDBObject.put("name", newCustomer.getName());

           

newDBObject.put("age", newCustomer.getAge());

           

newDBObject.put("sex", newCustomer.getSex());

           

dbCollection.update(oldDBObject, newDBObject);

           

} catch (UnknownHostException e)

           

{

           

e.printStackTrace();

           

}

           

}

           

/**

           

* 查询全部客户信息列表

           

*

           

* @return 客户信息列表

           

*/

           

public static List<Customer> getCustomerList()

           

{

           

List<Customer> customerList = new ArrayList<Customer>();

           

Customer customer = null;

           

try

           

{

           

Mongo mongo = CustomerService.getMongoDB();

           

String dbName = "test";

           

String collectionName = "users";

           

DB db = mongo.getDB(dbName);

           

DBCollection dbCollection = db.getCollection(collectionName);

           

DBCursor cur = dbCollection.find();

           

DBObject dbObject = null;

           

while (cur.hasNext())

           

{

           

dbObject = cur.next();

           

customer = new Customer();

           

customer.setName((String) dbObject.get("name"));

           

customer.setAge((Integer) dbObject.get("age"));

           

customer.setSex((String) dbObject.get("sex"));

           

customerList.add(customer);

           

}

           

} catch (UnknownHostException e)

           

{

           

e.printStackTrace();

           

}

           

return customerList;

           

}

           

/**

           

* 为方便测试,该方法将客户信息列表打印出来

           

*

           

* @param customerList

           

* 客户信息列表

           

*/

           

public static void printCustomerList(List<Customer> customerList)

           

{

           

System.out.println(">>>>>>>>>>>>>>>> CustomerList >>>>>>>>>>>>>>");

           

if (null == customerList || customerList.isEmpty())

           

{

           

return;

           

}

           

for (int i = 0; i < customerList.size(); i++)

           

{

           

System.out.println(customerList.get(i).toString());

           

}

           

System.out.println("<<<<<<<<<<<<<<< CustomerList <<<<<<<<<<<<<<<");

           

}

           

}

        

2.4 创建测试类

测试类Test.java代码如下:   

          

package com;

           

import java.net.UnknownHostException;

           

public class Test

           

{

           

/**

           

* @param args

           

* @throws UnknownHostException

           

* @throws InterruptedException

           

*/

           

public static void main(String[] args) throws UnknownHostException,

           

InterruptedException

           

{

           

System.out.println("Enter main");

           

CustomerService.printCustomerList(CustomerService.getCustomerList());

           

Customer customer = new Customer();

           

customer.setName("mazi");

           

customer.setAge(30);

           

customer.setSex("man");

           

CustomerService.removeCustomer(customer);

           

CustomerService.printCustomerList(CustomerService.getCustomerList());

           

CustomerService.addCustomer(customer);

           

CustomerService.printCustomerList(CustomerService.getCustomerList());

           

Customer newCustomer = new Customer();

           

newCustomer.setName("mazi");

           

newCustomer.setAge(33);

           

newCustomer.setSex("man");

           

CustomerService.updateCustomer(customer, newCustomer);

           

CustomerService.printCustomerList(CustomerService.getCustomerList());

           

CustomerService.removeCustomer(newCustomer);

           

CustomerService.printCustomerList(CustomerService.getCustomerList());

           

Thread.sleep(1000);

           

System.out.println("End main");

           

}

           

}

        

2.5 执行测试

确认工程无报错之后,就可以点击


进行测试了,如果一切OK的话,将会在console输出如下信息:

      本文由职坐标整理发布,学习更多的相关知识,请关注职坐标IT知识库!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 2 不喜欢 | 0
看完这篇文章有何感觉?已经有2人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程