﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-云-随笔分类-翻译</title><link>http://www.blogjava.net/matuobasyouca/category/51271.html</link><description>敏捷、分布式、ALM过程自动化、企业应用架构</description><language>zh-cn</language><lastBuildDate>Wed, 09 May 2012 21:10:47 GMT</lastBuildDate><pubDate>Wed, 09 May 2012 21:10:47 GMT</pubDate><ttl>60</ttl><item><title>MongoDB, Java 与对象关系映射</title><link>http://www.blogjava.net/matuobasyouca/archive/2012/05/09/377698.html</link><dc:creator>一酌散千忧</dc:creator><author>一酌散千忧</author><pubDate>Wed, 09 May 2012 05:46:00 GMT</pubDate><guid>http://www.blogjava.net/matuobasyouca/archive/2012/05/09/377698.html</guid><wfw:comment>http://www.blogjava.net/matuobasyouca/comments/377698.html</wfw:comment><comments>http://www.blogjava.net/matuobasyouca/archive/2012/05/09/377698.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/matuobasyouca/comments/commentRss/377698.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/matuobasyouca/services/trackbacks/377698.html</trackback:ping><description><![CDATA[<div>  <h2><span style="font-size:14.5pt;font-family:Arial;color:black;letter-spacing:-.75pt">MongoDB</span><span><span style="font-size:14.5pt;color:black;letter-spacing:-.75pt">介绍</span></span><span></span></h2>  <p><span>当今<span>NoSQL领域中有很多有力的竞争者通过多种方式来处理海量数据问题。其中重要的解决方案之一就是MongoDB。MongoDB是面向文档的弱结构化存储方案，使用JSON格式来展现、查询和修改数据。</span></span></p>  <p><span><span>MongoDB文档相当完备，扩展规模与安装一样简单。它提供冗余、切片、索引以及map/reduce等概念支持。MongoDB的开源社区非常大且非常活跃。MongoDB在很多大型产品中被实际运用，如：Disney, Craigslist, Foursquare, Github 和SourceForge。MongoDB是一个开源项目，由</span></span><a href="http://www.10gen.com/"><span><span style="color:#0B59B2">10gen.com</span></span></a><span>建立并维护，该公司由<span>DoubleClick的前任执行人员创立。同时，10gen也提供了极好的商业支持与参与建设。</span></span></p>  <h2><span><span><span style="font-size:14.5pt; font-family:Arial;color:black;letter-spacing:-.75pt">MongoDB </span></span></span><span><span style="font-size:14.5pt;color:black;letter-spacing:-.75pt">与</span></span><span><span><span style="font-size:14.5pt;font-family:Arial;color:black;letter-spacing: -.75pt"> NoSQL: </span></span></span><span><span><span style="font-size:14.5pt;color:black; letter-spacing:-.75pt">缺陷与优势</span></span></span><span></span></h2>  <p><span><span>MongoDB作为一个可用NoSQL方案具有很多优势。我刚开始接触NoSQL数据库了解了一系列基于Java的方案，并且花了大量的时间来弄懂什么是列家族，Hadoop与HBase的关系，ZooKeeper到底是什么。当我终于全部清楚之后，发现Cassandra与HBase确实是对于NoSQL领域非常可靠、可信赖的解决方案。但与其他的解决方案相比，MongoDB让我在能够开始写代码之前，不用理解那么多的概念。</span></span></p>  <p><span>与其他软件相似，<span>MongoDB也存在缺陷。经过一段时间使用MongoDB，我列举经历过并需要注意的一些事情，我成为&#8220;Gotchas&#8221;：</span></span></p>  <ul type="disc"><li style="      text-align:left;"><span><span style="font-family:宋体;">不要按照关系型数据库来思考。这很明显，MongoDB</span></span><span><span style="font-family:宋体;">使得构建和执行复杂查询变得非常容易。当实际使用的时候，你会主要关注于效率问题（像我一样）。</span></span></li><li style="      text-align:left;"><span><span>MongoDB</span></span><span><span style="font-family:宋体;">的索引是二进制的树。如果你不是很熟悉B-tree</span></span><span><span style="font-family:宋体;">，可能需要了解一下。这些都涉及到构建符合提供查询条件需求的建立索引的方式。</span></span></li><li style="      text-align:left;"><span><span style="font-family:宋体;">小心的设计索引结构。这涉及到上面提到的B-tree</span></span><span><span style="font-family:宋体;">。刚开始我的索引包含文档中的很多字段，以防我会使用到他们。不要犯同样的错误。我有一个很小集合的索引（大约1</span></span><span><span style="font-family:宋体;">千万记录）增长到超过17GB</span></span><span><span style="font-family:宋体;">的空间，比集合本身还大。你应该不会想要索引一个包含成百上千个实体的列表字段。</span></span></li><li style="      text-align:left;"><span><span>MongoDB</span></span><span><span style="font-family:宋体;">采用了非常有意思的方式来实现NoSQL</span></span><span><span style="font-family:宋体;">：采用BSON</span></span><span><span style="font-family:宋体;">作为存储，JSON</span></span><span><span style="font-family:宋体;">作为展示，JavaScript</span></span><span><span style="font-family:宋体;">用于管理和Map/Reduce</span></span><span><span style="font-family:宋体;">。因此也引起了一些小问题比如</span></span><a href="https://jira.mongodb.org/browse/SERVER-1672"><span><span><span style="font-family:      宋体;color:#0B59B2">这个</span></span></span></a><span> </span><span><span style="font-family:宋体;">（破坏了Number</span></span><span><span style="font-family:宋体;">和Long</span></span><span><span style="font-family:宋体;">的相等操作），在MongoDB</span></span><span><span style="font-family:宋体;">逐渐流行之后，可能会不断的展示出来。</span></span></li></ul>  <p><span>&nbsp;</span></p>  <h2><span><span><span style="font-size:14.5pt; font-family:Arial;color:black;letter-spacing:-.75pt">MongoDB, </span></span></span><span><span style="font-size:14.5pt;color:black;letter-spacing:-.75pt">命令行与驱动</span></span><span></span></h2>  <p><span><span>MongoDB基本是使用JavaScript客户端命令行程序来进行复杂任务管理的，如数据整合和简单信息处理，编程都是完全使用JavaScript语言来的。本文中，我们会展示命令行的使用示例。现在有大量的MongoDB客户端产品提供，并且由MongoDB社区来支持驱动。通常每种编程语言都有驱动，并且所有流行的语言都有包括，一些不那么流行的也包含在内。这篇文章展示了使用MongoDB的Java驱动，并使用一个ORM库（MJORM）与之进行比较。</span></span></p>  <h2><span><span><span style="font-size:14.5pt;color:black; letter-spacing:-.75pt">介绍</span></span></span><span><span><span style="font-size:14.5pt; font-family:Arial;color:black;letter-spacing:-.75pt"> MJORM: MongoDB</span></span></span><span><span style="font-size:14.5pt;color:black;letter-spacing:-.75pt">的</span></span><span><span><span style="font-size:14.5pt;font-family:Arial;color:black;letter-spacing: -.75pt">ORM</span></span></span><span><span><span style="font-size:14.5pt;color:black; letter-spacing:-.75pt">方案</span></span></span><span></span></h2>  <p><span>在解决的众多有意思的问题中，最近<span>NoSQL数据存储在开发者中主要的问题趋势就是对象关系映射。对象关系映射就是将传统中保存在关系型数据库中的持久化数据映射为在应用程序中使用的对象。这使得编程语言使用起来更加流畅和自然。</span></span></p>  <p><span><span>MongoDB面向文档的架构使得它非常适合对象关系映射，因为文档本身就是以对象形式存储的。可惜没有太多的MongoDB的Java对象关系映射库，但是还是有一些，如</span></span><a href="http://code.google.com/p/morphia/"><span><span><span style="color:#0B59B2">morphia-(A type-safe Java library for MongoDB)</span></span></span></a><span>，<span>&nbsp;</span></span><a href="http://www.springsource.org/spring-data/mongodb"><span><span style="color:#0B59B2">spring-data</span></span></a><span><span>(SpringData项目的MongoDB实现)</span></span><span></span></p>  <p><span>这些<span>ORM库大量使用了注解，因为一些原因对我不适合，其中最重要的就是这些被注解的对象在多个项目中的兼容性问题。这让我开始了mongo-Java-orm 或者 "MJORM" (发音 me-yorm)项目，一个MongoDB的Java对象关系映射项目。MJORM是在MIT许可之下，并且在发布在了</span></span><a href="http://code.google.com/p/mongo-Java-orm/"><span><span><span style="color:#0B59B2">google code project</span></span></span></a><span>。项目采用<span>maven构建，并且maven构件仓库托管于google code版本控制服务器。MJORM的最新可用发布版本为0.15，已经由一些项目使用与生产环境中。</span></span></p>  <h2><span><span><span style="font-size:14.5pt;color:black; letter-spacing:-.75pt">开始使用</span></span></span><span><span><span style="font-size:14.5pt; font-family:Arial;color:black;letter-spacing:-.75pt">ORM</span></span></span></h2>  <h3><span><span><span style="font-size:11.5pt;line-height:173%;font-family:宋体;letter-spacing: -.75pt">加入</span></span></span><span><span><span style="font-size:11.5pt; line-height:173%;font-family:Arial;letter-spacing:-.75pt">MJORM </span></span></span><span><span><span style="font-size:11.5pt;line-height:173%;font-family:宋体;letter-spacing: -.75pt">库</span></span></span><span></span></h3>  <p><span><span>Maven的使用者首先应当在pom.xml中加入MJORM的maven仓库，使得MJORM构件可用。</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;repository&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;id&gt;mjorm-webdav-maven-repo&lt;/id&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;name&gt;mjorm maven repository&lt;/name&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;url&gt;http://mongo-Java-orm.googlecode.com/svn/maven/repo/&lt;/url&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;layout&gt;default&lt;/layout&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;/repository&gt;</span></span></span></pre></div>  <p><span>然后加入依赖<span>:</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;dependency&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;groupId&gt;com.googlecode&lt;/groupId&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;artifactId&gt;mongo-Java-orm&lt;/artifactId&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;version&gt;0.15&lt;/version&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;/dependency&gt;</span></span></span></pre></div>  <p><span>这样就可以在应用中引入<span>MJORM代码。假如没有使用maven，则你需要手动下载MJORM的pom.xml中列举的所有依赖。</span></span></p>  <h3><span><span><span style="font-size:11.5pt;line-height:173%;font-family:宋体;letter-spacing: -.75pt">建立</span></span></span><span><span><span style="font-size:11.5pt; line-height:173%;font-family:Arial;letter-spacing:-.75pt"> POJOs</span></span></span></h3>  <p><span>依赖已经导入，可以开始编码了。我们从<span>POJO开始:</span></span><span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">class Author {</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private String firstName;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private String lastName;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ... setters and getters ...</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">}</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">class Book {</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private String id;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private String isbn;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private String title;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private String description;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private Author author;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ... setters and getters ...</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">}</span></span></span></pre></div>  <p><span>我们在这个对象模型中的描述是，作者有<span>ID、姓和名，书有ID、ISNB、标题、描述和作者。</span></span></p>  <p><span>你可能注意到书的<span>id属性是一个字符串，这是为了适应MongoDB的对象ID类型。MongoDB的ID是一个12字节的二进制值显示为一个十六进制的字符串。MongoDB要求集合中的每个文档都必须有一个唯一id，但不要求一定要是ObjectId。目前MJORM只支持ObjectId，并且显示为字符串。</span></span></p>  <p><span>你也可能注意到了<span>Author没有id字段。这是因为Book是它的父文档，因此不需要有id。记住，MongoDB只要求集合中的文档在根级别的id。</span></span></p>  <h3><span><span><span style="font-size:11.5pt;line-height:173%;font-family:宋体;letter-spacing: -.75pt">创建</span></span></span><span><span><span style="font-size:11.5pt; line-height:173%;font-family:Arial;letter-spacing:-.75pt">XML</span></span></span><span><span><span style="font-size:11.5pt;line-height:173%;font-family:宋体;letter-spacing: -.75pt">映射文件</span></span></span><span></span></h3>  <p><span>下一个步骤就是建立<span>XML映射文件，MJORM能够将MongoDB文档转换为对象。我们为每个文档创建一个对象作为示范，无论将所有的映射放在一个XML文件中还是分开都是可以的。</span></span></p>  <p><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Author.mjorm.xml</span></code>:</span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;?xml version="1.0"?&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;descriptors&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;object class="Author"&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="firstName" /&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="lastName" /&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/object&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;/descriptors&gt;</span></span></span></pre></div>  <p><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Book.mjorm.xml</span></code>:</span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;?xml version="1.0"?&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;descriptors&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;object class="Book"&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="id" id="true" auto="true" /&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="isbn" /&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="title" /&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="description" /&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="author" /&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/object&gt;</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&lt;/descriptors&gt;</span></span></span></pre></div>  <p><span>&nbsp;</span></p>  <p><span>这些映射文件能够很好的自解释。</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">descriptors</span></code>&nbsp;元素是根元素，必须包含在每个映射文件中。在它下面是</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">object</span></code>元素定义了文档与之对应的类。</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Object</span></code></span><span><code><span style="font-size:9.0pt;Courier New&quot;;Courier New&quot;;Courier New&quot;">包含的</span></code></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">property</span></code>&nbsp;元素主要用于描述<span>POJO中的属性以及这些属性如何与MongoDB中的文档想对应。</span></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">property</span></code>&nbsp;元素至少必须包含一个</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">name</span></code>&nbsp;属性，这个元素就是POJO和MongoDB的文档中的属性名称。</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">column</span></code>&nbsp;属性则是可选的，用于特定一个在MongoDB文档中的可选属性名称。</span></p>  <p><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">property</span></code>&nbsp;元素当中的<span>id属性应该是对象的唯一识别。一个对象只能有一个</span></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">property</span></code>&nbsp;元素包含<span>id属性。</span></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">auto</span></code>&nbsp;的设置会使得MJORM在持久化时为该属性自动生成一个值。</span></p>  <p><span>可以在<span>google code的MJORM项目主页中查看XML映射文件的更多细节描述。</span></span></p>  <h3><span><span><span style="font-size:11.5pt;line-height:173%;font-family:宋体;letter-spacing: -.75pt">整合</span></span></span><span><span><span style="font-size:11.5pt; line-height:173%;font-family:Arial;letter-spacing:-.75pt">POJO</span></span></span><span><span><span style="font-size:11.5pt;line-height:173%;font-family:宋体;letter-spacing: -.75pt">与</span></span></span><span><span><span style="font-size:11.5pt; line-height:173%;font-family:Arial;letter-spacing:-.75pt">XML</span></span></span></h3>  <p><span>我们创建了数据模型以及映射文件，使得<span>MJORM可以从MongoDB序列号以及反序列号POJO。我们可以进行一些有意思的事情了，首先打开MongoDB的链接：</span></span><span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">Mongo mongo = new Mongo(</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new MongoURI("mongodb://localhost/mjormIsFun")); // 10gen driver</span></span></span></pre></div>  <p><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Mongo</span></code>&nbsp;对象是由10gen编写的Java驱动提供的。示例中连接了一个本地的MongoDB实例中的mjormIsFun数据库。接下来我们创建MJORM </span><span><span><code><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;">ObjectMapper </span></code>。目前</span></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">ObjectMapper </span></code>在MJORM中的唯一实现就是</span><span><span><code><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;">XmlDescriptorObjectMapper</span></code>，使用XML结构描述信息。可能之后会增加对注解或其他结构定义的支持。</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">XmlDescriptorObjectMapper objectMapper = new XmlDescriptorObjectMapper();</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">mapper.addXmlObjectDescriptor(new File("Book.mjorm.xml"));</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">mapper.addXmlObjectDescriptor(new File("Author.mjorm.xml"));</span></span></span></pre></div>  <p><span>建立好了</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">XmlDescriptorObjectMapper</span></code>&nbsp;并且加入了映射文件。接下来建立由MJORM提供的</span><span><span><code><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;">MongoDao</span></code>&nbsp;对象的实例。</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">DB db = mongo.getDB("mjormIsFun"); // 10gen driver</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">MongoDao dao = new MongoDaoImpl(db, objectMapper);</span></span></span></pre></div>  <p><span>首先我们要获得<span>10gen驱动提供的DB对象实例。然后使用DB和</span></span><span><span><code><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;">ObjectMapper</span></code>&nbsp;建立</span></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">MongoDao</span></code>&nbsp;。我们准备开始持久化数据，建立一个</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Book</span></code>&nbsp;然后保存到MongoDB中。</span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">Book book = new Book();</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setIsbn("1594743061");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setTitle("MongoDB is fun");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setDescription("...");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book = dao.createObject("books", book);</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">System.out.println(book.getId()); // 4f96309f762dd76ece5a9595</span></span></span></pre></div>  <p><span>首先建立</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Book</span></code>&nbsp;对象并且填值，然后调用</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">MongoDao</span></code>&nbsp;的</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;"> createObject</span></code>&nbsp;方法，将</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Book</span></code>&nbsp;对象传入"</span><span><span><code><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;">books</span></code>" 的集合中。MJORM会按照之前的xml映射文件将</span></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">Book</span></code>&nbsp;转换为</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">DBObject</span></code>&nbsp;(这是10gen的Java驱动使用的基本类型)，并保存一个新的文档进"</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">books</span></code>" 集合。MJORM返回Book对象时，id属性会被填充。请注意，MongoDB默认是不需要在使用前建立数据库或集合的，系统会在需要时自动创建，这可能会造成某些困扰。在MongoDB的命令行中查看Book对象大概如下：</span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&gt; db.books.find({_id:ObjectId("4f96309f762dd76ece5a9595")}).pretty()</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">{</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "_id":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ObjectId("4f96309f762dd76ece5a9595"),</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "isbn":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "1594743061",</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "title":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "MongoDB is fun",</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "description":&nbsp;"..."</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">}</span></span></span></pre></div>  <p><span>&nbsp;</span></p>  <p><span>我们来看看假如不用<span>MJORM而直接使用10gen的Java驱动，如何使用</span></span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">createObject</span></code>&nbsp;方法：</span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">Book book = new Book();</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setIsbn("1594743061");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setTitle("MongoDB is fun");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setDescription("...");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">DBObject bookObj = BasicDBObjectBuilder.start()</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("isbn", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; book.getIsbn())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("title",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; book.getTitle())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("description",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; book.getDescription())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .get();</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">// 'db' is our DB object from earlier</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">DBCollection col = db.getCollection("books");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">col.insert(bookObj);</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">ObjectId id = ObjectId.class.cast(bookObj.get("_id"));</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">System.out.println(id.toStringMongod()); // 4f96309f762dd76ece5a9595</span></span></span></pre></div>  <p><span>&nbsp;</span></p>  <p><span>下面进行对象的查询<span>:</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">Book book = dao.readObject("books", "4f96309f762dd76ece5a9595", Book.class);</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">System.out.println(book.getTitle()); // "MongoDB is fun"</span></span></span></pre></div>  <p><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">readObject</span></code>&nbsp;方法根据给定文档的id从指定的集合中读取文档，转换为对象（再次使用映射文件）并返回。</span></p>  <p><span>敏锐的读者会注意到<span>Book还没有指定Author，仍然保存了。这归咎于MongoDB的结构不敏感的特性。我们不能要求集合中的文档包含所有属性（id属性是必须的），所有在MongoDB中没有Author的Book是可以的。我们现在为Book添加一个Author并且更新一下：</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">Author author = new Author();</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">author.setFirstName("Brian");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">author.setLastName("Dilley");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setAuthor(author);</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">dao.updateObject("books", "4f96309f762dd76ece5a9595", book);</span></span></span></pre></div>  <p><span>现在<span>Book就包含了Author，并且在MongoDB中持久化了。现在在命令行查看了Book：</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&gt; db.books.find({_id:ObjectId("4f96309f762dd76ece5a9595")}).pretty()</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">{</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "_id":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ObjectId("4f96309f762dd76ece5a9595"),</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "isbn":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "1594743061",</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "title":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "MongoDB is fun",</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "description":&nbsp;"..."</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "author": {</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "firstName": "Brian",</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; "lastName": "Dilley"</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">}</span></span></span></pre></div>  <p><span>可以看到持久化的<span>Book中已经包含了author。不使用MJORM来操作一遍：</span></span></p>  <div style="border:solid #EFEFEF 1.5pt;padding: 0cm 0cm 4.0pt 0cm;background:#FAFAFA"><pre style="line-height:11.25pt; background:#FAFAFA;border:none;padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">Author author = new Author();</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">author.setFirstName("Brian");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">author.setLastName("Dilley");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">book.setAuthor(author);</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">DBObject bookObj = BasicDBObjectBuilder.start()</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("isbn", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; book.getIsbn())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("title",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; book.getTitle())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("description",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; book.getDescription())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .push("author")</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("firstName", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; author.getFirstName())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add("lastName",  author.getLastName())</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .pop()</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .get();</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">DBCollection col = db.getCollection("books");</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span><span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#222222">col.update(new BasicDBObject("_id", bookObj.get("_id")), bookObj);</span></span></span></pre><pre style="line-height:11.25pt;background:#FAFAFA;border:none; padding:0cm;"><span>&nbsp;</span></pre></div>  <p><span>&nbsp;</span></p>  <p><span>对于</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">MongoDao</span></code>&nbsp;方法的深入讨论已经超出了本文的范围。对于将MJORM有兴趣用于实际项目中的用户强烈建议了解一下MJORM项目提供的相关文档，或者</span><span><code><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;">MongoDao</span></code>&nbsp;接口提供的相关用法。</span></p>  <h2><span><span><span style="font-size:14.5pt;color:black; letter-spacing:-.75pt">总结</span></span></span><span></span></h2>  <p><span>希望这篇文章对<span>MongoDB和MJORM的亮点有所展示。MongDB是一个优秀的呃NoSQL数据存储，有着大量优秀的特性，会是NoSQL市场中长期竞争者。若你会在一个Java项目中使用MongoDB，希望你也能够考虑使用MJORM作为你的ORM框架。十分欢迎大家提交特性需求、错误异常报告、文档和源码修正。</span></span></p>  <p style="line-height:12.0pt;background:white"><span>&nbsp;</span></p>  <h2><span><span style="font-size:14.5pt;color:black;letter-spacing:-.75pt">作者</span></span><span><span><span style="font-size:14.5pt;font-family:Arial;color:black;letter-spacing: -.75pt"> Bio</span></span></span></h2>  <p style="line-height:12.0pt;background:white"><span><span><strong><span style="font-size:10.0pt; font-family:Tahoma;color:black">Brian Dilley</span></strong></span></span><span><span>&nbsp;</span></span><span><span><span style="font-size:10.0pt;color:black">是一个经验丰富的高级工程师以及项目领导，在</span></span></span><span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">Java/Java EE /Spring Framework/Linux</span></span></span><span><span style="font-size:10.0pt;color:black">内部结构理解和管理有着超过</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">13</span></span><span><span style="font-size:10.0pt;color:black">年的经验。</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">Brian</span></span><span><span style="font-size:10.0pt;color:black">对于创业公司有很多经验，推向市场，构建</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">/</span></span><span><span style="font-size:10.0pt;color:black">维护产品等。他是</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">Iaas</span></span><span><span style="font-size:10.0pt;color:black">、</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">cloud</span></span><span><span style="font-size:10.0pt;color:black">、</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">PHP</span></span><span><span style="font-size:10.0pt;color:black">和</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">Linux</span></span><span><span style="font-size:10.0pt;color:black">的专家，熟悉产品的采购、安装及配置定义，以及公司的软硬件架构包括负载均衡、数据库、微博等。可以</span></span><span><span style="font-size:10.0pt;font-family:Tahoma;color:black">follow Brian</span></span><span><span style="font-size:10.0pt;color:black">的</span></span><span><span>&nbsp;</span></span><a href="https://twitter.com/#%21/dilleybrian"><span><span><span style="font-size:10.0pt; font-family:Tahoma;color:#0B59B2">Twitter</span></span></span></a><span> </span><span><span style="font-size:10.0pt;color:black">。</span></span><span></span></p>  </div><img src ="http://www.blogjava.net/matuobasyouca/aggbug/377698.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/matuobasyouca/" target="_blank">一酌散千忧</a> 2012-05-09 13:46 <a href="http://www.blogjava.net/matuobasyouca/archive/2012/05/09/377698.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据集成的演化：从EII到Big Data</title><link>http://www.blogjava.net/matuobasyouca/archive/2012/04/19/375213.html</link><dc:creator>一酌散千忧</dc:creator><author>一酌散千忧</author><pubDate>Wed, 18 Apr 2012 23:15:00 GMT</pubDate><guid>http://www.blogjava.net/matuobasyouca/archive/2012/04/19/375213.html</guid><wfw:comment>http://www.blogjava.net/matuobasyouca/comments/375213.html</wfw:comment><comments>http://www.blogjava.net/matuobasyouca/archive/2012/04/19/375213.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/matuobasyouca/comments/commentRss/375213.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/matuobasyouca/services/trackbacks/375213.html</trackback:ping><description><![CDATA[<p><span style="font-size: 10pt; font-family: 宋体; ">&#8220;企业信息集成（</span><span style="font-size: 10pt; font-family: Times, serif; ">EII</span><span style="font-size: 10pt; font-family: 宋体; ">）：实用方式&#8221;于</span><span style="font-size: 10pt; font-family: Times, serif; ">2005</span><span style="font-size: 10pt; font-family: 宋体; ">年发布，描述了一套集成不同数据源的方法论，利用了当时的先进技术，如面向服务架构（</span><span style="font-size: 10pt; font-family: Times, serif; ">SOA</span><span style="font-size: 10pt; font-family: 宋体; ">）、</span><span style="font-size: 10pt; font-family: Times, serif; ">Web Services</span><span style="font-size: 10pt; font-family: 宋体; ">、</span><span style="font-size: 10pt; font-family: Times, serif; ">XML</span><span style="font-size: 10pt; font-family: 宋体; ">、资源描述架构（</span><span style="font-size: 10pt; font-family: Times, serif; ">RDF</span><span style="font-size: 10pt; font-family: 宋体; ">）、基于</span><span style="font-size: 10pt; font-family: Times, serif; ">XML</span><span style="font-size: 10pt; font-family: 宋体; ">的元数据格式以及数据提取、转换和加载（</span><span style="font-size: 10pt; font-family: Times, serif; ">ETL</span><span style="font-size: 10pt; font-family: 宋体; ">）。</span><span style="font-size: 10pt; font-family: Times, serif; ">EII</span><span style="font-size: 10pt; font-family: 宋体; ">能够基本为关系型数据元素提供统一视角，但在性能效率上缺乏能够替代数据仓库和多维数据库的能力。五年之后技术已经得到了显著提升，不仅在于对于分散数据的操作，还有简化了单一容器下不同数据的整合，以及对数据深入挖掘的能力。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">转变了数据管理方式的技术正是虚拟化。低成本存储、云计算、</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">数据库以及</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Hadoop</span><span style="font-size: 10.0pt;font-family:宋体;">。当我们提起虚拟化时，已经远远超出为一台物理机器提供一套软件实例这一概念。时至今日，我们可以虚拟化服务器、存储以及网络。所有这些虚拟化意味着我们不再被这些物理条件所限制，能够迅速构建物理环境以支持我们特定时刻的特定需求。当面对</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Gb</span><span style="font-size: 10.0pt;font-family:宋体;">、</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Tb</span><span style="font-size: 10.0pt;font-family:宋体;">、</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Pb</span><span style="font-size: 10.0pt;font-family:宋体;">等级数据量的处理需求时，我们基本能摆脱结构化的数据仓库。我们不在需要仅仅为了发掘业务的某一方面而建立一个特殊的环境了。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">低成本存储在业务的数据存储方面节省了开支。高昂的存储成本会使得企业寻找在限定规模的数据之上进行关键业务分析的方案，这样使得如何选择最重要的数据变得十分关键，而且还限制了系统能够处理的数据的质量。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">负面影响便是业务最终可能面临很少的选择，因为没有足够的历史数据提供从而识别一个有效关键模式。或者因为高昂的投入使得业务被停止，而使用常规惯例来识别模式。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">云计算为那些需要通过海量数据源在合理时间范围内产生结果的需求提供了一个可用的方式。海量数据处理需要两点：弹性存储，</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">CPU</span><span style="font-size: 10.0pt;font-family:宋体;">。高速网络很有帮助，但是待会我们会看到在发掘软件在处理海量数据时，它并非是系统的瓶颈。弹性存储意味着企业不会在期望操作的数据规模或类型上受到限制，降低了使用数据仓库无法获取最佳结果的风险。更多的</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">CPU</span><span style="font-size: 10.0pt;font-family:宋体;">使得结果能够在期望的时间范围内更快的被交付。</span></p>
<p><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">提供了海量数据的支持，但与传统的关系型数据库没有关联。而且大部分</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">数据库是开源的，无须支付购买证书等费用。</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">对于表结构有着惊人的灵活性，无须随着系统的改进而不断修改完善定义。</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">可以支持不同数据源的合并查看，从而成为</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">之后另一个备选方案，这或许是</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">最重要的方面了。</span></p>
<p><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">内置了数据冗余与分布式数据存储机制。海量数据的最大问题之一就是磁盘读写，</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">通过将数据分布至一系列节点来缓解这个问题。当一个查询请求发出时，这些节点能够并行查询自身节点，而不是仅仅依靠一块磁盘，一个磁盘阵列或一条网络连接等，数据查询能够在节省了读写开支之后变得更加迅速。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">最终，我们来讨论</span><span style="font-size:10.0pt;font-family: &quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Hadoop</span><span style="font-size:10.0pt;font-family:宋体;">，集合了上述所有技术力量与一身的用于检测和分析数据的框架。有些人可能认为</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Hadoop</span><span style="font-size: 10.0pt;font-family:宋体;">是一项</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">技术，实际上</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Hadoop</span><span style="font-size: 10.0pt;font-family:宋体;">是一个分布组件的</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">java</span><span style="font-size: 10.0pt;font-family:宋体;">框架，用于分解&#8220;吃大象&#8221;（此处也双关</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Hadoop</span><span style="font-size: 10.0pt;font-family:宋体;">是以创立者的儿子给自己的一个大象玩具起的名字）的工作&#8212;&#8212;每次一口。</span></p>
<p><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Hadoop</span><span style="font-size: 10.0pt;font-family:宋体;">自身实际上与待处理数据是各自独立的。它将大型查询任务分解为小的并行查询任务，然后收集结果，并整合出答案返回给用户。</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Hadoop</span><span style="font-size: 10.0pt;font-family:宋体;">相对于</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">来说是一个并行查询框架，通过云计算驱动节点，运行在低成本存储及虚拟化技术之上。</span></p>
<p><strong><span style="font-size:16.0pt;font-family: &quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Kicking</span></strong><strong><span style="font-size:16.0pt;font-family:宋体;">的知识回顾</span></strong><strong></strong></p>
<p><span style="font-size:10.0pt;font-family:宋体;">当</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size:10.0pt;font-family:宋体;">第一次作为最佳实践出现于</span><span style="font-size:10.0pt;font-family: &quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">2003-2004</span><span style="font-size:10.0pt;font-family:宋体;">年，关键要素就是无需再移动数据了。当时大部分的数据中心仍然运行于低速网络中，有限的空间用于复制数据。之后，</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">成为了当时可用技术和问题域中最优秀的解决方案。</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">的某些方面的优秀即使在海量数据中也是很显著的。</span></p>
<p><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">的优点之一就是将处理过程转移到数据所在地。海量数据方案的关键架构要素之一就是将处理过程转移到数据所在地，而不是转移数据。</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">中的一个重要原则就是使用数据归属地的查询功能。这项实践就是构建靠近数据源网络的</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Web Service</span><span style="font-size:10.0pt;font-family:宋体;">，能够建立起通用查询接口，但只针对本地数据库进行查询。我们通过开放的基于</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Web</span><span style="font-size: 10.0pt;font-family:宋体;">的接口解决了数据的专有格式的问题，从而使得多个数据子集能够迅速的整合并以统一模式展示。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">有了低成本存储和</span><span style="font-size:10.0pt;font-family: &quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">10G</span><span style="font-size:10.0pt;font-family:宋体;">网络之后，我们就不必那么担心数据冗余与数据迁移，但还是有其他问题存在的，数据仓库无法确保数据的原始性便是其中之一。在</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">中，我们将从原始数据源获取数据视为&#8220;黄金准则&#8221;，这样就能够保证信息未被修改过，且是准确的。</span></p>
<p><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Big Data</span><span style="font-size:10.0pt;font-family:宋体;">要求数据必须转移到新的物理位置，这样可信任度又成为了问题。</span><span style="font-size: 10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size:10.0pt;font-family:宋体;">的那些获取基线数据的最佳实践仍然是相关而且重要的。实际上，那些为</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">设计开发的</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Web Services</span><span style="font-size:10.0pt;font-family:宋体;">接口最终在</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Big Data</span><span style="font-size:10.0pt;font-family:宋体;">的启用中扮演主要角色。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">当然，讨论数据管理不能不涉及到安全问题。</span><span style="font-size:10.0pt; font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size:10.0pt;font-family:宋体;">在安全领域中还是超过了</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Big Data</span><span style="font-size:10.0pt;font-family:宋体;">。技术上来说，</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Big Data</span><span style="font-size:10.0pt;font-family:宋体;">在数据集成方面更加高效与敏捷，但是大部分缺少了固有的安全性，因为在设计上会加大处理的难度。所以，可能要由源系统来担任起数据访问安全方面的责任。因为</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">EII</span><span style="font-size: 10.0pt;font-family:宋体;">直接在源系统中查询数据，所以必须要求有适当的授权，否则查询就将失败。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">上述关于安全讨论描述的是内在的安全控制情况。将访问权限控制列表集成进数据库是非常合理的，这将确保安全能够作为查询的一部分进行维护。然后，一旦能够直接查询</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">NoSQL</span><span style="font-size: 10.0pt;font-family:宋体;">数据源，就意味着能够自由的访问你所有的数据。</span></p>
<p><strong><span style="font-size:16.0pt;font-family:宋体;">总结</span></strong><strong></strong></p>
<p><span style="font-size:10.0pt;font-family:宋体;">引用老的</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Virginia Slims</span><span style="font-size:10.0pt;font-family:宋体;">的广告中的台词：&#8220;我们已经历很长的路途了，宝贝儿！&#8221;文中讨论到的技术的发展已经对</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">21</span><span style="font-size: 10.0pt;font-family:宋体;">世纪第二个</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">10</span><span style="font-size: 10.0pt;font-family:宋体;">年中的的数据解决方案产生了巨大的影响。商业化与小型化扫除了一些思想体系上的障碍，使得架构师能够专注于问题本身，而不是寻找一些实用及可实现的问题解决方案。构建</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">10000</span><span style="font-size: 10.0pt;font-family:宋体;">个节点的处理引擎，能够在数秒内处理</span><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">Pb</span><span style="font-size: 10.0pt;font-family:宋体;">级别的数据量，却只消耗每小时几便士，这就是数据处理的美好前景。</span></p>
<p><span style="font-size:10.0pt;font-family:宋体;">有了这些新工具，我们就要重新考虑如何推进数据管理。为何数据无法被很好地被维护整合，并且需要花费数万美元。数据管理几乎是每个大中型企业的心病。数据管理曾经在存储、管理、访问、整合以及查询上花费巨大，但是今后不再会是这样了。</span></p>
<p><strong><span style="font-size:16.0pt;font-family:宋体;">关于作者</span></strong><strong></strong></p>
<p><strong><span style="font-size:10.0pt; font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">JP Morgenthal</span></strong> <span style="font-size: 10pt; font-family: 宋体; ">是在</span><span style="font-size: 10pt; font-family: Times, serif; ">IT</span><span style="font-size: 10pt; font-family: 宋体; ">策略与云计算方面的世界级专家之一。他在企业复杂问题域的解决方案实施上有着</span><span style="font-size: 10pt; font-family: Times, serif; ">25</span><span style="font-size: 10pt; font-family: 宋体; ">年的经验。</span><strong><span style="font-size:10.0pt;font-family:&quot;Times&quot;,&quot;serif&quot;;Times New Roman&quot;">JP Morgenthal</span></strong><span style="font-size: 10pt; font-family: 宋体; ">以其在技术方面的深度和广度，有利的支持他在企业问题域中的敏感度。他在集成、软件开发和云计算是一位让人尊敬的作者，同时也是</span><span style="font-size: 10pt; font-family: Times, serif; ">InfoQ</span><span style="font-size: 10pt; font-family: 宋体; ">在引领云计算方面的编辑，并且参与了&#8220;云计算：评估风险&#8221;项目。</span>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family:&quot;MS Mincho&quot;;">原文</span><span style="font-family:宋体;">链</span><span style="font-family:&quot;MS Mincho&quot;;">接：</span>http://www.infoq.com/articles/DataIntegrationFromEIItoBigData</p><img src ="http://www.blogjava.net/matuobasyouca/aggbug/375213.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/matuobasyouca/" target="_blank">一酌散千忧</a> 2012-04-19 07:15 <a href="http://www.blogjava.net/matuobasyouca/archive/2012/04/19/375213.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Nosql企业之道</title><link>http://www.blogjava.net/matuobasyouca/archive/2012/04/04/373338.html</link><dc:creator>一酌散千忧</dc:creator><author>一酌散千忧</author><pubDate>Wed, 04 Apr 2012 13:41:00 GMT</pubDate><guid>http://www.blogjava.net/matuobasyouca/archive/2012/04/04/373338.html</guid><wfw:comment>http://www.blogjava.net/matuobasyouca/comments/373338.html</wfw:comment><comments>http://www.blogjava.net/matuobasyouca/archive/2012/04/04/373338.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/matuobasyouca/comments/commentRss/373338.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/matuobasyouca/services/trackbacks/373338.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Nosql企业之道  http://www.infoq.com/articles/nosql-in-the-enterprise    &nbsp;  介绍  作为一个企业架构师的好处，就是我一直在找一些新的有希望的概念或想法，能够帮助我的企业用户处理不同垂直行业之间的问题。甚至在NoSQL这个词被杜撰（错误的杜撰？此处作者认为NoSQL这个词并不恰当，后面会提到）出来之前，因为上述的原因我曾持续...&nbsp;&nbsp;<a href='http://www.blogjava.net/matuobasyouca/archive/2012/04/04/373338.html'>阅读全文</a><img src ="http://www.blogjava.net/matuobasyouca/aggbug/373338.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/matuobasyouca/" target="_blank">一酌散千忧</a> 2012-04-04 21:41 <a href="http://www.blogjava.net/matuobasyouca/archive/2012/04/04/373338.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>给开发维护大型项目的开发者的建议</title><link>http://www.blogjava.net/matuobasyouca/archive/2012/03/29/373009.html</link><dc:creator>一酌散千忧</dc:creator><author>一酌散千忧</author><pubDate>Thu, 29 Mar 2012 14:44:00 GMT</pubDate><guid>http://www.blogjava.net/matuobasyouca/archive/2012/03/29/373009.html</guid><wfw:comment>http://www.blogjava.net/matuobasyouca/comments/373009.html</wfw:comment><comments>http://www.blogjava.net/matuobasyouca/archive/2012/03/29/373009.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/matuobasyouca/comments/commentRss/373009.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/matuobasyouca/services/trackbacks/373009.html</trackback:ping><description><![CDATA[<p>Tips to Developers Starting on Large Applications</p>  <p>原文引用：http://www.infoq.com/articles/tips-to-developers-starting-on-large-apps</p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">假设你是正在开发和维护一个包含</span>2000<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">个类并使用了很多框架的</span>Java<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">开发者。你要如何理解这些代码？在一个典型的</span>Java<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">企业项目小组中，大部分能够帮你的高级工程师看起来都很忙。文档也很少。你需要尽快交付成果，并向项目组证明自己的能力。你会如何处理这种状况？这篇文字为开始一个新项目的</span>Java<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">开发者提供了一些建议。</span></p>  <p>&nbsp;</p>  <p style="margin-left:21.25pt;text-indent:-21.25pt;"><strong><span style="font-size:14.0pt;Times New Roman&quot;">1<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:14.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">不要试图一下子搞懂整个项目</span></strong><strong></strong></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">好好考虑一下，为什么理解项目代码是第一位的？大部分情况是你被要求修复一个</span>bug<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">或者加强系统已有功能。你要做的第一件事情不是理解整个项目的架构。当对项目进行维护时，</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">这样（理解整个项目架构）可能会对你造成巨大的压力。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">即便是有着</span>10<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">年可靠编程经验的</span>Java<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">开发者可能也没有理解项目的核心工作机制，尽管他们可能已经在这个项目工作超过一年（假设他们并非原始开发人员）。比如，对于认证机制或事务管理机制。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">他们是怎么做的？他们对于自己负责的部分非常了解，并且能够交付价值给小组。每天的交付价值远比了解一些以后还不确定有没有的东西重要的多。</span></p>  <p style="margin-left:21.25pt;text-indent:-21.25pt;"><strong><span style="font-size:14.0pt;Times New Roman&quot;">2<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:14.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">关注于尽快交付价值</span></strong><strong></strong></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">那我是否定了你对于项目架构理解的热情了么？完全不。我只是要求你尽早的交付价值，一旦你开始一个项目，搭建了开发环境，你就不应该花一两周时间才交付什么，无论他的规模大小。假如你是一个有经验的程序员却两周都没有任何交付，你的经理怎么会知道你是真的在工作还是在看新闻。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">所以交付可以使大家都轻松起来。不要认为你能够做有价值的交付前必须理解整个项目。这是完全错误的。加一段</span>javascript<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的验证代码对业务就很有价值，经理能够通过你的交付达到对你的信任。这样能够向上级领导证明你的贡献以及员工价值。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">日复一日，在你不断修复</span>bug<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">及增强功能之后，就能够慢慢开始理解项目架构。不要低估对系统方方面面理解时需要花费的时间。花</span>3-4<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">天理解认证机制，</span>2-3<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">天理解事物管理。这些都是依靠之前的相似项目的经历，但关键还是要花时间才能透彻的理解。要在日常工作中挤出时间，不要向经理要求特定的时间来做这些。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">找找项目是否有一些不断维护的单元测试用例。有效的单元测试用例是理解大型项目代码的很好途径。单元测试能够帮助理解代码片段，包括一个单元的外部接口（单元如何被调用以及返回内容）及其内部实现（调试单元测试比调试整个实际用例简单许多）。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">你如果能够很好的理解一些内容，写一些笔记，或者画一些类图、时序图、数据模型图，以便你或日后其他的开发者维护。</span></p>  <p>&nbsp;</p>  <p style="margin-left:21.25pt;text-indent:-21.25pt;"><strong><span style="font-size:14.0pt;Times New Roman&quot;">3<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:14.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">维护大型项目所必须的技能</span></strong><strong></strong></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">你能从事当前的工作，必然已经具有良好的</span>java<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">技术。我们来谈谈能够让你在新项目中良好表现的其他技能。大部分时间，你在项目中的任务是修复</span>bug<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">和增强功能。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">有两项很重要的技能能够协助你维护大型项目代码。</span></p>  <p style="margin-left:49.6pt;text-indent:-1.0cm;"><strong><span style="font-size:12.0pt;Times New Roman&quot;">3.1<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:12.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">能够迅速发现需要的类</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">在任何维护活动中，无论是修复bug或增强功能，第一个动作就是识别出当前修复或增强的用例中调用的类。当你定位到需要修复或增强的类/方法，就已经完工了一半。</p>  <p style="margin-left:49.6pt;text-indent:-1.0cm;"><strong><span style="font-size:12.0pt;Times New Roman&quot;">3.2<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:12.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">能够分析变更的影响</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">当你在完成必要的修改或增强工作后，最重要的就是要确认你的修改没有破坏代码的其他部分。你要用你的java技术及对其他框架的理解找出变更可能影响的部分。下面有两个简单的例子详细描述了最后提及的情况：</p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;background:white">a）当类A的equals()方法变更后，调用一个保护A实例的List的contains()方法时就会被影响到。若Java知识不够，很难考虑到这样的影响。</p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;background:white">b）在一个web项目中，我们假设&#8220;user id&#8221;保存在session中。一个新入程序员可能在&#8220;user id&#8221;中加入一些信息作为bug修复的方法，但是却不知道会影响到那些关联&#8220;user id&#8221;的用例。</p>  <p style="background:white">当你提高了如上两个技能，尽管你对项目不是非常了解，但大部分的维护任务会变得简单很多。若你修复一个bug，你会定位并修复这个bug，并且保证变更不会破坏项目的其他部分。若你增强或加入一个特性，基本上你只需要模仿现有的特性使用相似的设计。</p>  <p style="background:white">在一个在线银行项目中，为什么&#8220;查看账户摘要&#8221;和&#8220;查看交易历史&#8221;的设计需要巨大的差别呢？如果你理解了&#8220;查看账户摘要&#8221;的设计，完全可以模仿开发出&#8220;查看交易历史&#8221;的功能。</p>  <p style="background:white">就修复bug和增强来说，你不必完全理解所有2000个类的工作内容和代码如何运行来推动系统。你若有上面的技能，就能很快定位需要修改的代码的部分，使用良好的java和框架技能修复，保证变更不会破坏项目的其他部分并交付，尽管你可能只知道一小部分项目的设计。</p>  <p style="margin-left:21.25pt;text-indent:-21.25pt;"><strong><span style="font-size:14.0pt;Times New Roman&quot;">4<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:14.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">使用工具找到需要的变更内容以及变更产生的影响</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">继续我们尽快交付的主题，你应当寻找那些能够通过尽量少的了解项目但能帮助你尽快实施交付的工具作为辅助。</p>  <p style="margin-left:49.6pt;text-indent:-1.0cm;"><strong><span style="font-size:12.0pt;Times New Roman&quot;">4.1<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:12.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">迅速发现需要变更内容的工具</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">无论是修复bug还是系统增强，首先都要找到该用例调用的你需要修改的类及方法。基本有两种方式理解一个用例的工作方式，静态代码分析和运行时分析。</p>    <p style="background:white">源码分析统计扫描所有代码并且展示类之间的关系。市场上有很多设备与工具。比如：Architexa， AgileJ， UModel， Poseidon等。</p>  <p style="background:white">所有的静态代码分析工具缺点在于无法确切展示用例中类或方法的运行时调用情况。因此Java新加入了特性，如回调机制（callback patterns）。如静态分析工具无法推断出当页面提交按钮被点击时哪个Servlet被调用了。</p>  <p style="background:white">运行时分析工具能够展示类和方法在用例运行时的状态。工具包括：MaintainJ， Diver，jSonde，Java Call Tracer等。这些工具可以捕获运行时的堆栈状态，并以此为一个用例生成序列图和类图。</p>  <p style="background:white">序列图展示了该用例在运行时所有调用的方法。若你在修复一个bug，那这个bug很可能就是这些被调用的方法之一。</p>  <p style="background:white">若你在增强已有功能，利用序列图理解调用流程然后再修改。可能是新增一个验证，修改DAO等。</p>  <p style="background:white">若你在新增功能，找到一些相似的特性，利用序列图理解调用流程然后模仿开发新功能。</p>  <p style="background:white">要小心挑选运行时分析工具。信息过多是这类工具的主要问题。选择一些提供简单过滤无效信息并能够方便的查看各种视图的工具。</p>  <p style="margin-left:49.6pt;text-indent:-1.0cm;"><strong><span style="font-size:12.0pt;Times New Roman&quot;">4.2<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:12.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">迅速发现需要变更内容的工具</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">若单元测试有效，可以通过运行单元测试发现变更有没有破坏其他测试用例。有效维护并且覆盖大型企业应用的单元测试还是比较少的。下面有一些针对该情况的工具。</p>  <p style="background:white">仍然是有两种技术静态代码分析和运行时分析可以使用。市场中有很多静态代码分析工具可用。如：Lattix, Structure101, Coverity, nWire and IntelliJ's DSM。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">给定一个变更后的类，上述工具均可识别对该类存在依赖的类的集合。开发者需要根据这些信息&#8220;猜测&#8221;可能产生影响的用例，因为这些工具无法展示运行时类之间的调用关系。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">市场上的可以用于运行时影响分析的工具并不多，除了MaintainJ。MaintainJ先捕获在一个用例中调用的所有类和方法。当所有用例的上述信息都被捕获之后，就很容易发现类的变更对用例的影响。MaintainJ能够有效工作的前置条件就是项目的所有用例都应当先运行一遍，以便能够获得运行时的依赖关系。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">总之，目前你在迅速准确分析变更影响方面，还是可以从工具中获得有限的帮助。首先根据需要实施一些影响分析，然后根据自己或小组其他高级成员评审来判断变更的影响。你可能需要上面提到的工具对你的判断进行反复确认。</p>  <p style="margin-left: 21.25pt; text-indent: -21.25pt; "><strong><span style="font-size:14.0pt;Times New Roman&quot;">5<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:14.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">对上述内容的两个忠告</span></strong><strong></strong></p>  <p style="margin-left:49.6pt;text-indent:-1.0cm;"><strong><span style="font-size:12.0pt;Times New Roman&quot;">5.1<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:12.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">不要降低代码质量</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">为了快速交付，所以没有全盘理解架构，但绝不能以降低代码质量为条件。下面是一些你可能因为只考虑快速交付而引发的代码质量问题。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">因为修改代码涉及到很多的依赖，所以新增代码相对而言风险较小。例如，有5个用例都调用了某个方法。为了改进某个用例，你需要修改这个方法的实现。最简单的做法就是复制这个方法，重命名，然后在改进的用例中调用新方法。千万不要这么做。代码冗余绝对是非常有害的。尝试对方法进行包装或者重写，甚至是直接修改，然后重新测试所有用例，通常停下来想一想，然后亲手去实施，是一个比较好的方式。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">另一个例子是将&#8220;private&#8221;方法改为&#8220;public&#8221;，使得别的类也可以调用。尽量不要将非必须的部分暴露出来。假如为了更好的设计需要重构，就应当着手去做。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">大部分应用都有确定的结构和模式来实施。修复或增强程序时，确认你没有偏离这样的模式。若对约定不确定，请其他的高级开发者来审核你的变更。若你必须做一些违背约定的实施，尽量放置于一个规模较小的类中（一个200行代码的类中的私有函数应当不会影响应用的整体设计）</p>  <p style="margin-left: 49.6pt; text-indent: -1cm; "><strong><span style="font-size:12.0pt;Times New Roman&quot;">5.2<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:12.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">不要停止深入理解项目架构</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">按照文章列出的方式，假设你能够在对项目了解较少的情况下进行交付并以此持续下去，可能你会停止对项目架构的深入了解。这样从长远角度来说对你的职业生涯没有帮助。当你的经验增加时，你应当承担比较大的模块任务。如构建一个完整的新特性或者修改项目的一些基础设计等较大的改进。当你能够做这些改进时，你对项目的整体架构应该相当了解。文中列举的方法是让你在最短的时间内提升自己，而不是阻止你完整理解整个项目。</p>  <p style="margin-left: 21.25pt; text-indent: -21.25pt; "><strong><span style="font-size:14.0pt;Times New Roman&quot;">6<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-size:14.0pt;font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">结论</span></strong><strong></strong></p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">整篇文章集中在对项目进行必要了解的前提下进行快速交付。你可以在不降低代码质量的前提下这么做。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">若修复一个bug，迅速定位并修复。有必要可以使用运行时分析工具。若新增一个特写，可以寻找相似特写，理解流程（有必要使用工具）并编写。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">或许这些听起来很简单，但是实用吗？当然。但前提是你有良好的java技术以及对框架足够了解才能先修改代码，然后对变更影响进行分析。对变更影响的分析比实施变更需要更多的技巧。你可能需要高级开发人员协助你分析变更影响。</p>  <p style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">大约有50%的IT可操作预算用于简单的bug修复和功能增强。根据文中的建议，对于维护活动中的经费的节省应当还是很有帮助的。</p>  <p style="background:white">作者Choudary Kothapalli 也是MaintainJ项目的建立者。</p>  <h2><span style="font-size: 12pt; font-family: Arial, sans-serif; letter-spacing: -0.65pt; ">About the Author</span></h2>  <p style="line-height: 10pt; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; "><strong><span style="font-size: 8pt; font-family: Tahoma, sans-serif; ">Choudary Kothapalli</span></strong>&nbsp;<span style="font-size: 8pt; font-family: Tahoma, sans-serif; ">is the founder of&nbsp;<a href="http://www.maintainj.com/"><span style="color:#0B59B2">MaintainJ Inc.</span></a>, the company that builds tools to reduce the costs of maintaining large Java applications. He has over 15 years of experience in developing and maintaining enterprise Java applications. He is a Sun Certified Enterprise Architect and Java Programmer. He lives with his wife and two sons in Toronto, Canada.</span></p>  <p>&nbsp;</p><img src ="http://www.blogjava.net/matuobasyouca/aggbug/373009.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/matuobasyouca/" target="_blank">一酌散千忧</a> 2012-03-29 22:44 <a href="http://www.blogjava.net/matuobasyouca/archive/2012/03/29/373009.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>