mongodb web

当今时代是数据以非常大的速度增长的时代。 数据存储不是问题,是的,但是它的结构化和存储方式可能会增加或减少所需数据块的查找时间。

不断增长的非结构化数据的用例

  • 脸书:
    • 活跃用户达7.5亿,互联网用户中有三分之一拥有Facebook帐户
    • 每月共享的内容超过300亿条(Web链接,新闻报道,博客文章,便笺,相册等)。
    • 容纳30PB数据进行分析,每天增加12 TB压缩数据
  • 推特
    • 2亿用户,每日2亿条推文
    • 每天有16亿个搜索查询
    • 每天生成7 TB数据用于分析

在这样的规模下,传统的数据存储,技术和分析工具不起作用!

当前方案要求需要NoSQL数据库(例如Apache Cassandra,Mongo DB)来处理不断增长的非结构化数据。 NoSQL数据库提供比传统RDBMS宽松的一致性模型,用于存储和检索数据。 NoSQL数据库将数据存储为高度优化的键值对,这导致简单的检索和附加操作,从而在低延迟和高吞吐量方面提高了性能。

NoSQL数据库在开发和维护大数据和实时Web应用程序的行业中起着重要作用。

Mongo DB和Web应用程序的用例

让我们想象一下,我们想在后端使用JSF2.0和Mongo DB创建一个汽车注册门户。 将有两个功能

  • 汽车登记
  • 查看已注册汽车的报告

图1描述了要进行的项目的流程。

假设:

  • Mongo DB已安装并作为服务运行
  • Eclipse靛蓝或更高版本
  • Tomcat 7.0或更高版本
  • 存在必需的jar文件(JSF2.0 jars jsf-api.jar,jsf-impl),mongo-java-driver-2.10.1.jar或任何合适的版本

应用程序具有三个页面,即Home.xhtml,AddCar.xhtml,Report.xhtml。

Home.xhtml的实现如下:

Home.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:f="http://java.sun.com/jsf/core"xmlns:h="http://java.sun.com/jsf/html"xmlns:j="http://java.sun.com/jsp/jstl/core">
<h:head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></h:head><h:body><f:view>
<h:form><center>
<h2> MSD Car Portal</h2><h4><h:outputLink value="AddCar.xhtml">Add Car</h:outputLink><br/><br/>
<h:commandLink action="#{carBean.getCarDetails}" >See Registered Cars</h:commandLink></h4></center></h:form>
</f:view></h:body>
</html>

单击“ 添加汽车”链接后 ,将加载AddCar.xhtml并执行以下代码行。

AddCar.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:f="http://java.sun.com/jsf/core"xmlns:h="http://java.sun.com/jsf/html"xmlns:j="http://java.sun.com/jsp/jstl/core">
<h:head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></h:head><h:body><f:view>
<h:form><center><h2>MSD Car Portal</h2>
<h3>Add a Car</h3><h:panelGrid border="2" columns="2"><h:outputText value="CarName"></h:outputText>
<h:inputText value="#{carBean.carTO.carName}"></h:inputText><h:outputText value="Company"></h:outputText>
<h:inputText value="#{carBean.carTO.company}"></h:inputText><h:outputText value="Model"></h:outputText>
<h:inputText value="#{carBean.carTO.model}">
<f:convertDateTime pattern="dd-MMM-yyyy"></f:convertDateTime>
</h:inputText><h:outputText value="CC"></h:outputText>
<h:inputText value="#{carBean.carTO.cc}"></h:inputText><h:outputText value="Price"></h:outputText>
<h:inputText value="#{carBean.carTO.price}"></h:inputText></h:panelGrid><h:commandButton action="#{carBean.addCar}" value="Add Car"></h:commandButton><br/><h:outputText value="#{carBean.message}"></h:outputText><br/><h:outputLink value="Home.xhtml">Home</h:outputLink></center></h:form>
</f:view></h:body>
</html>

点击“ 添加汽车”按钮后,将调用CarBean (后备bean)的动作处理程序,并在后备Bean 中将CarTO (Car Transfer Object)发送到CarServiceinsertData函数,此处将CarTO值的值插入文档中并将文档添加到集合中。 并单击Home Home.xhtml。 以下清单显示了CarBean的代码。

CarBean.java

package mongo.db.bean;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import mongo.db.service.CarService;
import mongo.db.to.CarTO;
@ManagedBean
public class CarBean {private List<CarTO> list = new ArrayList<CarTO>();private String message;private CarTO carTO= new CarTO();/**Action handler to save the data in the collection*/public String addCar(){try {/**Inserting the data to the Service class to insert it intoMongo DB*/message= new CarService().insertData("testdb","MyNewJAVATableCollection3",carTO);/**Message property is shown on the Page to show successmessage on the page*/} catch (UnknownHostException e) {message= e.getMessage();}return "samePage";}/**Action handler to get the data from the collection*/public String getCarDetails(){try {/**Reading the data From the Service class which further readthe data from the  Mongo DB */list = new CarService().findData("testdb","MyNewJAVATableCollection3");if(list.size()==0){message="No records Exist";}} catch (UnknownHostException e) {message= e.getMessage();}return "success";}
/*Getters  and setters to be coded*/
}

服务类的代码如下:

CarService.java

package mongo.db.service;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import mongo.db.to.CarTO;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;public class CarService {/**Utility to Get the Connection from the database*/
public static DBCollection getConnection(String dbName, String collectionName)throws UnknownHostException {/** Connecting to MongoDB */MongoClient mongo = new MongoClient("localhost", 27017);/**Gets database, incase if the database is not existingMongoDB Creates it for you*/DB db = mongo.getDB(dbName);/**Gets collection / table from database specified ifcollection doesn't exists, MongoDB will create it foryou*/DBCollection table = db.getCollection(collectionName);return table;}/**function to insert the data to the database */
public  String insertData(String dbName, String collectionName, CarTO carTO) throws UnknownHostException {/**Connecting to MongoDB*/
DBCollection table =CarService.getConnection(dbName, collectionName);/**creating a document to store as key and value*/BasicDBObject document = new BasicDBObject();document.put("carName", carTO.getCarName());document.put("company", carTO.getCompany());document.put("model", carTO.getModel());document.put("cc", carTO.getCc());document.put("Price", carTO.getPrice());/** inserting to the document to collection or table*/table.insert(document);return "Car added successfully with the record number :"+table.count();
}/**function to get Details from the database*/
public  List<CarTO> findData(String dbName, String collectionName) throws UnknownHostException {/**Connecting to database*/
DBCollection table= CarService.getConnection(dbName, collectionName);/**getting results from the database*/
DBCursor cursor = table.find();
List<CarTO>list= new ArrayList<CarTO>();/**iterating over the documents got from the database*/
while (cursor.hasNext()) {DBObject obj= cursor.next();/**documentToMapUtility is coded to convert the document received from database to key value pairs and put it inside a map*/
Map map=CarService.documentToMapUtility(obj.toString());/**Map having values is iterated using entry set and CarTO is populated and CarTO is added in the List<CarTO> and this list returned*//**Getting the Entery Set from the map  */
Set<Entry<String,String>> set= map.entrySet();/**Getting the Iterator to iterate the entry Set*/
Iterator<Entry<String,String>> itr= set.iterator();CarTO carTO = new CarTO();/**loop to put ever Key value pair to CarTO object*/       while(itr.hasNext()){Entry<String, String> entry = itr.next();String key=entry.getKey();/**Removing the unwanted from the keys*/key = CarService.subStringUtility(key);String value=entry.getValue();if(key.equalsIgnoreCase("carName")){carTO.setCarName(value.substring(2,value.length()-2));}else if(key.equalsIgnoreCase("company")){carTO.setCompany(value.substring(2,value.length()-2));}else if(key.equalsIgnoreCase("model")){String date[]=value.split("-");int year=Integer.parseInt(date[0].substring(2));int month=Integer.parseInt(date[1]);int datemon=Integer.parseInt(date[2].substring(0, date.length-1));Calendar c= Calendar.getInstance();c.set(Calendar.YEAR, year);c.set(Calendar.MONTH, month);c.set(Calendar.DATE, datemon);carTO.setModel(c.getTime());}else if(key.equalsIgnoreCase("cc")){carTO.setCc(Integer.parseInt(value.trim()));}else if(key.equalsIgnoreCase("Price")){carTO.setPrice(Double.parseDouble(value.trim()));}}          /**inner While closed*/list.add(carTO);
}   /**while iterating over the cursor records closed here*/return list;
}/**Utility to remove un wanted contents*/public static String subStringUtility (String s){return s.substring(2,s.length()-2);}/**Utility to convert the document to map*/public static Map<String,String> documentToMapUtility (String s){s= s.substring(1,s.length()-1);String sArr[]= s.split(",");Map<String,String> map = new LinkedHashMap<String,String>();for(int i=1;i<sArr.length;i++){if(!sArr[i].contains("$date")){String keyValue[]= sArr[i].split(":");map.put(keyValue[0],keyValue[1]);System.out.println(keyValue[0]+","+keyValue[1]);}else{String keyValue[]= sArr[i].split(":");map.put(keyValue[0],keyValue[2]);}}return map;}}

以下清单显示了传输对象类CarTO的内容

CarTO.java

package mongo.db.to;
import java.util.Date;
public class CarTO {private String carName;private String company;private Integer cc;private Double price;private Date model;/*Getters and Setters to be coded*/
}

在单击主页上的“查看注册的汽车”链接时,因为它是命令链接,所以将执行CarBean的动作处理程序getCarDetails并在方法findData的帮助下从CarService类获取详细信息,请参阅动作处理程序getCarDetailsCarBean代码 下面的清单表示Report.xhtml的代码

Report.xhtml.java

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:f="http://java.sun.com/jsf/core"xmlns:h="http://java.sun.com/jsf/html"xmlns:j="http://java.sun.com/jsp/jstl/core">
<h:head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></h:head><h:body><f:view>
<h:form><center>
<h2>MSD Car Portal</h2>
<h3>Car Details</h3><h:dataTable value="#{carBean.list}" var="item" border="2" rendered="#{not empty carBean.list}"><h:column>
<f:facet name="header">
<h:outputText value="CarName"></h:outputText>
</f:facet>
<h:outputText value="#{item.carName}"></h:outputText>
</h:column><h:column>
<f:facet name="header">
<h:outputText value="Company"></h:outputText>
</f:facet>
<h:outputText value="#{item.company}"></h:outputText>
</h:column><h:column>
<f:facet name="header">
<h:outputText value="Model"></h:outputText>
</f:facet>
<h:outputText value="#{item.model.time}">
<f:convertDateTime pattern="dd-MMM-yyyy"></f:convertDateTime>
</h:outputText>
</h:column><h:column>
<f:facet name="header">
<h:outputText value="CC"></h:outputText>
</f:facet>
<h:outputText value="#{item.cc}"></h:outputText>
</h:column><h:column>
<f:facet name="header">
<h:outputText value="Price"></h:outputText>
</f:facet>
<h:outputText value="#{item.price}"></h:outputText>
</h:column></h:dataTable><br/><h:outputText value="#{carBean.message}"></h:outputText><br/><h:outputLink value="Home.xhtml">Home</h:outputLink>
</center></h:form>
</f:view></h:body>
</html>

结论

从给定的示例中可以很明显地看出,MongoDb可以与现有的Web框架集成,并且可以用于制作可以轻松处理大数据问题的Web应用程序。

参考:

  • http://www.rabidgremlin.com/data20/
  • http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
  • http://docs.oracle.com/javaee/6/tutorial/doc/bnaph.html

翻译自: https://www.javacodegeeks.com/2013/09/mongodb-and-web-applications.html

mongodb web

mongodb web_MongoDB和Web应用程序相关推荐

  1. mongodb安装_MongoDB和Web应用程序

    mongodb安装 当今时代是数据大规模增长的时代. 数据存储不是问题,是的,但是结构化和存储的方式可能会增加或减少所需数据块的查找时间. 不断增长的非结构化数据的用例 脸书: 7.5亿用户处于活跃状 ...

  2. 使用 Node.js、Express、AngularJS 和 MongoDB 构建一个Web程序

    为什么80%的码农都做不了架构师?>>>    使用 Node.js.Express.AngularJS 和 MongoDB 构建一个实时问卷调查应用程序 2014 年 3 月 20 ...

  3. MongoDB和Web应用程序

    当今时代是数据大规模增长的时代. 数据存储不是问题,是的,但是结构化和存储的方式可能会增加或减少所需数据块的查找时间. 不断增长的非结构化数据的用例 脸书: 7.5亿用户处于活跃状态,三分之一的互联网 ...

  4. aws mongodb_在AWS上托管React flask mongodb Web应用程序第4部分

    aws mongodb This is a 4 part blog series of articles. Here, you will learn the basic concepts of AWS ...

  5. ASP.NET Core开源Web应用程序框架ABP

    "作为面向服务架构(SOA)的一个变体,微服务是一种将应用程序分解成松散耦合服务的新型架构风格. 通过细粒度的服务和轻量级的协议,微服务提供了更多的模块化,使应用程序更容易理解,开发,测试, ...

  6. 操作方法:具有多个Mongo存储库和Kotlin的Spring Boot 2 Web应用程序

    首先,免责声明:如果您正在编写微服务 (每个人现在都对吗?)并希望它是惯用的 ,那么通常不会在其中使用几个不同的数据源. 图片取自Pixabay© https: //pixabay.com/illus ...

  7. 使用Spring Boot和MongoDB快速进行Web应用原型设计

    回到我以前的项目之一,我被要求制作一些应急申请. 时间表紧张,范围简单. 内部编码标准是PHP,因此尝试建立经典的Java EE堆栈将是一个真正的挑战. 而且,说实话,完全过大了. 那怎么办 我趁机尝 ...

  8. 使用Java和Spring构建现代Web应用程序

    使用Spring Framework创建Java Web应用程序从未如此简单. 如果您已经熟悉Java并且几乎没有创建Web应用程序的经验,或者如果您担心所有很酷的孩子都放弃Java取而代之的是Rub ...

  9. 通过制作数字桌面游戏和Web应用程序学习JavaScript

    Building 2D games can be a great way to learn JavaScript, especially when working through the basics ...

最新文章

  1. 乐高ev3搭建图_乐高EV3作品|机械夹子(二)
  2. Erlang里实现MapReduce
  3. 用dos命令给mysql数据库新建表
  4. 如何通过IP定位交换机
  5. 云盒子企业网盘入驻阿里云市场,正式向公有云市场发力!
  6. 根据IP地址查询其所属城市
  7. 【耿老师公开课】反转!物联网火爆,开发者却很难入门?
  8. IE浏览器各版本的CSS Hack
  9. Eclipse修改相同内容的高亮显示(pydev编辑python)
  10. 如何修复MySQL配置文件?
  11. iSecure Center 综合安防管理平台
  12. ​杭州,苏州,成都哪个最宜居?
  13. ES index not_analyzed
  14. opencv批量转换图片格式
  15. 解决 java.lang.RuntimeException: Method i in android.util.Log not mocked. See http://g.co/androidstudi
  16. 有特点的3D网络拓扑图
  17. 获取Word2vec训练得到的所有词与词向量
  18. 移动安全技术如何未雨绸缪?
  19. 【MFC】Ribbon界面开发(一)
  20. 《漫游》之《云依柳俏》

热门文章

  1. 面试-线程池的成长之路
  2. 在Swing和Swt中使用JavaFX
  3. 漫画:什么是SnowFlake算法
  4. java实现人脸识别源码【含测试效果图】——ServiceImpl层(UserServiceImpl)
  5. 最全三大框架整合(使用映射)——struts.xml和web.xml配置
  6. 希尔排序+移位法(吊打交换法)
  7. mysql---批量插入数据:100w条数据
  8. SparkSQL 内置函数的使用(JAVA与Scala版本)
  9. bs架构 erp 进销存_从应用架构看生鲜电商信息化建设
  10. window7连接其他计算机的打印机,win7系统电脑怎样连接其它电脑上在打印机?