MongoDB和Web应用程序

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

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



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

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

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

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

Mongo数据库和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.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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/367441.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【译】10个有趣的JSCSS库(2018.10)

Tutorialzine每月都会给我们精心挑选优秀的web开发资源&#xff0c;这些资源可以帮助我们解锁最新和最炫酷的网络开发姿势。前端er,让我们一起先睹为快吧&#xff01; WatermelonDB WatermelonDB是用于构建React和React Native应用程序的下一代数据库。快速&#xff0c;高度可…

自定义分页器

好久没来写东西那&#xff01;今天写个自定义分页器给大家参考下吧 首先我们在自己创建的Django项目的app下新建一个utils文件夹&#xff0c;用来放我们的分页器组件 简单说下分页器实现原理&#xff1a; 1.利用ORM查询语句的限制数据条数来确定每页显示的数据 2.设置我们每页显…

五 Vue学习 首页学习 (上)

首页&#xff1a; http://localhost:8002/#/&#xff0c; 登录页面如下&#xff1a; index.js文件中如下的路由配置&#xff0c;转过去看login.vue是如何实现的。 const routes [ { path: /, component: login },&#xff08;这里一个问题&#xff1a; logi…

linux下mqm添加用户,Linux 下MQ的安装和配置亲测

开篇之前奉上几条黄金链接&#xff1a;MQ参考文档http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0m0/index.jsp?topic%2Fcom.ibm.mq.doc%2Fhelp_home_wmq.htmhttp://www-01.ibm.com/support/docview.wss?uidswg27006467MQ下载地址&#xff1a;http://www-03.ibm.com/so…

Java核心面试问题

问&#xff1a;如果main方法被声明为私有该怎么办&#xff1f; 回答&#xff1a; 该程序可以正确编译&#xff0c;但在运行时会显示“ Main方法不公开”。 信息。 问&#xff1a;在Java中按引用传递和按值传递是什么意思&#xff1f; 回答&#xff1a; 通过引用方式传递&…

android 判断是否是标点符号_Java 中文字符判断 中文标点符号判断

Java Character 实现Unicode字符集介绍 CJK中文字符和中文标点判断主要内容&#xff1a;1. Java Character类介绍&#xff1b;2. Unicode 简介及 UnicodeBlock 与 UnicodeScript区别和联系3. 如何判断汉字及中文标点符号做中文信息处理&#xff0c;经常会遇到如何判断一个字是…

小程序tabbar这套方案全搞定!

关于微信小程序的tarbar&#xff0c;相信你们都不会陌生 在实现小程序微信原装的tabbar却比较呆板&#xff0c;不够精致&#xff0c;往往不符合自己的要求 这个时候怎么办呢 这套方案接着&#xff01; 先简单的来说一下主要思想:自定义字体图标组件以及tabbar组件&#xff0c…

linux服务器用哪个面板好,Linux服务器管理面板哪家比较好用?

Linux服务器管理面板哪家比较好用&#xff1f;发布时间&#xff1a;2020-07-21 06:08:33来源&#xff1a;51CTO阅读&#xff1a;261作者&#xff1a;BirdCloud_1022现在&#xff0c;越来越多的站长朋友都会选择服务器用来搭建网站&#xff0c;但是势必需要我们自己搭建WEB环境&…

spring boot(一)入门

目录 spring boot(一)入门一、简介1、微服务的概念2、什么是spring boot3、快速入门4.springboot的快捷部署spring boot(一)入门 一、简介 1、微服务的概念 说起spring boot&#xff0c;我们不得不说一下“微服务”一词的兴起&#xff0c;微服务一词最早起源于2014年&#xff0…

使用Couchbase分页

如果对Couchbase集群进行查询时必须处理大量文档&#xff0c;则使用分页来逐页获取行很重要。 您可以在“ 分页 ”一章的文档中找到一些信息&#xff0c;但是我想在本文中提供更多详细信息和示例代码。 在此示例中&#xff0c;我将基于啤酒样本数据集创建一个简单的视图&#…

android 常用机型尺寸_不同手机/设备和操作系统版本上的Android堆大小

素胚勾勒不出你不仅手机生产商&#xff0c;而且任何创建Android操作系统版本的人&#xff0c;都可以根据设备的特定需求&#xff0c;指定允许的最大堆大小。一些Android根&#xff0c;如CyanogenMod&#xff0c;甚至允许用户自己选择堆大小作为设置。可以使用该方法检测允许的最…

vue移动端项目缓存问题实践

最近在做一个vue移动端项目&#xff0c;被缓存问题搞得头都大了&#xff0c;积累了一些经验&#xff0c;特此记录总结下&#xff0c;权当是最近项目问题的一个回顾吧&#xff01; 先描述下问题场景&#xff1a;A页面->B页面->C页面。假设A页面是列表页面&#xff0c;B页…

pdf解析与结构化提取

PDF解析与结构化提取 PDF解析 对于PDF文档&#xff0c;我们选择用PDFMiner对其进行解析&#xff0c;得到文本。 PDFMiner PDFMiner使用了一种称作lazy parsing的策略&#xff0c;只在需要的时候才去解析&#xff0c;以减少时间和内存的使用。要解析PDF至少需要两个类&#xff1…

Linux usb bus日志如何打开,从linux usb bus节点来认识usb linux usb认识

首先从linux dmesg来认识usb:<6>[ 19.610046] msm_hsic_host msm_hsic_host: Qualcomm EHCI Host Controller using HSIC<6>[ 19.620391] msm_hsic_host msm_hsic_host: new USB bus registered, assigned bus number 1<6>[ 19.659942] msm_hsic_host …

Spring面试问题

还可以查看我们最新的文章69Spring面试问答-最终名单 。 1&#xff09;什么是春天&#xff1f; 回答&#xff1a; Spring是控件和面向方面的容器框架的轻量级转换。 2&#xff09;解释春天&#xff1f; 回答&#xff1a; 轻巧&#xff1a;在尺寸和透明度方面&#xff0c; S…

java 字符串转utc时间_JAVA 本地时间字符串转UTC时间字符串

本来想偷懒百度一个时间字符串转UTC的代码&#xff0c;但发现没有一个能用&#xff0c;写得还复杂得要死&#xff0c;没办法还是自己撸一个。/*** UTC时间字符串转本地时间字符串* 我的本地getDateTimeInstance()是格式&#xff1a;yyyy-MM-dd HH:mm:ss* param str UTC时间字符…

前端解读面向切面编程(AOP)

前言 面向对象(OOP)作为经典的设计范式&#xff0c;对于我们来说可谓无人不知&#xff0c;还记得我们入行起始时那句经典的总结吗-万事万物皆对象。 是的&#xff0c;基于OOP思想封装、继承、多态的特点&#xff0c;我们会自然而然的遵循模块化、组件化的思维来设计开发应用&a…

windows和linux允许分片,请问hadoop的hdfs文件系统和本地windows文件系统或linux文件系统是什么关系啊,谢谢...

虚拟文件系统 Virtual File Systems(VFS)Linux 是近年来发展起来的一种新型的操作系统&#xff0c;其最重要的特征之一就是支持多种文件系统&#xff0c;使其更加灵活&#xff0c;从而与许多其它的操作系统共存。Linux支持ext&#xff0c;ext2&#xff0c;xia&#xff0c;minix…

201771010120 苏浪浪 《面向对象程序设计(java)》第二周学习总结

理论知识总结 第三章Java基本程序设计结构 1、基本知识&#xff1a;&#xff08;1&#xff09;标识符&#xff1a;是由字母、下划线、美元符号和数字组成&#xff0c;且第一个符号不能为数字。&#xff08;2&#xff09;关键字&#xff1a;剧啊语言中被赋予特定意义的一些单词。…

Apache Camel简介

Apache Camel是著名的企业集成模式的开源实现。 Camel是一个路由和中介引擎&#xff0c;可以帮助开发人员以各种领域特定语言&#xff08;DSL&#xff09;&#xff08;例如Java&#xff0c;Spring / XML&#xff0c;scala等&#xff09;创建路由和中介规则。 骆驼用途广泛 Cam…