mongodb web_MongoDB和Web应用程序

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

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

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

相关文章

win7亮度怎么调_揭秘极米NEW Z8X投影仪怎么样?千万不要上当?!!!!【揭秘反馈

反馈测评极米NEW Z8X投影仪怎么样?求真实点评注意事项极米NEW Z8X投影仪怎么样?靠谱真实回答 外形外观&#xff1a;简约时尚大气&#xff0c;手感不错&#xff01;\n投影亮度&#xff1a;1080P、4K&#xff0c;都能做到&#xff01;\n投影色彩&#xff1a;非常好&#xff0c;…

php的完整代码块,简单测试了一下php中的代码块、内部类等知识

简单测试了一下php中的代码块、内部类等知识<?php class a{public $b;public function print_result(){//普通代码块&#xff0c;但已经不是java中的代码块{$c变量;echo 普通代码块&#xff1f;;}echo $c;}//错误的语法&#xff0c;Parse error: syntax error, unexpected …

Paw 百度ai_直面落地!百度EasyDL产业智能创新大赛成果覆盖能源、交通、水利民生重业...

物体检测模型实现高压线路隐患检测、图像分类实现短视频快速剪辑和量产、文本情感分类辅助潜在心理疾病患者自发检测、图片识别车辆轮轴数监管车辆载重……每一个创想都能简单快速实现&#xff0c;没有AI开发基础的小伙伴们也能做到&#xff01;这一切都缘于百度零门槛AI开发平…

Java 9、10及更高版本:Java平台的未来

您紧跟Java平台新功能的秘密武器 自去年9月发布Java 9以来&#xff0c;感觉整个平台都经历了重大变化。 在我们甚至无法确定Java 9所能提供的一切之前&#xff0c;我们已经在标记Java 10的发布。现在&#xff0c;我们已经开始期待Java 11在2018年9月发布。 Oracle决定为Java平…

php同时抢购 代码,浅谈PHP实现大流量下抢购方案

要求要有小时分钟秒的实时倒计时的显示&#xff0c;用户端修改日期时间不会影响到倒计时的正常显示(也就是以服务器时间为准)。其实这和很多的考试等系统的时间限制功能同样的要求。总不能用ajax每秒都获取服务器时间吧&#xff0c;所以实时倒计时一定要用javascript实现。这很…

c fread 快读 详解_奔驰ACC(自适应巡航系统)详解

什么是ACC自适应巡航&#xff1f;ACC自适应巡航( Adaptive Cruise Control )&#xff0c;又可称为智能巡航控制系统&#xff0c;简称ACC系统&#xff0c;它是在传统巡航控制基础上发展起来的新一代汽车驾驶员辅助驾驶系统。它将汽车自动巡航控制系统CCS 和车辆前向撞击报警系统…

php使用邮件找回密码,php利用Zend_Mail发送邮件(实现邮件重设密码功能)

[php]代码库<?php include_once conn/conn.php;require_once Zend/Mail.php;//调用发送邮件的文件require_once Zend/Mail/Transport/Smtp.php;//调用SMTP验证文件$reback 0;$name $_GET[foundname];$question $_GET[question];$answer $_GET[answer];$sql "sele…

_Linux 最常用命令整理,建议收藏!

Linux是目前应用最广泛的服务器操作系统&#xff0c;基于Unix&#xff0c;开源免费&#xff0c;由于系统的稳定性和安全性&#xff0c;市场占有率很高&#xff0c;几乎成为程序代码运行的最佳系统环境。linux不仅可以长时间的运行我们编写的程序代码&#xff0c;还可以安装在各…

openjdk 使用_如何在OpenJDK中使用ECC

openjdk 使用曾经试图在Java和OpenJDK中使用椭圆曲线密码术 &#xff08;ECC&#xff09;的每个人要么被迫使用Bouncy Castle&#xff0c;要么被SunEC提供者弄糊涂了 。 SunEC提供程序根据文档 &#xff08;报价&#xff09;提供以下算法&#xff1a; AlgorithmParameters 欧…

bpcs uploader.php,linux 备份定时同步到百度云盘

导读&#xff1a;现在的百度云盘免费容量都是2T了&#xff0c;即便把电脑上全部的东东都放上去&#xff0c;也还有大把的剩余空间。对于站长来讲&#xff0c;是彻底能够充分利用这些硬盘空间的&#xff0c;如今咱们就用百度云盘来备份Linux服务器上的数据。php一直在想&#xf…

hadoop可以解决什么问题_快速解决皮带机轴磨损问题可以这样做

皮带机是皮带输送机的简称&#xff0c;皮带机运用输送带的连续或间歇运动来输送各种轻重不同的物品&#xff0c;既可输送各种散料&#xff0c;也可输送各种纸箱、包装袋等单件重量不大的件货&#xff0c;用途广泛。皮带机运行时轴磨损是一个很常见的设备问题&#xff0c;某企业…

matlab 从 excel读取 日期_毕业季:计量经济学实证研究中,哪款软件好(SPSS,Eviews,Matlab,stata,SAS)...

毕业季&#xff1a;计量经济学实证研究中&#xff0c;哪款软件好&#xff1f;&#xff08;SPSS&#xff0c;Eviews&#xff0c;Matlab&#xff0c;stata&#xff0c;SAS&#xff09;对于这个问题&#xff0c;我也深深的迷茫过。用了包括但不限于Excel、SPSS、SPSS modeler、Evi…

php 猴子选大王,php猴子选大王

法一&#xff1a;function monkeyKing($n,$m){$arrrange(1,$n);$i0;while (count($arr)>1){for($i1;$i<$m-1;$i){array_push($arr, array_shift($arr));}array_shift($arr);}echo "$arr[0]";}monkeyKing(6,4);//5法二&#xff1a;function king($m ,$n){//构造…

java 队列和堆栈_Java中的堆栈和队列

java 队列和堆栈我最近一直在研究一些需要堆栈和队列的Java代码。 使用的选择不是立即显而易见的。 有一个Queue接口&#xff0c;但没有明确的具体实现要使用。 还有一个Stack类&#xff0c;但是javadocs指出其他类“应该优先于此类使用”。 那么&#xff0c;您对Java中的堆栈和…

dbassit 包_CELINE新包,篮子包、圆盒包、腋下包、托特包等

CELINE 2021春夏女装系列以纪录片形式发布&#xff0c;由创意总监 Hedi Slimane 执导并设计配乐&#xff0c;在摩纳哥路易二世体育场取景。CELINE 2021春夏时装秀本季作品以「一代人的肖像」为题&#xff0c;在复古优雅中融入富有街头感的运动元素。CELINE 2021春夏时装秀女孩们…

Php数组面包屑导航,php可应用于面包屑导航的迭代寻找家谱树实现方法

php是通过定义类来实现迭代器接口来构造迭代器&#xff0c;通过yield构造迭代器可以提高性能并节省系统开销&#xff0c;下面就跟着爱站技术频道小编的步伐来学习php可应用于面包屑导航的迭代寻找家谱树实现方法吧。具体实现方法如下&#xff1a;echo "";$area arra…

bootstrap 悬浮固定_CST Tech Tips - 流式细胞术中如何固定和通透细胞?

CST TECH TIPS 系列课程 欢迎关注「CST博士互助平台」 有关流式细胞术(Flow Cytometry&#xff0c;FCM)实验步骤中的固定和通透化&#xff0c;你需要了解什么&#xff1f;进行流式细胞术&#xff0c;如果你所有的靶标均在外表面表达&#xff0c;那么可使用活细胞。但当你靶向胞…

php按钮css样式,CSS 按钮

CSS 按钮本章节我们为大家介绍使用 CSS 来制作按钮。我们先看一下默认按钮和用css制作的按钮html>php中文网(php.cn).button {background-color: #4CAF50;border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;fo…

电脑网络维护_电脑维护小技巧(全面)

如果你真的想了解&#xff0c;请耐心看完&#xff0c;都是干货电脑使用维护小技巧1、电脑为何莫名奇妙多了那么多软件呢&#xff1f; 有的人在使用电脑时候会经常发现&#xff0c;电脑无缘无故会冒出很多不知道的软件&#xff0c;“没见过、不是我下载的、卸载不掉”&#xff0…

自动装箱自动拆箱java,自动装箱?拆箱?==问题?详解java面试常见的一个问题...

1&#xff1a;前言相信大家都在面试中都被问到过一个问题&#xff0c;这个问题也是近年来面试官刁难人比较常见的一个问题&#xff0c;所以也被大家所熟知了&#xff0c;本质上也很简单&#xff0c;但是也是非常基础的一个题目。Integer a 100;Integer b 100;System.out.prin…