大数据 |
您所在的位置:网站首页 › nosql应用实例 › 大数据 |
大数据——四种数据库(MySQL,HBase,MongoDB,Redis)操作实例
问题描述:
student文档如下: 1. 根据上面给出的文档信息,用MongoDB模式设计student集合。 a) 设计完后,用find指令浏览集合的相关信息,给出截图。 b) 查询zhangsan 的Computer成绩,给出截图。 c) 修改lisi的Math成绩,改为95,给出截图。 2. 根据上面已经设计出的student集合,用MongoDB的JAVA客户端编程; a) 添加数据:name: scofield,English:45 Math:89 Computer:100 b) 获取scofield的English成绩信息 问题解决: 1. 根据上面给出的文档信息,用MongoDB模式设计student集合。设计集合代码:(复制粘贴代码时,要删除注释部分,下同) hadoop@ -VirtualBox:~$ mongo//启动MongoDB 在>提示符下,分别键入如下语句: > use Stu //数据库Stu被自动创建 > db.createCollection('student')//创建集合student > show dbs//显示所有数据库 > use Stu > var stus=[{"name":"zhangsan","scores":{"English":69,"Math":86,"Computer":77}},{"name":"lisi","score":{"English":55,"Math":100,"Computer":88}}] //注意,这个是一整行 > db.student.insert(stus)//向集合中插入数据 对应的Linux终端运行截图: a) 设计完后,用find指令浏览集合的相关信息,给出截图。 > db.student.find().pretty() //显示集合信息 对应的Linux终端运行截图: b) 查询zhangsan 的Computer成绩,给出截图。 > db.student.find({"name":"zhangsan"},{"_id":0,"name":0}) //查询张三的成绩,不显示id和姓名 对应的Linux终端运行截图: c) 修改lisi的Math成绩,改为95,给出截图。 > db.student.update({"name":"lisi"}, {"$set":{"score.Math":95}} ) //修改李四的数学成绩 对应的Linux终端运行截图: 2. 根据上面已经设计出的student集合,用MongoDB的JAVA客户端编程;(1)按Ubuntu操作系统桌面左上角的搜索图标,搜索到Eclipse,点击打开 (2)新建一个Java Project,随便起一个工程名,点击完成 a) 添加数据:name: scofield,English:45 Math:89 Computer:100 (1)右键点击工程名,新建一个class,起名mongo_insert (2)将如下代码粘贴至mongo_insert .java 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.MongoDatabase; public class mongo_insert { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //实例化一个mongo客户端 MongoClient mongoClient=new MongoClient("localhost",27017); //实例化一个mongo数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("Stu"); //获取数据库中某个集合 MongoCollection collection = mongoDatabase.getCollection("student"); //实例化一个文档,内嵌一个子文档 Document document=new Document("name","scofield"). append("score", new Document("English",45). append("Math", 89). append("Computer", 100)); List documents = new ArrayList(); documents.add(document); //将文档插入集合中 collection.insertMany(documents); System.out.println("文档插入成功"); } }(3)将JDBC驱动mongo-java-driver-3.4.2.jar导入到工程中去,操作方法:工程名(右键)--buildpath--configure build path--add external jars(在Libraries栏目下),添加mongo-java-driver-3.4.2.jar(如果没有jar包,可下载:四种数据库(MySQL,HBase,Redis,MongoDB)的Java客户端所需jar包.zip-其它文档类资源-CSDN下载),点击确定。 (4)运行此mongo_insert .java,在Linux终端通过select语句查询名为scofield的学生记录是否已经被添加? Java运行结果: MongoDB检验结果:scofield的学生记录已经被添加 b) 获取scofield的English成绩信息 (1)为了获取scofield的English成绩信息,请将如下代码添加至新建的mongo_query.java文件中,调试运行,给出结果截图。 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; import static com.mongodb.client.model.Filters.eq; public class mongo_query { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //实例化一个mongo客户端 MongoClient mongoClient=new MongoClient("localhost",27017); //实例化一个mongo数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("Stu"); //获取数据库中某个集合 MongoCollection collection = mongoDatabase.getCollection("student"); //进行数据查找,查询条件为name=scofield, 对获取的结果集只显示score这个域 MongoCursor cursor=collection.find( new Document("name","scofield")). projection(new Document("score",1).append("_id", 0)).iterator(); while(cursor.hasNext()) System.out.println(cursor.next().toJson()); } }Java运行结果: 补充:因为我们使用的是Ubuntu16.04,其安装mongodb的方法会与Ubuntu14.04下安装mongodb的方法有些差异,如果你想在自己的机器的Ubuntu16.04上自己动手安装mongodb,请参考 How to Install MongoDB on Ubuntu 16.04 | DigitalOcean |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |