﻿<?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-hengheng123456789-随笔分类-云</title><link>http://www.blogjava.net/hengheng123456789/category/47447.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 29 Apr 2011 10:15:18 GMT</lastBuildDate><pubDate>Fri, 29 Apr 2011 10:15:18 GMT</pubDate><ttl>60</ttl><item><title>Amazon AWS使用体验（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349292.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 29 Apr 2011 09:26:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349292.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/349292.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349292.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/349292.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/349292.html</trackback:ping><description><![CDATA[转自：<a href="http://huandu.me/2010/06/19/619/">http://huandu.me/2010/06/19/619/</a>
<div><br />
</div>
<div>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">近几个月来简单体验了Amazon AWS大部分服务，现在分享一下使用感受。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Amazon EC2</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">从各方面来看，EC2应该算是Amazon的王牌产品。EC2功能全面，它可以在数分钟内launch一个实例，可以（理论上）自动提升/降低机器性能配置，可以绑定EBS成为具有高可用性特质的计算核心，可以摇身一变成为ELB成为一个load balancer。这些功能非常实用，特别适合针对国外市场且快速增长的小网站，可以在前期完全不受机器约束。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">EC2机器的内核在申请时指定，有很多选择，如果有闲心，还可以<a href="http://www.udfi.biz/2009/05/creating-a-centos-53-amazon-ami/" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: underline; outline-style: none; outline-width: initial; outline-color: initial; color: #575757; ">做一个自己专用的</a>，我们没用这个功能，实在太繁琐了。EC2提供了完全的root权限，可以随意安装软件，由于EC2一般带有足够的硬盘空间（Large Instance就自带850GB磁盘空间），装个mysql当数据库机器也完全没有问题。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">EC2最大优势是灵活，最大劣势则是性能。EC2的IO性能欠佳，当EC2机器作为mysql数据库时，系统吞吐量只能到3~10 MB/s，相比而言，在同样数据同样程序条件下，使用真实机器则能轻松达到30MB/s。EC2的CPU是多个实例共享的，一般stolen的CPU都在10%左右，对于nginx+php-cgi这样的组合，EC2高端配置也不太能撑住压力，总是CPU吃紧。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">虽说EC2劣势明显，但由于互联网应用一般都很容易做到水平扩展，它的灵活性可以很大程度缓解这个劣势，不会有太大问题。不过最终我们还是放弃了EC2，主要因为今年5月Amazon云计算服务出了大量的事故，仅5月头两周，在同一个机房就出了5起大事故，我们在3起事故中受影响，每次都很不幸的都是单点EC2出问题，最长的一次在高峰期中断了6个小时服务，损失巨大。最郁闷的是，到了5月下旬，有一台跑数据库的EC2机器莫名奇妙磁盘损坏，还有一台莫名其妙的无法ssh，让我们感觉越来越不靠谱，于是决定逃离。云计算的可用性始终值得担心。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">EC2的价格相对传统IDC要贵不少，按高端机器High-Memory Double Extra Large每小时1.2US$来算，每月就要花864US$，再加上流量费，很容易达到每台服务器1000US$的水平。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Amazon RDS</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">RDS是一种数据库托管服务，通过RDS toolkit可以很方便的创建RDS实例并立即使用。据Amazon帮助文档说，RDS的数据库软件维护、升级、优化的工作全部由Amazon负责，用户只需要使用功能即可。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">可惜，RDS纯粹是一个看起来很美的东西。首先是RDS为了同时支持异构的数据库，牺牲了很多灵活性，例如，因为无法访问RDS的物理机器或存储空间，使得跑着mysql的RDS实例连记录slow log都成了难事，更别说什么err log，出了问题只能一抹瞎。如果要获取bi/bo这样的数据需要使用专用的RDS toolkit完成，很难集成到现成的监控工具里去。其次，RDS底层使用的是类似于EBS的网络存储，其IO性能差到一个令人发指的地步。一个很感性的数据是：使用RDS Double Extra Large DB Instance跑mysql时慢查询占1%，同样数据转到EC2 High-Memory Double Extra Large实例后慢查询降为0.01%。最让人无语的是RDS价格高于同等配置的EC2+EBS价格，每小时都贵超过0.3US$，相当于贵25%，省下来的钱用来雇专业运维人员绰绰有余，而且RDS功能和灵活性还远弱于后者，不怕折腾但怕花钱的人不建议选择RDS。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">如果执意要使用RDS，那么一定要注意RDS没有固定IP，每次重启实例（或过一段时间）都会变，程序连接的时候最好写域名，当然，这会损失性能。此外，RDS可用性也没宣传的那么高，我们5月最先挂的就是RDS实例，一挂就是好几个小时。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Amazon S3</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">S3是少有的看上去物美价廉的东西，只需要花几美元就可以拿到几十GB空间，对一般的互联网应用来说绰绰有余。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">S3提供丰富的API和工具上传文件。对firefox用户来说，有一个专门的S3扩展可用，可直接上传本地文件。还有一个s3curl.pl工具，可以用命令行传任意文件，可以方便的做到自动化。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">S3存储的文件可以直接通过域名从外网访问，不过比较郁闷的是文件尾部不能带&#8220;?&#8220;，一切基于文件名后面加随机串的避免缓存的做法都会失效。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Amazon CloudFront</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">CloudFront是一种CDN，它的访问速度据称不错，我们并没有实际使用过。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">我们不使用CloudFront主要是因为很难控制文件在CloudFront中失效。根据我们的程序结构，我们需要在静态文件后面加version信息强制用户在必要时更新到最新版，现在由于CloudFront既不能手动invalidate某个文件，也不能保证CloudFront上缓存的文件及时更新（可能更新会花24小时），我们需要修改现在的策略才能使用到它。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">CloudFront是根据用户所在网络来收费的，对于国内，CloudFront使用的是香港价格，贵于美洲和欧洲，不太划算。对于欧美市场，我也不清楚欧美CDN的价格究竟如何，无法比较。CloudFront在欧美的流量费用与EC2相同，不过由于没有按小时计的租费，总体价格会比EC2提供静态文件服务便宜很多。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Amazon CloudWatch</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">CloudWatch是一个性能数据监视器，对于这个东西我只能说太小儿科了，没看出来有什么实际的用处。一旦开启CloudWatch，Amazon就会帮忙收集开启了这个服务的instance数据，包括CPU和IO信息等。可惜，数据精度不高（大概是几分钟一次统计），数字也只是个大概值，能看的数据也很少，无论对入门者还是高手来说，都不能带来太多价值。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">CloudWatch看上去比较好的是可以配合EC2/EBS/Load Balancer toolkit做一些auto scale的事情，自动提升/降低EC2/EBS参数，还可以（理论上）自动launch新实例。这些东西我们都没尝试过，有兴趣的同学可以试试。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">CloudWatch价格不贵，一台机器就只用0.1US$而已，不过一般还是不建议开，装一个其他监控软件更靠谱。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Amazon Premiun Support</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">Premiun Support包括即时的电话支持和一个专有的提案系统。从服务质量上来说，Amazon的客服总是很热心，响应速度也很快，专业素质也不错，很不错。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">Premium Support非常贵，简直是抢钱。它是按照每月总花费来算价格的，最多增加总花费的16%，最少为无限接近于10%，代价十分明显。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">结语</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 18px; padding-left: 0px; color: #575757; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 18px; ">Amazon AWS提供丰富功能，还有不少toolkit和API，适合小应用使用。对于要求较高性能或较低成本的应用来说则不太适合，折腾一下IDC应该达到更好效果。</p>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/349292.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-04-29 17:26 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349292.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Google App Engine（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349279.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 29 Apr 2011 07:05:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349279.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/349279.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349279.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/349279.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/349279.html</trackback:ping><description><![CDATA[转自：<a href="http://baike.baidu.com/view/1524918.htm">http://baike.baidu.com/view/1524918.htm</a>
<div>
<div class="mod-top" style="margin-bottom: 30px; font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; ">
<div class="card-summary nslog-area" data-nslog-type="72" style="line-height: 25px; font-size: 14px; margin-bottom: 25px; ">
<div style="float: left; width: 469px; ">
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Google App Engine 是一种让您可以在 Google 的基础架构上运行您的网络<a target="_blank" href="http://baike.baidu.com/view/330120.htm" style="text-decoration: underline; color: #136ec2; ">应用程序</a>。Google App Engine 应用程序易于构建和维护，并可根据您的访问量和数据存储需要的增长轻松扩展。使用 Google App Engine，将不再需要维护服务器：您只需上传您的应用程序，它便可立即为您的用户提供服务。</p>
</div>
<div class="clear" style="clear: both; font-size: 0px; line-height: 0; height: 0px; visibility: visible; "><br />
</div>
</div>
</div>
<div class="lemma-main-content" id="lemmaContent-0" style="letter-spacing: 0.5px; width: 690px; overflow-x: hidden; overflow-y: hidden; float: left; font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; ">
<h2 class="headline-1 first bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: none; "><a name="1" style="text-decoration: underline; color: #136ec2; width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a><span class="headline-content">简介</span></h2>
您可以使用 Google提供的免费域名（见扩展阅读）为您的<a target="_blank" href="http://baike.baidu.com/view/330120.htm" style="text-decoration: underline; color: #136ec2; ">应用程序</a>提供服务，也可以使用 Google Apps 从您自己的域为它提供服务。您可以与全世界的人共享您的应用程序，也可以限制为只有您组织内的成员可以访问。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
您可以免费开始使用 App Engine。注册一个免费帐户即可开发和发布您的应用程序以供全世界的人共享，而且不需要承担任何费用和责任。每个免费帐户都可使用多达 500MB 的持久存储空间，以及可支持每月约 500 万页面浏览量的足够 CPU 和宽带<sup style="margin-left: 2px; color: #3366cc; cursor: pointer; ">[1]</sup><a name="ref_[1]" style="text-decoration: underline; color: #136ec2; width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a>。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
使用 Google App Engine 的试用版本期间，只可以使用免费帐户。您很快就能够购买其他的计算资源。 App Engine提供了一个web应用运行的框架，App Engine 应用程序易于运行、部属和扩展。App Engine不同于其他方案：App Engine不是网格计算—它不能运行任意的作业。它不提供物理主机但提供了打包代码上传的方式。用户在只需要在代码中指定了response和request，剩下的运行和部署的任务就交给了App Engine。用户不需要预留资源，例如RAM、CPU或者诸如此类。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content"><br />
应用程序环境</span></h2>
通过 Google App Engine，即使在重载和数据量极大的情况下，也可以轻松构建能安全运行的应用程序。该环境包括以下特性：
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
动态网络服务，提供对常用网络技术的完全支持
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
持久存储有查询、分类和事务
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
自动扩展和载荷平衡
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
用于对用户进行身份验证和使用 Google 帐户发送电子邮件的 API
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
一种功能完整的本地开发环境，可以在您的计算机上模拟 Google App Engine
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
Google App Engine 应用程序是使用 Python 编程语言实现的。该运行时环境包括完整 Python 语言和多数 Python 标准库。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
目前，Google App Engine 支持 Python 语言和 JAVA 语言，但是我们希望将来它可以支持更多语言。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content">Sandbo</span></h2>
在安全环境中运行的应用程序，仅提供对基础<a target="_blank" href="http://baike.baidu.com/view/880.htm" style="text-decoration: underline; color: #136ec2; ">操作系统</a>的有限访问权限。这些限制让 App Engine 可以在多个服务器之间分发应用程序的网络请求，并可以启动和停止服务器以满足访问量需求。Sandbox 将您的应用程序隔离在它自己的安全可靠环境中，该环境与<a target="_blank" href="http://baike.baidu.com/view/813.htm" style="text-decoration: underline; color: #136ec2; ">网络服务器</a>的硬件、操作系统和物理位置无关。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
安全 Sandbox 环境的限制实例包括：
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
应用程序只能通过提供的网址获取和<a target="_blank" href="http://baike.baidu.com/view/576456.htm" style="text-decoration: underline; color: #136ec2; ">电子邮件服务</a>和 API 访问互联网中的其他计算机。其他计算机只能通过在标准端口上进行 HTTP（或 HTTPS）请求来连接至该应用程序。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
应用程序无法向文件系统写入。应用程序只能读取通过应用程序代码上传的文件。该应用程序必须使用 App Engine 数据库存储所有在请求之间持续存在的数据。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
应用程序代码仅在响应网络请求时运行，且必须在几秒钟内返回响应数据。请求处理程序不能在响应发送后产生子进程或执行代码。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content"><br />
Python 运行时环境</span></h2>
App Engine 提供了一个使用 Python 编程语言的运行时环境。将来的版本将考虑使用其他编程语言和运行时环境配置。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
Python 运行时环境使用 Python 2.5.2 版。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
该环境包括 Python 标准库。当然，您无法调用违反了 Sandbox 限制的库方法，例如尝试打开套接字或向文件写入。方便起见，其主要功能不受该运行时环境支持的标准库中的多个模块已被禁用，而导入这些模块的代码将发生错误。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
应用程序代码只能以 Python 编写。具有用 C 编写的扩展的代码不受支持。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
Python 环境为数据库、Google 帐户、网址获取和电子邮件服务提供了丰富的 Python API。App Engine 还提供了一个称为 webapp 的简单 Python 网络应用程序框架，从而可以轻松开始构建应用程序。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
方便起见，App Engine 还包括 Django 网络应用程序框架 0.96.1 版。请注意，App Engine 数据库不是某些 Django 组件所需的关系数据库。某些组件（例如 Django 模板引擎）按照文档化的程序工作，而其他组件则需要做更多工作。有关将 Django 与 App Engine 一起使用的提示，请参阅文章部分。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
只要这些库是完全使用 Python 实现并且不需要任何不受支持的标准库模块，您就可以使用您的应用程序上传其他第三方库。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
有关 Python 运行时环境的详细信息，请参阅 Python 运行时环境。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content"><br />
数据库</span></h2>
App Engine 提供了一个强大的分布式数据存储服务，其中包含查询引擎和事务功能。就像<a target="_blank" href="http://baike.baidu.com/view/492583.htm" style="text-decoration: underline; color: #136ec2; ">分布式网络</a>服务器随访问量增加一样，该<a target="_blank" href="http://baike.baidu.com/view/68389.htm" style="text-decoration: underline; color: #136ec2; ">分布式数据库</a>也会随数据而增加。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
该 App Engine 数据库与传统关系数据库不同。数据对象（或&#8220;实体&#8221;）有一类和一组属性。查询可以检索按属性值过滤和分类的给定种类的实体。属性值可以是受支持的属性值类型中的任何一种。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
数据库的 Python API 包括一个可以定义数据库实体结构的<a target="_blank" href="http://baike.baidu.com/view/1452242.htm" style="text-decoration: underline; color: #136ec2; ">数据建模</a>界面。数据模型可以指示属性值必须位于给定范围内，如果未给定任何范围，还可以提供默认值。您的应用程序可以根据需要向数据提供或多或少的结构。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
数据库使用乐观锁定进行并发控制。如果有其他进程尝试更新某实体，而同时该实体位于以固定次数进行重新尝试的事务中，此时该实体将更新。应用程序可以在一个事务中执行多项数据库操作（全部成功或者全部失败），从而确保数据的完整性。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
数据库通过其分布式网络使用&#8220;实体组&#8221;实现事务。一个事务操作一个组内的实体。同一组的实体存储在一起，以高效执行事务。应用程序可以在实体创建时将实体分配到组。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
有关数据库的详细信息，请参阅数据库 API 参考。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content"><br />
Google 帐户</span></h2>
App Engine 包括用于与 Google 帐户集成的服务 API。应用程序使用户可以通过 Google 帐户登录，并可以访问与该帐户关联的<a target="_blank" href="http://baike.baidu.com/view/126647.htm" style="text-decoration: underline; color: #136ec2; ">电子邮件地址</a>和可显示的名称。使用 Google 帐户使用户可以更快地开始使用您的应用程序，因为用户可以不需要创建新帐户了。Google 帐户还省去只为您的应用程序执行用户帐户系统的麻烦。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
如果您的应用程序正在 Google Apps 下运行，则它可以与您组织的成员和 Google Apps 帐户成员使用相同的功能。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
用户 API 还可告知应用程序当前用户是否是应用程序的注册<a target="_blank" href="http://baike.baidu.com/view/315045.htm" style="text-decoration: underline; color: #136ec2; ">管理员</a>。这样便可以轻松实现您站点上仅用于管理的区域。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
有关与 Google 帐户集成的详细信息，请参阅用户 API 参考。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content"><br />
App Engine 服务</span></h2>
App Engine 提供了多种服务，从而使您可以在管理应用程序的同时执行常规操作。提供了以下 API 以访问这些服务：
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
网址获取
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
应用程序可以使用 App Engine 的网址获取服务访问互联网上的资源，例如网络服务或其他数据。网址获取服务使用用于为许多其他 Google 产品检索网页的高速 Google 基础架构来检索<a target="_blank" href="http://baike.baidu.com/view/21050.htm" style="text-decoration: underline; color: #136ec2; ">网络资源</a>。有关网址获取服务的详细信息，请参阅网址获取 API 参考。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
邮件
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
应用程序可以使用 App Engine 的邮件服务发送电子邮件。邮件服务使用 Google 基础架构发送电子邮件。有关邮件服务的详细信息，请参阅邮件 API 参考。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
Memcache
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
Memcache 服务为您的应用程序提供了高性能的内存键值缓存，您可通过应用程序的多个实例访问该缓存。Memcache 对于那些不需要数据库的永久性功能和事务功能的数据很有用，例如临时数据或从数据库复制到缓存以进行高速访问的数据。有关 Memcache 服务的详细信息，请参阅 Memcache API 参考。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
图片操作
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
图片服务使您的应用程序可以对图片进行操作。使用该 API，您可以对 JPEG 和 PNG 格式的图片进行大小调整、剪切、旋转和翻转。有关图片操作服务的详细信息，请参阅图片 API 参考。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content"><br />
开发工作流程</span></h2>
App Engine 软件开发套件 (SDK) 包括可以在您的本地计算机上模拟所有 App Engine 服务的网络服务器应用程序。该 SDK 包括 App Engine 中的所有 API 和库。该网络服务器还可以模拟安全 Sandbox 环境，包括检查是否存在禁用模块的导入以及对不允许访问的<a target="_blank" href="http://baike.baidu.com/view/53557.htm" style="text-decoration: underline; color: #136ec2; ">系统资源</a>的尝试访问。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
Python SDK 完全使用 Python 实现，可以在装有 Python 2.5 的任何平台上运行，包括 Windows、Mac OS X 和 Linux。您可以在 Python 网站上获得适用于您的系统的 Python。该 SDK 以 Zip 文件提供，安装程序可用于 Windows 和 Mac OS X。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
您可以在此处下载该 SDK。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
该 SDK 还包括可将您的应用程序上传到 App Engine 的工具。创建了您应用程序的代码、静态文件和配置文件后，即可运行该工具上传数据。该工具会提示您提供 Google 帐户电子邮件地址和密码。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
构建已在 App Engine 上运行的应用程序的新主要发行版时，可以将新发行版作为新版本上传。在您改为使用新版本之前，旧版本可以继续为用户提供服务。可以在仍运行旧版本的同时在 App Engine 上测试新版本。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
管理控制台是基于网络的界面，用于管理在 App Engine 上运行的应用程序。您可以使用它创建新应用程序、配置域名、更改您的应用程序当前的版本、检查访问权限和错误日志以及浏览应用程序数据库。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div>
<h2 class="headline-1 bk-sidecatalog-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 6px; padding-left: 0px; font-size: 18px; font-weight: bold; line-height: 24px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dedfe1; clear: both; "><span class="headline-content"><br />
限额和限制</span></h2>
创建 App Engine 应用程序不仅简单，而且是免费的！您可以创建帐户，然后发布一个应用程序，用户无需承担任何费用和责任即可立即使用该应用程序。通过免费帐户获得的应用程序可使用多达 500MB 的存储空间和多达每月 500 万的页面浏览量。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
此试用时间内，仅提供免费帐户。您很快就能够以有竞争力的市场价格购买其他的计算资源。试用期过后，免费帐户可继续使用。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
此试用期间，最多可注册 3 个应用程序。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
应用程序资源限制（&#8220;限额&#8221;）会不断刷新。如果您的应用程序达到基于时间的限额（例如宽带），则该限额将以给定限制的比率立即开始刷新。固定限额（例如存储使用量）仅在降低使用量时才会减小。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
有些功能会施加与限额无关的限制，以保护系统的稳定性。例如，当调用某应用程序以为网络请求提供服务时，该应用程序必须在几秒钟内发出响应。如果该应用程序花费的时间过长，则进程将被终止并且服务器将向用户返回错误代码。响应超时是动态的，如果请求处理程序经常达到其超时，则可以缩短请求超时以节省资源。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
服务限制的另一实例是查询返回的结果数。一个查询最多可返回 1,000 条结果。要返回更多结果的查询只能返回该最大值。在这种情况下，执行这种查询的请求不可能在超时前返回请求，但限制仍存在以节省数据库上的资源。
<div class="spctrl" style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>
试图破坏或滥用限额（例如同时在多个帐户上操作应用程序）违反服务条款，并可能导致应用程序被禁用或帐户关闭。
<div class="bpctrl" style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; clear: both; "></div>
<div class="clear" style="clear: both; font-size: 0px; line-height: 0; height: 0px; visibility: visible; "></div>
</div>
<div class="clear" style="clear: both; font-size: 0px; line-height: 0; height: 0px; visibility: visible; font-family: arial, 宋体, sans-serif; "></div>
<div class="reinforce" id="lemmaExtend" style="font-size: 12px; font-family: arial, 宋体, sans-serif; line-height: 24px; "><dl class="nslog-area" data-nslog-type="2" id="viewRefer" style="margin-top: 0px; margin-right: 0px; margin-bottom: 30px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><dt style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #cccccc; font-weight: bold; line-height: 1; font-size: 14px; ">参考资料</dt><dd class="reference" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; word-wrap: break-word; word-break: break-all; ">
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">
    <li class="layout" style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; zoom: 1; width: 690px; font-size: 12px; "><span class="ref-index" style="float: left; "><span class="r-index" style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">1</span></span>
    <div class="ref-bd line-1" style="margin-left: 26px; ">
    <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">谷歌欲借云计算应用软件Google Apps打进企业挑战Office<a class="ref-top" href="http://baike.baidu.com/view/1524918.htm#ref_[1]" id="refIndex_1" name="refIndex_1" title="向上跳转" style="text-decoration: none; color: #136ec2; width: 12px; height: 12px; overflow-x: hidden; overflow-y: hidden; padding-top: 0px; padding-right: 10px; padding-bottom: 0px; padding-left: 10px; margin-left: 4px; background-image: url(http://img.baidu.com/img/baike/s/up_down.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: -1px -78px; background-repeat: no-repeat no-repeat; ">&nbsp;&nbsp;</a></p>
    <p class="refUrl" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a href="http://www.cloudcomputing-china.cn/Article/cloudcomputing/200807/142.html" target="_blank" style="text-decoration: underline; color: #136ec2; ">http://www.cloudcomputing-china.cn/Article/cloudcomputing/200807/142.html</a></p>
    </div>
    </li>
</ul>
</dd></dl><dl class="nslog-area" data-nslog-type="1" id="viewExtRead" style="margin-top: 0px; margin-right: 0px; margin-bottom: 30px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><dt style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #cccccc; font-weight: bold; line-height: 1; font-size: 14px; ">扩展阅读：</dt><dd class="reference" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; word-wrap: break-word; word-break: break-all; ">
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; ">
    <li class="layout" style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; zoom: 1; width: 690px; font-size: 12px; "><span class="ref-index" style="float: left; "><span class="r-index" style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">1</span></span>
    <p class="ref-bd line-1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 26px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a target="_blank" href="http://code.google.com/intl/zh-CN/appengine/docs/whatisgoogleappengine.html" style="text-decoration: underline; color: #136ec2; ">http://code.google.com/intl/zh-CN/appengine/docs/whatisgoogleappengine.html</a></p>
    </li>
    <li class="layout" style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; zoom: 1; width: 690px; font-size: 12px; "><span class="ref-index" style="float: left; "><span class="r-index" style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">2</span></span>
    <p class="ref-bd line-1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 26px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a target="_blank" href="http://www.cloudcomputing-china.cn/" style="text-decoration: underline; color: #136ec2; ">http://www.cloudcomputing-china.cn</a></p>
    </li>
    <li class="layout" style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; zoom: 1; width: 690px; font-size: 12px; "><span class="ref-index" style="float: left; "><span class="r-index" style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">3</span></span>
    <p class="ref-bd line-1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 26px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a target="_blank" href="http://china-app-blog.appspot.com/" style="text-decoration: underline; color: #136ec2; ">http://china-app-blog.appspot.com</a></p>
    </li>
    <li class="layout" style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; zoom: 1; width: 690px; font-size: 12px; "><span class="ref-index" style="float: left; "><span class="r-index" style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">4</span></span>
    <p class="ref-bd line-1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 26px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Google提供的免费域名：appspot.com</p>
    </li>
</ul>
</dd></dl></div>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/349279.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-04-29 15:05 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349279.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Google App Engine和Amazon EC2谁更强（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349278.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 29 Apr 2011 07:00:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349278.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/349278.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349278.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/349278.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/349278.html</trackback:ping><description><![CDATA[转自：<a href="http://tech.163.com/digi/10/1111/06/6L6IS5NO001618J7.html">http://tech.163.com/digi/10/1111/06/6L6IS5NO001618J7.html</a>
<div>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">最近一个潜在客户要求我们比较一下Amazon EC2和Google App Engine，正好我们刚刚在EC2和Google App Engine上完成了两个相对来说规模较大的项目，因此有必要做一下总结，我打算从三个角度来对比这两大云计算平台：技术，业务和未来发展趋势，本文是技术方面的对比，ok，准备好咖啡，我们开始吧!</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  如果按平台类型来分，大家可能已经知道Google App Engine属于PaaS(平台即服务)，而Amazon EC2属于IaaS(基础设施即服务)，Amazon EC2给你一个安装了操作系统的&#8220;盒子&#8221;，你可以自己安装应用程序，也可以使用AMI(Amazon Machine Image)，如果你想构建一个高度复杂的定制应用，Amazon EC2是不二之选，它允许你控制环境参数，底层操作系统，存储和网络需求，从技术上讲，它属于很底层的服务，你可以调整你需要的大部分东西。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  Google App Engine给你一个完整的平台，包括完整的SDK(以及Eclipse插件)和服务，你可以构建和部署你自己的应用程序，但你不能很好地控制操作系统，硬件和存储，诸如写文件系统，使用线程等操作都有限制，这样设计的目的是为了确保平台不会被某个应用程序绑架。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  简单起来就是：</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  IaaS：原始硬件(处理器，网络和存储)</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  PaaS：操作系统，系统软件，开发框架和虚拟机。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>下面从技术角度来比较一下这两个平台。</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; "><strong>  1、提供的服务</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  Google App Engine凭借丰富的服务击败Amazon EC2，Google App Engine提供的服务可以让开发人员快速进入开发状态。如Blobstore，Images，Mail，Memcache，Multitenancy，Oauth，Task Queues，URL Fetch，Users，XMPP这些服务在Amazon EC2上是需要自己安装的，为了便于比较，假设你已经为Memcache，Mail和多租户搭建好了基础设施，看看在EC2上你用了多长时间安装，我敢打赌你会超过一个小时，使用Google App Engine时，这些服务都是现成的，就象果盘中插好牙签的水果一样，你可以随时享用。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>2、管理</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  Google App Engine再次胜出，因为一旦你的应用程序部署成功后，它几乎不需要管理，当你的应用程序负载变大时，你不需要向服务注入新的实例，Google App Engine可以自由扩大负载能力，新实例是实时剥离的。使用Amazon EC2时，你必须时刻跟踪通信流量(现在可以通过脚本自动跟踪了)，新实例是在你的配置基础上剥离的，因此，如果我的峰值负载是2x+y，那么需要剥离2个以上的应用程序服务器。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  此外，使用Google App Engine升级应用程序服务器实例，安装新的负载均衡器时，没有管理开销，这一切都是自动执行的。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>3、抽象水平</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  和上一条联系紧密的是抽象水平，Google App Engine抽象得比较好，你只需要关心你的应用程序和业务逻辑，不用担心底层基础设施的管理，正如Nick Johnson所说的那样，抽象水平应作为挑选云计算平台的一个基本原则，你需要做的是驾驶，不需要研究引擎盖以下的东西。在我看来，如果你的核心业务是货物运输，那么你应该买一辆卡车，它能高效地把你的货物从A地运输到B地，相反，你不应该考虑如何购买零部件自己组装一辆卡车。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  在软件开发领域，我们看到有Grails，RoR等框架，它们大受欢迎，是因为它们提供了高水平的抽象，如果你是一名泥瓦匠，它们就象是脚手架，你可以踩在它们上面干你的工作。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>4、可靠性</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  从我个人的认识来讲，两者都很可靠，这一点从它们的用户数量就可以知道一二，用户可以时刻查看Google App Engine的状态，它是透明的，但从历史数据来看，Amazon EC2的正常运行时间比Google App Engine要好。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>5、可移植性</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  从使用的底层操作系统和开发框架来看，Amazon EC2具有更好的可移植性，但也不要担心你会被Google App Engine给锁住，Google已经给出了迁移指南，指导你如何从转移出Google App Engine平台，当然包含你所有的数据在内。还有AppScale这样的程序可以帮助你将Google App Engine上的程序转移到Amazom EC2或其它云平台上，AppScale已经可以支持EC2，Eucalyptus，Xen和KVM。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>6、存储</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  Google App Engine目前严重依赖于BigTable，开发人员需要从一个完全不同的角度来认识和学习它，特别是对于那些特熟悉关系数据库，被关系数据库理论束缚的人更需要洗洗脑，它提供了一个JPA&amp;JDO访问接口，但它不支持所有的JPA&amp;JDO功能，特别是关系部分，Google最近也高调宣布要让Google App Engine支持传统的SQL数据库。Amazon EC2已经支持SQL数据库，你可以使用Oracle，MySQL等你所熟悉的关系数据库。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>7、应用程序维护和升级</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  对Google App Engine来说，应用程序维护和升级是件轻而易举的事，它为各种应用程序提供了一个详细的管理面板，包括日志查看器和数据查看器，一个程序可以有多个版本，当新版本经过测试，可以用于生产环境时，你可以将其设为默认的版本，而Amazon EC2就麻烦多了，因为它属于IaaS类型，所有维护和升级相关的事情你必须亲力亲为。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>8、开发限制</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  使用Google App Engine时，你必须受到平台的限制，如果你的查询处于僵死状态，很难将其杀掉，此外，Google App Engine没有线程，提供的SDK也是受限的，有些类和功能被列入黑名单，因此不能被使用，也不能写文件系统等等。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  从表面上看这些限制是不可理喻的，但如果有朝一日你也要提供PaaS类型的平台时，你就能理解为什么Google要做这些限制了，这样才能确保运行在平台上的应用程序不会违反平台的规则，否则平台就可能被应用程序绑架，从而变得不可使用，平台上的其它应用程序就会收到牵连。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  即便有这些限制，90%的商业应用程序仍然可以在Google App Engine上正常运行，但对于那些要使用线程，或写文件系统的应用，最好还是选择Amazon EC2，因为它提供了所有底层访问和控制权。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  <strong>9、语言支持</strong></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  截至目前，Google App Engine支持Java和Python，但任何可以转换成字节码，可在JVM上执行的任何编程语言都可以在Google App Engine上运行，如果你喜欢其它编程语言，最好选择Amazon EC2，因为你可以在它的操作系统上面安装语言运行时环境，你拥有几乎完整的硬件和操作系统控制权，还有什么不能做的呢?在Amazon EC2上也托管了许多有趣的C#，.NET，ASP.NET MVC/Visual Studio项目，具有讽刺意味着的是，尽管还有Microsoft Azure，但许多以MS技术开发的项目却托管在Amazon EC2上。</p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; "></p>
<p style="font-size: 14px; line-height: 23px; text-indent: 2em; color: #2b2b2b; font-family: 宋体, serif; ">  概括地说，Amazon EC2是进入云计算的早期尝试者，它利用互联网标准和开放平台创建了一个非常灵活的云计算平台，Google则利用了它在大型数据库方面的研究成果和它内部实现的一些技术创建了一个强大，但有更多限制的云计算环境。从核心技术来讲，Amazon EC2允许你扩展任何计算机实例到多个实例，因此你拥有每个虚拟盒子的完全控制权，Google App Engine从操作系统抽象而来，没有计算机实例的概念，如果你的Web应用程序不需要操作系统相关的功能，那么Google App Engine无疑是最好的选择，如果需要更好地控制你的系统环境，特别是操作系统相关的控制，那么最好选择Amazon EC2。</p>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/349278.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-04-29 15:00 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/04/29/349278.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Amazon的WEB服务（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/02/17/344548.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Thu, 17 Feb 2011 06:21:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/02/17/344548.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/344548.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/02/17/344548.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/344548.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/344548.html</trackback:ping><description><![CDATA[转自：http://www.cww.net.cn/zhuanti/html/2009/4/12/20095211347521767.htm
<div>
<div>&nbsp;&nbsp; &nbsp;作为一家网上书店和线上零售商，Amazon已经广为人知，但现在，Amazon的全新业务是云计算。两年多以前，云计算这块市场开始兴起，Amazon是当时为数不多的云计算提供商之一，在今天，全球有不少用户在计划接入&#8220;云&#8221;时都会考虑Amazon的服务。</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;Amazon的云计算服务总称为AmazonWebServices(AWS，Amazon WEB服务)，迄今它包括四种主要的服务：Simple Storage Service (S3，一种简单的存储服务)；Elastic Compute Cloud (EC2，弹性可扩展的云计算服务器)；Simple Queuing Service(一种简单的消息队列)；以及仍处在测试阶段的SimpleDB(简单的数据库管理)。换句话说，Amazon现在通过互联网提供存储、计算处理、消息队列(message queuing)、数据库管理系统等&#8220;即插即用&#8221;型的服务。</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;这些服务所需要的巨大的后台IT基础架构是Amazon的数据中心。不过，用户只需要对自己消费了的服务进行付费：S3存储是每月每GB为15美分，根据配置不同，EC2服务器容量是每小时10-80美分。</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;目前来看，AWS已经具备了云计算的三个基本特征：用户所需要的IT资源不在公司自己的数据中心里面，这些资源可以通过互联网获得，没有固定的投资成本。</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;Amazon所提供的第一种云服务是S3，它可以提供无限制的存储空间，让用户存放文档、照片、视频和其他数据。其后是EC2，用户可以选择不同的服务器配置，并对实际用到的计算处理量进行付费。</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;那么，Amazon为什么会如此大张旗鼓地进入WEB服务领域呢？其实，在它多年运作电子商务的过程中，Amazon就积累了非常强的技术经验，而且也对其数据中心进行了极大规模的投资建设。现在，它只是把其中一些资产开放出来，让其他企业去使用；同时，许多CIO也不想在自己的IT基础架构上花费更多的钱，希望能找到一种替代品，所以，双方一拍即合。负责AWS产品管理和开发者关系的副总裁AdamSelipsky就说，&#8220;很多人可能不理解的是，Amazon从根本上来说并不是一家书店或零售商，而是一家技术型公司。&#8221;</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;因为不再需要事先部署软件和服务器，从个人到大企业的许多开发人员已经开始登录Amazon的AWS平台来开发和提供应用软件。目前，使用Amazon的AWS服务的开发人员已经有37万人之多。</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;AWS起初的目标是中小企业，而不是大企业，但现在一些大企业也开始对AWS产生了兴趣，毕竟它能够降低前期投资成本，有伸缩自如的可扩展性以及灵活的IT资源。为了更好地支持这些大客户，Amazon几个月前已经开始提供全天侯的电话支持以及企业级水准的服务。举个例子，在一个月内，如果S3的可用性降到了99.9%以下，那么，客户就能获得至少10%的信用额度。当然，Amazon并非完美，近期它的客户也正面临着WEB站点访问速度变慢等一系列问题。</div>
<div><br />
</div>
<div>&nbsp;&nbsp; &nbsp;Amazon没想把自己变成一家SaaS提供商，但很多初创型小企业和其他软件开发人员正在使用AWS提供他们各自的SaaS产品。这里面包括Vertica，销售基于S3的数据仓库，以及Sonian，把它的文档服务构建在Amazon的基础架构之上。</div>
<div><br />
</div>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/344548.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-02-17 14:21 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/02/17/344548.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何构建Apache Wink REST服务（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/02/15/344316.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Tue, 15 Feb 2011 02:06:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/02/15/344316.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/344316.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/02/15/344316.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/344316.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/344316.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://www.searchsoa.com.cn/showcontent_35288.htm　　首先简要介绍一下REST。REST代表Representational State Transfer，它是World Wide Web所依赖的一套架构原则。Roy Fielding在他的博士论文&#8220;Architectural Styles and the Design of...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2011/02/15/344316.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/344316.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-02-15 10:06 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/02/15/344316.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HTML5崛起 2011年五大网络前端技术展望（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342879.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Wed, 12 Jan 2011 07:54:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342879.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342879.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342879.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342879.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342879.html</trackback:ping><description><![CDATA[转自：http://labs.chinamobile.com/news/48136
<div>
<div style="float: left; width: 567px; margin-top: 15px; padding-top: 10px; padding-right: 10px; padding-bottom: 8px; padding-left: 15px; border-left-color: #ededed; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; line-height: 24px; color: #666666; font-family: 宋体, Arial, Helvetica, sans-serif; font-size: 12px; "><span style="color: #006797; font-size: 14px; ">内容摘要：</span><span style="color: #666666; font-size: 14px; "><a alt="" title="" style="color: #000000; text-decoration: none; ">2011年将会有什么web前端技术出现或者成熟？以下列举了5个web技术以及技术趋势，我们认为这些技术应该会在今年进入我们的视线范围，有些列举项虽然不是具体的web开发技术，但无论如何，都会或多或少的影响到我们。</a></span></div>
<div class="contentarea" style="width: 575px; margin-top: 20px; padding-top: 0px; padding-right: 8px; padding-bottom: 0px; padding-left: 10px; display: inline; font-size: 14px; overflow-x: hidden; float: left; color: #000000; line-height: 24px; font-family: 宋体, Arial, Helvetica, sans-serif; "><a style="color: #000000; text-decoration: none; ">
<p>　　刚刚过去的2010年里，我们看到了 HTML5技术的崛起，以及CSS3的日渐成熟，经历了Rails 3.0 新版本的推出，以及全球范围内采用的@font-face selector 技术（属于CSS3 部分内容）等等一些更多的新技术和新鲜事情。总之，2010年对于web前端开发人员来说是一个忙碌的一年。<br />
<br />
2011年将会有什么web前端技术出现或者成熟？以下列举了5个web技术以及技术趋势，我们认为这些技术应该会在今年进入我们的视线范围，有些列举项虽然不是具体的web开发技术，但无论如何，都会或多或少的影响到我们。<br />
<br />
1.&nbsp;<strong>jQuery Mobile</strong>&nbsp;jQuery 移动应用<br />
<br />
jQuery 已经成为JavaScript事实上的代名词，很大程度上是因为其可扩展性，易用性以及在项目管理方面可控。随着用于平板电脑和智能手机等设备的触屏优化技术的演进，以及与此相关的web框架的发展，jQuery Mobile 将成为跨平台，支持多设备的手机应用的非常重要的技术选择之一。<br />
<br />
越来越多的开发者开始考虑使用html5技术来开发移动应用，建立一个稳定的易用性好的JavaScript框架将成为移动技术开发人员的重要选择。jQuery Mobile具备成为这一框架的事实上的基础。（因jQuery目前的应用范围相当广泛和普遍）&nbsp;<br />
<br />
2.&nbsp;<strong>Hardware-Accelerated Web Browsers</strong>&nbsp;浏览器硬件加速技术<br />
<br />
由于ie9使用了 DirectX10以及硬件加速技术，大幅度提高了浏览器的速度和承载能力，因此Internet Explorer 9 (IE9) 目前相对于其他竞争对手来说速度奇快。Google 和 Mozilla 必然会争先抢后地使用这些相关技术譬如硬件加速技术来应对来自 IE9的压力和竞争。今年，我们大家的浏览器定会飞速，对我们大家绝对是利好消息。<br />
<br />
3.<strong>&nbsp;Node.js 技术以及服务端JavaScript技术</strong><br />
<br />
Node.js技术被很多网站描述成为 事件驱动的 JavaScript V8 引擎。本质上，他是一个工具，为了用JavaScript技术来编写服务端事件驱动的工具集。什么意思？简单的说，技术编程的时候，根本不需要等待 一个输入输出I/O 操作完成才能去进行另一个输入输出操作。对于 Node.js 来说，意味着它可以在服务端做所有数据运算等工作，减轻了客户端的压力。对于一些数据处理类的app应用来说，像是像 实时应用、搜索引擎web爬虫、文件上传，流媒体技术等等，速度是至关重要的首要环节。<br />
<br />
Node.js 作为web前端技术实现显示层扩展到使用服务端JavaScript技术，将在 2011年受到广泛的应用。想了解更多的技术原理等详情，请查看 Simon Willison 的博客。<br />
<br />
4.&nbsp;<strong>点击分享实时化 Real-Time Clickstream Sharing</strong>&nbsp;（点击分享后立即呈现该分享内容到社区或者网站等）<br />
<br />
有无数多的方法来实现将用户分享的内容呈现到互联网上，包括今年出现的一些社交网站分享或者一些在线分享或者在线发布工具等。随着大规模集成分享按钮的应用，以及逐渐采用客户端、weidget页面组件、和在线书签等的应用方式，这，将使分享越来越容易和简便。实时分享的下一方向将会是个人在线阅历或经验分享，在 2011年，这点将会决定互联网是否更加好用。<br />
<br />
5.&nbsp;<strong>No SQL Databases</strong>&nbsp;无需SQL语句的数据库技术<br />
<br />
NoSQL，是一个泛定义词汇，用来说明取代现有使用SQL的数据技术，这种NoSql技术不会采用目前已经广泛使用的传统关系型数据库技术。这种NoSQL技术近些年已经开始兴盛。无表数据库技术已经被Google Amazon 谷歌和亚马逊等巨头所关注，其取代技术有 Google的 BigTable技术和 亚马逊Amazon的 Dynamo技术，阿帕奇的Apache Cassandra, MongoDB, CouchDB, Voldemort 以及Riak技术等等。<br />
<br />
诚然，不同的技术实现不代表着新技术是更好的，我们肯定不会像扔掉小孩子一样轻易放弃原有技术，技术实现的过程总是有所取舍。就像Ted Dziuba wrote所说，&#8220;by replacing MySQL or Postgres with a different, new data store, you have traded a well-enumerated list of limitations and warts for a newer, poorly understood list of limitations and warts.&#8221;（大致意思为：选择取代 MySQL或者 Postgres数据库技术，而使用一个新的不同的技术方案，就意味着你已经使用了一个有很多局限性的不成熟技术，来取代另一个毛病多多的旧技术。）<br />
<br />
尽管如此，NoSQL技术在 2011年仍然将是唯一的一个取代目前关系型数据库的新技术，这个新技术在过去几年发展，一直朝着这个方向发展。</p>
</a></div>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/342879.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-12 15:54 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342879.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>揭秘全球最大网站Facebook背后的那些软件（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342876.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Wed, 12 Jan 2011 07:35:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342876.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342876.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342876.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342876.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342876.html</trackback:ping><description><![CDATA[转自：http://www.javaeye.com/news/16925
<div>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; "><strong style="font-weight: bold; ">2010年6月，Google公布全球Top 1000网站。Facebook独占鳌头。</strong><br />
&nbsp;</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-align: center; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; "><img src="http://dl.javaeye.com/upload/attachment/278864/d55098b6-32f9-39c6-921e-ee838a910df0.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-align: center; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; ">&nbsp;</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; ">以Facebook现在的经营规模，诸多传统服务器的技术均将崩溃或根本无法支撑。那么面对5亿的活跃用户，Facebook的工程师们又将如何让网站平稳运转呢？伯乐在线 - 职场博客的这篇文章将展示Facebook的工程师完成这个艰巨任务所用到的一系列软件。<br />
<br />
<br />
&nbsp;<span style="font-size: medium; "><strong style="font-weight: bold; ">Facebook级别规模的挑战</strong></span><br />
<br />
在我们深入细节之前，先了解一组Facebook不得不面对数据，你就可以想象这种规模。</p>
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; ">
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Facebook每月的PV量：630,000,000,000 （6万3千亿）</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Facebook上的图片数量超过其他图片网站的总和（包括诸如Flickr这样的图片网站）</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">每个月有超过30亿的图片上传到Facebook</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Facebook系统每秒可以处理120万张图片。这还不包括Facebook的CDN处理的图片。</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">每月处理超过250亿的信息内容（包括用户状态更新，评论等）</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Facebook的服务器数量超过3万台（此数据为2009年的数据）</li>
</ul>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; "><br />
<br />
&nbsp;<span style="font-size: medium; "><strong style="font-weight: bold; ">Facebook所用的软件</strong></span><br />
<br />
从某些方面来说，Facebook还是属于LAMP类型网站，但是，为了配合其他大量的组件和服务，Facebook对已有的方法，已经做了必要的改变、拓展和修改。<br />
<br />
比如：</p>
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; ">
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Facebook依然使用PHP，但Facebook已重建新的编译器，以满足在其Web服务器上加载本地代码，从而提升性能；</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Facebook使用Linux系统，但为了自身目的，也已做了必要的优化。（尤其是在网络吞吐量方面）；</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Facebook使用MySQL，但也对其做优化。</li>
</ul>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px; "><br />
还有定制的系统，比如， Haystack -- 高度可扩展的对象存储，用来处理Facebook的庞大的图片；Scribe -- Facebook的日志系统。<br />
<br />
下面展现给大家的是，全球最大的社交网站Facebook所使用到的软件。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">Memcached&nbsp;</strong></span>&nbsp;<img src="http://dl.javaeye.com/upload/attachment/278866/392adbec-35d9-3454-817e-dd2e4286e13b.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
Memcached是一款相当有名的软件。它是分布式内存缓存系统。Facebook(还有大量的网站)用它作为Web服务器和MySQL服务器之间的缓存层。经过多年，Facebook已在Memcached和其相关软件（比如，网络栈）上做了大量优化工作。<br />
<br />
Facebook运行着成千上万的Memcached服务器，借以及时处理TB级的缓存数据。可以这样说，Facebook拥有全球最大的Memcached设备。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">HipHop for PHP&nbsp;</strong></span>&nbsp;<img src="http://dl.javaeye.com/upload/attachment/278868/d040485c-4fe5-31a6-a717-fff07afc84a6.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
和运行在本地服务器上代码相比，PHP的运行速度相对较慢。HipHop把PHP代码转换成C++代码，提高编译时的性能。因为Facebook很依赖PHP来处理信息，有了HipHop，Facebook在Web服务器方面更是如虎添翼。<br />
<br />
HipHop诞生过程：在Facebook，一小组工程师（最初是3位）用了18个月研发而成。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">Haystack&nbsp;</strong></span>&nbsp;<img src="http://dl.javaeye.com/upload/attachment/278870/e4de0499-ed77-374a-b103-0437781b39a3.jpg" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
Haystack是Facebook高性能的图片存储/检索系统。（严格来说，Haystack是一对象存储，所以它不一定要存储图片。）Haystack的工作量超大。Facebook上有超过2百亿张图片，每张图片以四种不同分辨率保存，所以，Facebook有超过8百亿张图片。<br />
<br />
Haystack的作用不单是处理大量的图片，它的性能才是亮点。我们在前面已提到，Facebook每秒大概处理120万张图片，这个数据并不包括其CDN处理的图片数。这可是个惊人的数据！！！<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">BigPipe&nbsp;</strong></span>&nbsp;<img src="http://dl.javaeye.com/upload/attachment/278872/3a895df7-b738-30d3-8a15-5c4b7ac8220f.jpg" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
BigPipe是Facebook开发的动态网页处理系统。为了达到最优，Facebook用它来处理每个网页的分块（也称&#8220;Pagelets&#8221;）。<br />
<br />
比如，聊天窗口是独立检索的，新闻源也是独立检索的。这些Pagelets是可以并发检索，性能也随之提高。如此，即使网站的某部分停用或崩溃后，用户依然可以使用。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">Cassandra&nbsp;</strong></span>&nbsp;<img src="http://dl.javaeye.com/upload/attachment/278874/2d78cb30-c9d4-3783-af8e-538ce9442cf9.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
Cassandra是一个没有单点故障的分布式存储系统。它是前NoSQL运动的成员之一，现已开源（已加入Apache工程）。Facebook用它来做邮箱搜索。<br />
<br />
除了Facebook之外，Cassandra也适用于很多其他服务，比如Digg。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">Scribe&nbsp;</strong></span>&nbsp;<img src="http://dl.javaeye.com/upload/attachment/278876/b0e0e5b3-e604-37e1-9ecd-eb41a26a84dc.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
Scribe是个灵活多变的日志系统，Facebook把它用于多种内部用途。Scribe用途：处理Facebook级别日志，一旦有新的日志分类生成，Scribe将自动处理。（Facebook有上百个日志分类）。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">Hadoop and Hive</strong></span><img src="http://dl.javaeye.com/upload/attachment/278878/9d008846-7a72-32f2-ab7b-d90bc2dc5535.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
Hadoop是款开源Map/Reduce框架，它可以轻松处理海量数据。Facebook用它来做数据分析。（前面就说到了，Facebook的数据量是超海量的。）Hive起源于Facebook，Hive可以使用SQL查询，让非程序员比较容易使用Hadoop。（注1: Hive是是基于Hadoop的一个数据仓库工具，可以将结构化的数据文件映射为一张数据库表，并提供完整的sql查询功能，可以将sql语句转换为 MapReduce任务进行运行。 ）<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">Thrift</strong></span>&nbsp;<br />
<br />
Facebook在其不同的服务中，使用了不同的语言。比如: PHP用在前端，Erlang用于聊天系统，Java和C++用于其它地方，等等。Thrift是内部开发的跨语言的框架，把不同的语言绑定在一起，使之可以相互&#8220;交流&#8221;。这就让Facebook的跨语言开发，变得比较轻松。<br />
<br />
Facebook已把Thrift开源，Thrift支持的语言种类将更多。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">Varnish&nbsp;</strong></span>&nbsp;<img src="http://dl.javaeye.com/upload/attachment/278880/f3487ae6-f9ab-38cd-9392-0f09f02c29ef.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />
&nbsp;<br />
<br />
Varnish是一个HTTP加速器，担当负载均衡角色，同时也用于快速处理缓存内容。<br />
<br />
Facebook用Varnish处理图片和用户照片，每天都要处理十亿级的请求。和Facebook其他的应用应用一样，Varnish也是开源的。<br />
<br />
&nbsp;<span style="font-size: medium; "><strong style="font-weight: bold; ">Facebook可以平稳运行，还得利于其他方面</strong></span><br />
<br />
虽然上面已经提到了一些构成Facebook系统的软件，但是处理如此庞大的系统，本身就是一项复杂的任务。所以，下面还将列出使Facebook能平稳运行的一些东西。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">逐步发布&amp;暗启动</strong></span><br />
<br />
Facebook有一个系统，他们称之为&#8220;门卫&#8221;。该系统可以针对不同种类的用户运行不同的代码。（它简单介绍了代码库中的不同条件。）该系统让Facebook逐步发布新特性、A/B测试、激活仅针对Facebook员工的特性 等等。<br />
<br />
门卫系统也让Facebook做些&#8220;暗启动&#8221;的事情。比如，在某一特性上线之前，可以激活该特性背后的元件。另外，它还可以做模拟压力测试，发现瓶颈和潜在的问题。默默启动一般都是在正式启动之前的2周完成。<br />
<br />
&nbsp;<strong style="font-weight: bold; "><span style="font-size: small; ">实时系统的简介</span></strong><br />
<br />
Facebook会仔细监控自身系统，有趣的是，它还监控每个PHP函数在实时生产环境下的性能。这一实时PHP环境监控是通过一个叫XHProf的开源工具完成的。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">逐步禁用某些特性，借以提高性能</strong></span><br />
<br />
如果Facebook遇到性能问题，Facebook有大量的途径来逐步禁用不很重要的特性，以提高其核心特性性能。<br />
<br />
&nbsp;<span style="font-size: small; "><strong style="font-weight: bold; ">尚未提到的东西</strong></span><br />
<br />
虽然这里无法过多深入硬件方面，但硬件绝对是Facebook能达到空前规模的重要因素。比如，和其他大型网站一样，Facebook也用CDN来处理静态内容。Facebook还在美国西部的俄勒冈州建有一超大的数据中心，可以随时增加服务器。<br />
<br />
当然了，除了前面已经提到的，还有其他大量的软件没有说到。但是，希望能突出其中非常有特色的。<br />
<br />
&nbsp;<strong style="font-weight: bold; "><span style="font-size: medium; ">Facebook和开源之间的&#8220;恋情&#8221;</span></strong><br />
<br />
Facebook和开源之间联系，此文不能不提，虽不能说Facebook是多么地钟爱开源，但至少可以这样说，Facebook是&#8220;爱&#8221;着开源的。<br />
<br />
Facebook不仅使用（也捐赠）开源软件，比如，Linux、Memcached、MySQL、Hadoop等等，它还内部开发不少软件，并且也将之开源。<br />
<br />
Facebook开发的开源工程，包括HipHop、Cassandra、Thrift和Scribe。另外，Facebook也把Tornado开源了。Tornado是一个高性能的Web服务器框架，由FriendFeed幕后团队开发而成。（2009年8月，Facebook收购 FriendFeed。）<br />
<br />
（Facebook所用到的开源软件，可以在Facebook的开源页面找到。）<br />
<br />
&nbsp;<span style="font-size: medium; "><strong style="font-weight: bold; ">面临更多的大规模挑战</strong></span><br />
<br />
Facebook以一种令人难以置信的速度成长。它的用户群几乎是成倍增加，活跃用户数量现已接近5亿。而且，谁都无法预测今年底，活跃用户量会到多少。<br />
<br />
Facebook甚至成立了一个专门的&#8220;成长小组&#8221;，该小组不断思考如何让人们使用facebook并融入到facebook中。<br />
<br />
这一快速成长，意味着Facebook将遇到不同的性能瓶颈。Facebook会面临来这如下方面的挑战：PV、搜索、上传的图片和状态消息，用户之间的交互和用户和Facebook之间的交互带来的挑战。<br />
<br />
这也是Facebook面对的事实。Facebook的工程师们将继续寻求新方法来扩展（这不只是增加服务器的问题了）。比如，随着网站成长，其图片存储系统已经多次完全重写。<br />
<br />
所以，我们将看到Facebook的工程师们奔向下一个&#8220;山头&#8221;。我们相信他们不会辜负众望。毕竟，他们正跨越山头，那个我们大多数人仅能向往的山头；他们正扩展网站，那个用户来自全球各地的网站。当你实现那个里程碑时，你将彪炳史册。</p>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/342876.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-12 15:35 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342876.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HBase vs Cassandra: 我们迁移系统的原因（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342852.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Wed, 12 Jan 2011 06:23:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342852.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342852.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342852.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342852.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342852.html</trackback:ping><description><![CDATA[转自：http://wangxu.me/blog/?p=371
<div><span  style="color: #242626; font-size: 13px; font-family: 'Helvetica Neue', Arial, 'Lucida Grande', 'Lucida Sans Unicode', 'Microsoft YaHei', sans-serif; line-height: 20px; ">
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">原文:&nbsp;<a title="http://ria101.wordpress.com/2010/02/24/hbase-vs-cassandra-why-we-moved/" href="http://ria101.wordpress.com/2010/02/24/hbase-vs-cassandra-why-we-moved/" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">http://ria101.wordpress.com/2010/02/24/hbase-vs-cassandra-why-we-moved/</a>&nbsp;<br />
原作者：Dominic Williams&nbsp;<br />
原文发布日期：February 24, 2010 at 7:27 pm&nbsp;<br />
译者：王旭（<a href="http://wangxu.me/blog/" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">http://wangxu.me/blog/</a>&nbsp;, @gnawux）&nbsp;<br />
翻译时间：2010年3月21-25日</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">我的团队近来正在忙于一个全新的产品——即将发布的网络游戏&nbsp;<a href="http://translate.googleusercontent.com/translate_c?hl=en&amp;ie=UTF-8&amp;sl=en&amp;tl=zh-CN&amp;u=http://www.fightmymonster.com/&amp;prev=_t&amp;rurl=translate.google.com&amp;twu=1&amp;usg=ALkJrhh_x7GYSmfh1ly73ymLlymT_aahvQ" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">www.FightMyMonster.com</a>。这让我们得以奢侈地去构建一个全新的 NOSQL 数据库，也就是说，我们可以把恐怖的 MySQL sharding 和昂贵的可伸缩性抛在脑后了。最近有很多人一直在问，为什么我们要把注意力从 HBase 上转移到 Cassandra 上去。我确认，确实有这样的变化，实际上我们基本上已经把代码移植到了 Cassandra 上了，这里我将给出解释。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">为了那些不熟悉 NOSQL 的读者，后面的其他文章中，我会介绍为什么我们将会在未来几年中看到地震式的从 SQL 到 NOSQL 的迁移，这正和向云计算的迁移一样重要。后面的文章还会尝试解释为什么我认为 NOSQL 可能会是贵公司的正确选择。不过本文我只是解释我们选择 Cassandra 作为我们的 NOSQL 解决方案的选择。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">免责声明——如果你正在寻找一个捷径来决定你的系统选择，你必须要明白，这可不是一个详尽而严格的比较，它只是概述了另一个初创团队在有限时间和资源的情况下的逻辑。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">Cassandra 的血统是否预言了它的未来</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">我最喜欢的一个工程师们用来找 bug 的谒语是&#8220;广度优先而非深度优先&#8221;。这可以可能对那些解决技术细节的人来说很恼人，因为它暗示着如果他们只是看看的话，解决方法就会简单很多（忠告：只对那些能够原谅你的同事说这个）。我造出这个谒语的原因在于，我发现，软件问题中，如果我们强迫我们自己在进入某行代码的细节层面之前，先去看看那些高层次的考虑的话，可以节省大量时间。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">所以，在谈论技术之前，我在做 HBase 和 Cassandra 之间的选择问题上先应用一下我的箴言。我们选择切换的技术结论可能已经可以预测了：Hbase和Cassandra有着迥异的血统和基因，而我认为这会影响到他们对我们的业务的适用性。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">严格的说，Hbase 和它的支持系统源于著名的 Google BigTable 和 Google 文件系统设计（GFS 的论文发于 2003 年，BigTable 的论文发于 2006 年）。而 Cassandra 则是最近 Facebook 的数据库系统的开源分支，她在实现了 BigTable 的数据模型的同时，使用了基于 Amazon 的 Dynamo 的系统架构来存储数据（实际上，Cassandra 的最初开发工作就是由两位从 Amazon 跳槽到 Facebook 的 Dynamo 工程师完成的）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">在我看来，这些不同的历史也导致Hbase更加适合于数据仓库、大型数据的处理和分析（如进行Web页面的索引等），而 Cassandra 则更适合于实时事务处理和提供交互型数据。要进行系统研究来证明这个观点超出了本文的范畴，但我相信你在考虑数据库的时候总能发现这个差异的存在。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">注意：如果你在寻找一个简单的证明，你可以通过主要 committer 的关注点来进行验证：大部分 HBase 的 committer 都为 Bing 工作（M$ 去年收购了他们的搜索公司，并允许他们在数月之后继续提交开源代码）。与之对应，Cassandra 的主要 committer 来自 Rackspace，用来可以自由获得的支持先进的通用的 NOSQL 的解决方案，用来和 Google, Yahoo, Amazon EC2 等提供的那些锁定在专有的 NOSQL 系统的方案相抗衡。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">Malcolm Gladwell 会说只是根据这些背景的不同就可以简单地选择了 Cassandra。不过这是小马过河的问题。但当然，闭着眼睛就进行一个商业选择是相当困难的&#8230;&#8230;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">哪个 NOSQL数据库风头更劲?</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">另一个说服我们转向 Cassandra 的原因是我们社区中的大风向。如你所知，软件平台行业里，大者恒大——那些被普遍看好的平台，会有更多人聚集在这个平台周围，于是，从长远看，你可以得到更好的生态系统的支持（也就是说，大部分支持的软件可以从社区中获得，也有更多的开发者可以雇佣）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">如果从 HBase 开始时，我的印象就是它后面有巨大的社区力量，但我现在相信，Cassandra 更加强大。最初的印象部分来源于 StumpleUpon 和 Streamy 的两位 CTO 的两个非常有说服力的出色的讲演，他们是 Web 行业中两个在 Cassandra 成为一个可选系统之前的 HBase 的两个重要的贡献者，同时也部分来源于快速阅读了一篇名为&#8220;HBase vs Cassandra： NoSQL 战役！&#8221;的文章（大部分内容都被广泛证实了）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">势头是很难确证的，你不得不自己进行研究，不过我可以找到的一个重要的标志是 IRC 上的开发者动向。如果你在 freenode.org 上比较 #hbase 和 #cassandra 的开发这频道，你会发现 Cassandra 差不多在任何时候都有两倍的开发者在线。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">如果你用考虑 HBase 一般的时间来考察 Cassandra，你就能发现 Cassandra 的背后确实有非常明显的加速势头。你可能还会发现那些逐渐出现的鼎鼎大名，如 Twitter，他们也计划广泛使用 Cassandra（<a href="http://translate.googleusercontent.com/translate_c?hl=en&amp;ie=UTF-8&amp;sl=en&amp;tl=zh-CN&amp;u=http://nosql.mypopescu.com/post/407159447/cassandra-twitter-an-interview-with-ryan-king&amp;prev=_t&amp;rurl=translate.google.com&amp;twu=1&amp;usg=ALkJrhhAV1dF6U42J_vUIBl8TrGtPX8EmQ" target="_blank" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">这里</a>）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">注：Cassandra 的网站看起来比 HBase 的好看多了，但认真的说，这可能不仅是市场的趋势。继续吧。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">深入到技术部分: CAP 和 CA 与 AP 的神话</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">对于分布式系统，有个非常重要的理论（这里我们在讨论分布式数据库，我相信你注意到了）。这个理论被称为 CAP 理论，由 Inktomi 的 联合创始人兼首席科学家 Eric Brewer 博士提出。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">这个理论说明，分布式（或共享数据）系统的设计中，至多只能够提供三个重要特性中的两个——一致性、可用性和容忍网络分区。简单的说，一致性指如果一个人向数据库写了一个值，那么其他用户能够立刻读取这个值，可用性意味着如果一些节点失效了，集群中的分布式系统仍然能继续工作，而容忍分区意味着，如果节点被分割成两组无法互相通信的节点，系统仍然能够继续工作。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">Brewer教授是一个杰出的人物，许多开发者，包括 HBase 社区的很多人，都把此理论牢记在心，并用于他们的设计当中。事实上，如果你搜索线上的关于 HBase 和 Cassandra 比较的文章，你通常会发现，HBase 社区解释他们选择了 CP，而 Cassandra 选择了 AP ——毫无疑问，大多数开发者需要某种程度的一致性 （C）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">不过，我需要请你注意，事实上这些生命基于一个不完全的推论。CAP 理论仅仅适用于一个分布式算法（我希望 Brewer 教授可以统一）。但没有说明你不能设计一个系统，在其中的各种操作的底层算法选择上进行这种。所以，在一个系统中，确实一个操作职能提供这些特性中的两个，但被忽视的问题是在系统设计中，实际是可以允许调用者来选择他们的某个操作时需要哪些特性的。不仅如此，现实世界并不简单的划分为黑白两色，所有这些特性都可以以某种程度来提供。这就是 Cassandra。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">这点非常重要，我重申：Cassandra 的优点在于你可以根据具体情况来选择一个最佳的折衷，来满足特定操作的需求。Cassandra 证明，你可以超越通常的 CAP 理论的解读，而世界仍然在转动。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">我们来看看两种不同的极端。比如我必须从数据库中读取一个要求具有很高一致性的值，也就是说，我必须 100%保证能够读取到先前写入的最新的内容。在这种情况下，我可以通过指定一致性水平为&#8220;ALL&#8221;来从 Cassandra 读取数据，这时要求所有节点都有数据的一致的副本。这里我们不具有对任何节点失效和网络分裂的容错性。在另一个极端的方面，如果我不特别关心一致性，或仅仅就是希望最佳性能，我可以使用一致性级别&#8220;ONE&#8221;来访问数据。在这种情况下，从任意一个保存有这个副本的节点获取数据都可以——即使数据有三个副本，也并不在意其他两个有副本的节点是否失效或是否有不同，当然，这种情况下我们读到的数据可能不是最新的。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">不仅如此，你不必被迫生活在黑白世界中。比如，在我们的一个特定的应用中，重要的读写操作通常使用&#8220;QUORUM&#8221;一致性级别，这意味着大部分存有此数据的节点上的副本是一致的——我这里是个简要描述，具体写你的 Cassandra 程序之前最好还是仔细研究一下。从我们的视角看，这这提供了一个合理的节点失效与网络分裂的耐受性，同时也提供了很高的一致性。而在一般情况下，我们使用前面提到的&#8220;ONE&#8221;一致性级别，者可以提供最高的性能。就是这样。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">对我们来说，这是 Cassandra 的一个巨大的加分项目。我们不仅能轻易地调整我们的系统，也可以设计它。比如，当一定数量的节点失效或出现网络连接故障时，我们的大部分服务仍然可以继续工作，只有那些需要数据一致性的服务会失效。HBase并没有这么灵活，它单纯地追求系统的一个方面（CP），这让我再次看到了 SQL 开发者和查询优化人员们之间的那道隔阂——有些事情最好能够超越它，HBase！</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">In our project then, Cassandra has proven by far the most flexible system, although you may find your brain at first loses consistency when considering your QUORUMs.在我们的项目之后，卡桑德拉已被证明是迄今为止最灵活的系统，虽然你可能发现一致性第一失去你的大脑在考虑您的法定人数。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">在我们的项目中，Cassandra 已经证明了它是有史以来最灵活的系统，虽然你可能在对这个问题进行投票（QUORUM）的时候发现的大脑失去了一致性。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">什么时候单体会比模块化强？</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">Cassandra 和 HBase 的一个重要区别是， Cassandra 在每个节点是是一个单 Java 进程，而完整的 HBase 解决方案却由不同部分组成：有数据库进程本身，它可能会运行在多个模式；一个配置好的 hadoop HDFS 分布式文件系统，以及一个 Zookeeper 系统来协调不同的 HBase 进程。那么，这是否意味着 HBase 有更好的模块化结构呢？</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">虽然 HBase 的这种架构可能确实可以平衡不同开发团队的利益，在系统管理方面，模块化的 HBase 却无法视为一个加分项目。事实上，特别是对于一些小的初创公司，模块化倒是一个很大的负面因素。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">HBase的下层相当复杂，任何对此有疑惑的人应该读读 Google 的 GFS 和 BigTable 的论文。即使是在一个单一节点的伪分布式模式下来架设 HBase 也很困难——事实上，我曾经费力写过一篇快速入门的教程（如果你要试试HBase的话<a href="http://ria101.wordpress.com/2010/01/28/setup-hbase-in-pseudo-distributed-mode-and-connect-java-client/" target="_blank" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">看看这里</a>）。在这个指南里你可以看到，设置好 HBase 并启动它实际包含了两个不同系统的手工设置：首先是 hadoop HDFS，然后才是 HBase 本身。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">然后，HBase 的配置文件本身就是个怪兽，而你的设置可能和缺省的网络配置有极大的不同（在文章里我写了两个不同的Ubuntu的缺省网络设置，以及 EC2 里易变的 Elastic IP 和内部分配的域名）。当系统工作不正常的时候，你需要查看大量的日志。所有的需要修复的东西的信息都在日志里，而如果你是一个经验丰富的管理员的话，就能发现并处理问题。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">但是，如果是在生产过程中出现问题，而你又没有时间耐心查找问题呢？如果你和我们一样，只有一个小的开发团队却有远大的目标，没有经历去 7*24 的进行系统监控管理会怎么样呢？</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">严肃地说，如果你是一个希望学习 NoSQL 系统的高级 DB 管理员的话，那么选择 HBase。这个系统超级复杂，有灵巧双手的管理员肯定能拿到高薪。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">但是如果你们是一个向我们一样尽力去发现隧道尽头的小团队的话，还是等着听听别的闲话吧</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">胜在 Gossip</strong><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">！</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">Cassandra 是一个完全对称的系统。也就是说，没有主节点或像 HBase 里的 region server 这样的东西——每个节点的角色是完全一样的。不会有任何特定的节点或其他实体来充当协调者的角色，集群中的节点使用称为 &#8220;Cossip&#8221; 的纯 P2P 通信协议来协调他们的行为。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">对 Gossip 的详细描述和使用 Gossip 的模型超过了本文的内容，但 Cassandra 所采用的 P2P 通信模型都是论证过的，比如发现节点失效的消息传播到整个系统的时间，或是一个客户应用的请求被路由到保存数据的节点的时间，所有这些过程所消耗的时间都毫无疑问的非常的短。我个人相信，Cassandra 代表了当今最振奋的一种 P2P 技术，当然，这和你的 NOSQL 数据库的选择无关。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">那么，这个基于 Gossip 的架构究竟给 Cassandra 用户带来什么显示的好处呢。首先，继续我们的系统管理主体，系统管理变得简单多了。比如，增加一个新节点到系统中就是启动一个 Cassandra 进程并告诉它一个种子节点（一个已知的在集群中的节点）这么简单。试想当你的分布式集群可能运行在上百个节点的规模上的时候，如此轻易地增加新节点简直是难以置信。更进一步，当有什么出错的时候，你不需要考虑是哪种节点出了问题——所有节点都是一样的，这让调试成为了一个更加易于进行且可重复的过程。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">第二，我可以得出结论，Cassandra 的 P2P 架构给了它更好的性能和可用性。这样的系统中，负载可以被均衡地三步倒各个节点上，来最大化潜在的并行性，这个能力让系统面临网络分裂和节点失效的时候都能更加的无缝，并且节点的对称性防止了 HBase 中发现的那种在节点加入和删除时的暂时性的性能都懂（Cassandra 启动非常迅速，并且性能可以随着节点的加入而平滑扩展）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">如果你想寻找更多更多的证据，你会对一个原来一直关注 hadoop 的小组（应该对 HBase 更加偏爱）的报告很感兴趣&#8230;&#8230;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">一份报告胜过千言万语。</strong><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">我是指图表</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">Yahoo！进行的第一个 NOSQL 系统的完整评测。研究似乎证实了 Cassandra 所享有的性能优势，从图表上看，非常倾向于 Cassandra。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">目前这些论文还是草稿，你可以从这里找到这些论文：&nbsp;<br />
<a href="http://www.brianfrankcooper.net/pubs/ycsb-v4.pdf" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">http://www.brianfrankcooper.net/pubs/ycsb-v4.pdf</a>&nbsp;<br />
<a href="http://www.brianfrankcooper.net/pubs/ycsb.pdf" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">http://www.brianfrankcooper.net/pubs/ycsb.pdf</a></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">注意：这份报告中 HBase 仅在对一个范围的记录进行扫描这一项上优于 Cassandra。虽然 Cassandra 团队相信他们可以很快达到 HBase 的时间，但还是值得指出，在通常的 Cassandra 配置中，区间扫描几乎是不可能的。我建议你可以无视这一点，因为实际上你应该在 Cassandra 上面来实现你自己的索引，而非使用区间扫描。如果你对区间扫描和在 Cassandra 中存储索引相关问题有兴趣，可以看我的<a href="http://ria101.wordpress.com/2010/02/22/cassandra-randompartitioner-vs-orderpreservingpartitioner/" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">这篇文章</a>。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">最后一点： 这篇文章背后的 Yahoo！研究团队正尝试让它们的评测应用通过法律部门的评估，并将它发布给社区。如果他们成功的话，我当然希望他们成功，我们将能够看到一个持续的竞争场面，不论 HBase 还是 Cassandra 无疑都会进一步提高他们的性能。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">锁和有用的模块性</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">毫无疑问，你会从 HBase 阵营听到这样的声音：HBase 的复杂结构让它可以提供 Cassandra 的 P2P 架构无法提供的东西。其中一个例子可能就是 Hbase 提供给开发者行锁机制，而 Cassandra 则没有（在 HBase 中，因为数据副本发生在 hadoop 底层，行锁可以由 region server 控制，而在 Cassandra 的 P2P 架构中，所有节点都是平等的，所以也就没有节点可以像一个网管囊样负责锁定有副本的数据）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">不够，我还是把这个问题返回到关于模块化的争论中，这实际是对 Cassandra 有理的。Cassandra 通过在对称节点上分布式存储数据来实现了 BigTable 的数据模型。它完整地实现了这些功能，而且是以最灵活和高性能的方式实现的。但如果你需要锁、事务和其它功能的话，这些可以以模块的方式添加到你的系统之中——比如，我们发现我们可以使用 Zookeeper 和相关的工具来很简单地为我们的应用提供可扩展的锁功能（对于这个功能，Hazelcast 等系统可能也可以实现这个功能，虽然我们没有进行研究）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">通过为一个窄领域目的来最小化它的功能，对我来说，Cassandra 的设计达到了它的目的——比如前面指出可配置的 CAP 的折衷。这种模块性意味着你可以依据你的需求来构建一个系统——需要锁，那么拿来 Zookeeper，需要存储全文索引，拿来&nbsp;<a href="http://go2.wordpress.com/?id=725X1342&amp;site=ria101.wordpress.com&amp;url=http%3A%2F%2Fblog.sematext.com%2F2010%2F02%2F09%2Flucandra-a-cassandra-based-lucene-backend%2F" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #004d99; ">Lucandra</a>&nbsp;，等等。对于我们这样的开发者来说，这意味着我们不必部署复杂度超出我们实际需要的系统，给我们提供了更加灵活的构建我们需要的应用的终极道路。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">MapReduce，别提 MapReduce！</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">Cassandra 做的还不够好的一件事情就是 MapReduce！对于不精通此项技术同学简单的解释一句，这是一个用于并行处理大量数据的系统，比如从上百万从网络上抓取的页面提取统计信息。MapReduce 和相关系统，比如 Pig 和 Hive 可以和 HBase 一起良好协作，因为它使用 HDFS 来存储数据，这些系统也是设计用来使用 HDFS 的。如果你需要进行这样的数据处理和分析的话，HBase 可能是你目前的最佳选择。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">记住，这就像小马过河！</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">因此，我停止了对 Cassandra&nbsp; 的优点的赞美，实际上，HBase 和 Cassandra 并不一定是一对完全的竞争对手。虽然它们常常可以用于同样的用途，和 MySQL 和 PostgreSQL 类似，我相信在将来它们将会成为不同应用的首选解决方案。比如，据我所知 StumbleUpon 使用了 HBase 和 hadoop MapReduce 技术，来处理其业务的大量数据。Twitter 现在使用 Cassandra 来存储实时交互的社区发言，可能你已经在某种程度上使用它了。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">作为一个有争议的临别赠言，下面我们进入下一个话题。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">注意：在继续下一个小节之前，我要指出，Cassandra 在 0.6 版本会有 hadoop 支持，所以 MapReduce 整合能获得更好的支持。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; "><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2e2e2e; ">兄弟，我不能失去数据&#8230;</strong></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">作为先前 CAP 理论争议的一个可能结果，可能有这样的印象，HBase 的数据似乎比 Cassandra 中的数据更安全。这是我希望揭露的最后一个关于 Cassandra 的秘密，当你写入新数据的时候，它实际上立刻将它写入一个将要存储副本的仲裁节点的 commit log 当中了，也被复制到了节点们的内存中。这意味着如果你完全让你的集群掉电，只可能会损失极少数据。更进一步，在系统中，通过使用 Merkle tree 来组织数据的过分不一致（数据熵），更加增加了数据的安全性<img alt="：）" src="http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; max-width: 576px; " /></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">事实上，我对 HBase 的情况并不是非常确切——如果能有更细节的情况，我回尽快更新这里的内容的——但现在我的理解是，因为 hadoop 还不支持 append，HBase 不能有效地将修改的块信息刷入 HDFS （新的对数据变化会被复制为多个副本并永久化保存）。这意味着会有一个更大的缺口，你最新的更改是不可见的（如果我错了，可能是这样，请告诉我，我回修正本文）。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">所以，尽管希腊神话中的 Cassandra 非常不幸（译注：Cassandra 是希腊神话里，特洛伊的那个可怜的女先知的名字，如果你不知道详情的话，可以参考wiki），但你的 Cassandra 中的数据不会有危险。</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #4d4d4d; ">注意：Wade Amold 指出， hadoop .21 很快就会发布，其中将会解决 HBase 的这个问题。</p>
</span></div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/342852.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-12 14:23 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342852.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>visual studio 2005 下安装使用boost库（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342821.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Wed, 12 Jan 2011 02:33:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342821.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342821.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342821.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342821.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342821.html</trackback:ping><description><![CDATA[转自：http://jinyun2012.blog.sohu.com/155691639.html
<div><span  style="line-height: 22px; color: #6d5335; font-family: Verdana, Arial, Helvetica, 宋体, sans-serif; font-size: 14px; ">
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">下载boost最新库&nbsp;</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">安装:<br style="line-height: 25px; " />
1、打开vs2005在菜单tools中选择Visual Studio 2005 Command Prompt，打开已配置好环境的命令行。<br style="line-height: 25px; " />
2、进入目录boost_1_34_1\libs\regex\build，<br style="line-height: 25px; " />
编译文件：nmake -f vc8.mak<br style="line-height: 25px; " />
安装(将编译好的文件复制到vs2005的特定目录下)：nmake -f vc8.mak install<br style="line-height: 25px; " />
删除临时文件：nmake -f vc8.mak clean<br style="line-height: 25px; " />
3、Tools-&gt;Options-&gt;Projects and Solutions-&gt;VC++ Directories-&gt;Include files添加boost_1_34_1路径</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">&nbsp;</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">&nbsp;在vs2005中编写如下代码</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">#include "stdafx.h"<br style="line-height: 25px; " />
#include &lt;cstdlib&gt;<br style="line-height: 25px; " />
#include &lt;stdlib.h&gt;<br style="line-height: 25px; " />
#include &lt;boost/regex.hpp&gt;<br style="line-height: 25px; " />
#include &lt;string&gt;<br style="line-height: 25px; " />
#include &lt;iostream&gt;<br style="line-height: 25px; " />
using namespace std;<br style="line-height: 25px; " />
using namespace boost;</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*)");<br style="line-height: 25px; " />
int main(int argc, char* argv[])<br style="line-height: 25px; " />
{<br style="line-height: 25px; " />
&nbsp;std::string in;<br style="line-height: 25px; " />
&nbsp;cmatch what;<br style="line-height: 25px; " />
&nbsp;cout &lt;&lt; "enter test string" &lt;&lt; endl;<br style="line-height: 25px; " />
&nbsp;getline(cin,in);<br style="line-height: 25px; " />
&nbsp;if(regex_match(in.c_str(), what, expression))<br style="line-height: 25px; " />
&nbsp;{<br style="line-height: 25px; " />
&nbsp;&nbsp;for(int i=0;i&lt;what.size();i++)<br style="line-height: 25px; " />
&nbsp;&nbsp;&nbsp;cout&lt;&lt;"str :"&lt;&lt;what[i].str()&lt;&lt;endl;<br style="line-height: 25px; " />
&nbsp;}<br style="line-height: 25px; " />
&nbsp;else<br style="line-height: 25px; " />
&nbsp;{<br style="line-height: 25px; " />
&nbsp;&nbsp;cout&lt;&lt;"Error Input"&lt;&lt;endl;<br style="line-height: 25px; " />
&nbsp;}<br style="line-height: 25px; " />
&nbsp;return 0;<br style="line-height: 25px; " />
}</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; "></font>&nbsp;</p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">在提示下输入</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">enter test string<br style="line-height: 25px; " />
select name from table</font></p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; "></font>&nbsp;</p>
<p style="line-height: 25px; color: #6d5335; margin-top: 10px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "><font face="Verdana" style="line-height: 25px; ">得到如下结果，具体为什么自己认真分析<br style="line-height: 25px; " />
str :select name from table<br style="line-height: 25px; " />
str :name<br style="line-height: 25px; " />
str :table</font></p>
</span></div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/342821.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-12 10:33 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/12/342821.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive 入门（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/05/342354.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Wed, 05 Jan 2011 07:03:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/05/342354.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342354.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/05/342354.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342354.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342354.html</trackback:ping><description><![CDATA[<br />
<div>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; "><strong><span  style="color: #000000; font-family: Times; font-weight: normal; line-height: normal; font-size: medium; ">转自：http://www.zypx.cn/technology/20100606132430320046.html</span></strong></p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; "><strong>Hive 是什么</strong></p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">在接触一个新的事物首先要回到的问题是：这是什么？</p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">这里引用 Hive wiki 上的介绍：</p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; "><em>Hive is a data warehouse infrastructure built on top of Hadoop. It provides tools to enable easy data ETL, a mechanism to put structures on the data, and the capability to querying and analysis of large data sets stored in Hadoop files. Hive defines a simple SQL-like query language, called QL, that enables users familiar with SQL to query the data. At the same time, this language also allows programmers who are familiar with the MapReduce fromwork to be able to plug in their custom mappers and reducers to perform more sophisticated analysis that may not be supported by the built-in capabilities of the language.</em></p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">Hive 是建立在 Hadoop 上的数据仓库基础构架。它提供了一系列的工具，可以用来进行数据提取转化加载（ETL），这是一种可以存储、查询和分析存储在 Hadoop 中的大规模数据的机制。Hive 定义了简单的类 SQL 查询语言，称为 QL，它允许熟悉 SQL 的用户查询数据。同时，这个语言也允许熟悉 MapReduce 开发者的开发自定义的 mapper 和 reducer 来处理内建的 mapper 和 reducer 无法完成的复杂的分析工作。</p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; "><em>Hive does not mandate read or written data be in the &#8220;Hive format&#8221;—there is no such thing. Hive works equally well on Thrift, control delimited, or your specialized data formats. Please see File Format and SerDe in Developer Guide for details.<br />
</em><br />
Hive 没有专门的数据格式。 Hive 可以很好的工作在 Thrift 之上，控制分隔符，也允许用户指定数据格式。</p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; "><strong>Hive 资源</strong></p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">Hive 本身提供了较丰富的文档，以下链接提供了 Hive 的一些基础文档：</p>
<ul style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">
    <li>FaceBook 镜像（被墙）：[[http://mirror.facebook.com/facebook/hive]]</li>
    <li>Wiki 页面：[[http://wiki.apache.org/hadoop/Hive]]</li>
    <li>入门指南：[[http://wiki.apache.org/hadoop/Hive/GettingStarted]]</li>
    <li>查询语言指南：[[http://wiki.apache.org/hadoop/Hive/HiveQL]]</li>
    <li>演示文稿：[[http://wiki.apache.org/hadoop/Hive/Presentations]]</li>
    <li>蓝图：[[http://wiki.apache.org/hadoop/Hive/Roadmap]]</li>
</ul>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">大多数有关 Hive 的使用和特性的问题可以从以上的链接中寻找到答案。当然，由于 Hive 本身在不断的发展中，文档的更新速度很多时候都赶不上 Hive 本身的更新速度，若希望了解 Hive 的最新动态或者遇到 Bug，可以加入 Hive 的邮件列表：<br />
* User: hive-user@hadoop.apache.org<br />
* Developer: hive-dev@hadoop.apache.org</p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; "><strong>Hive 的下载配置安装</strong><br />
请参考入门指南, 这里给出最基本的提纲:</p>
<ul style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">
    <li>安装配置 Hadoop。</li>
    <li>安装配置数据库（mysql 等）。</li>
    <li>获得 Hive 源码或者可执行代码。wget http://www.apache.org/dist/hadoop/hive/hive-0.5.0/hive-0.5.0-bin.tar.gz</li>
    <li>tar xzf hive-0.5.0-bin.tar.gz</li>
    <li>cd hive-0.5.0</li>
    <li>配置 Hive 如何访问数据库，如何访问 Hadoop。</li>
    <li>运行 Hive。</li>
</ul>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">当看到 Hive 提示符&#8216;Hive&gt;&#8217;的时候，恭喜，你可以开始你的 Hive 之旅了。</p>
<p style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 22px; ">
</p>
<h3>Hive 体系结构</h3>
<p>Hive 的结构如图所示，</p>
<p style="text-align: center; "><a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/hive-arch-edited.jpg" style="color: #333333; text-decoration: none; "><img height="424" alt="" src="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/hive-arch-edited.jpg" width="371" /></a></p>
<p>主要分为以下几个部分：</p>
<ul>
    <li>用户接口，包括 CLI，Client，WUI。</li>
    <li>元数据存储，通常是存储在关系数据库如 mysql, derby 中。</li>
    <li>解释器、编译器、优化器、执行器。</li>
    <li>Hadoop：用 HDFS 进行存储，利用 MapReduce 进行计算。</li>
</ul>
<ol>
    <li>用户接口主要有三个：CLI，Client 和 WUI。其中最常用的是 CLI，Cli 启动的时候，会同时启动一个 Hive 副本。Client 是 Hive 的客户端，用户连接至 Hive Server。在启动 Client 模式的时候，需要指出 Hive Server 所在节点，并且在该节点启动 Hive Server。 WUI 是通过浏览器访问 Hive。</li>
    <li>Hive 将元数据存储在数据库中，如 mysql、derby。Hive 中的元数据包括表的名字，表的列和分区及其属性，表的属性（是否为外部表等），表的数据所在目录等。</li>
    <li>解释器、编译器、优化器完成 HQL 查询语句从词法分析、语法分析、编译、优化以及查询计划的生成。生成的查询计划存储在 HDFS 中，并在随后有 MapReduce 调用执行。</li>
    <li>Hive 的数据存储在 HDFS 中，大部分的查询由 MapReduce 完成（包含 * 的查询，比如 select * from tbl 不会生成 MapRedcue 任务）。</li>
</ol>
<h3>Hive 元数据存储</h3>
<p>Hive 将元数据存储在 RDBMS 中，有三种模式可以连接到数据库：</p>
<ul>
    <li>Single User Mode： 此模式连接到一个 In-memory 的数据库 Derby，一般用于 Unit Test。<a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/metastore-single-mode.jpg" style="color: #333333; text-decoration: none; "><img height="307" alt="" src="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/metastore-single-mode.jpg" width="523" /></a></li>
    <li>Multi User Mode：通过网络连接到一个数据库中，是最经常使用到的模式。<a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/multi-user-mode.jpg" style="color: #333333; text-decoration: none; "><img height="307" alt="" src="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/multi-user-mode.jpg" width="523" /></a></li>
    <li>Remote Server Mode：用于非 Java 客户端访问元数据库，在服务器端启动一个 MetaStoreServer，客户端利用 Thrift 协议通过 MetaStoreServer 访问元数据库。<a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/remote-server-mode.jpg" style="color: #333333; text-decoration: none; "><img height="307" alt="" src="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/remote-server-mode.jpg" width="523" /></a></li>
</ul>
<h3>Hive 的数据存储</h3>
<p>首先，Hive 没有专门的数据存储格式，也没有为数据建立索引，用户可以非常自由的组织 Hive 中的表，只需要在创建表的时候告诉 Hive 数据中的列分隔符和行分隔符，Hive 就可以解析数据。</p>
<p>其次，Hive 中所有的数据都存储在 HDFS 中，Hive 中包含以下数据模型：Table，External Table，Partition，Bucket。</p>
<ol>
    <li>Hive 中的 Table 和数据库中的 Table 在概念上是类似的，每一个 Table 在 Hive 中都有一个相应的目录存储数据。例如，一个表 pvs，它在 HDFS 中的路径为：/wh/pvs，其中，wh 是在 hive-site.xml 中由 ${hive.metastore.warehouse.dir} 指定的数据仓库的目录，所有的 Table 数据（不包括 External Table）都保存在这个目录中。</li>
    <li>Partition 对应于数据库中的 Partition 列的密集索引，但是 Hive 中 Partition 的组织方式和数据库中的很不相同。在 Hive 中，表中的一个 Partition 对应于表下的一个目录，所有的 Partition 的数据都存储在对应的目录中。例如：pvs 表中包含 ds 和 city 两个 Partition，则对应于 ds = 20090801, ctry = US 的 HDFS 子目录为：/wh/pvs/ds=20090801/ctry=US；对应于 ds = 20090801, ctry = CA 的 HDFS 子目录为；/wh/pvs/ds=20090801/ctry=CA</li>
    <li>Buckets 对指定列计算 hash，根据 hash 值切分数据，目的是为了并行，每一个 Bucket 对应一个文件。将 user 列分散至 32 个 bucket，首先对 user 列的值计算 hash，对应 hash 值为 0 的 HDFS 目录为：/wh/pvs/ds=20090801/ctry=US/part-00000；hash 值为 20 的 HDFS 目录为：/wh/pvs/ds=20090801/ctry=US/part-00020</li>
    <li>External Table 指向已经在 HDFS 中存在的数据，可以创建 Partition。它和 Table 在元数据的组织上是相同的，而实际数据的存储则有较大的差异。</li>
</ol>
<ul>
    <li>Table 的创建过程和数据加载过程（这两个过程可以在同一个语句中完成），在加载数据的过程中，实际数据会被移动到数据仓库目录中；之后对数据对访问将会直接在数据仓库目录中完成。删除表时，表中的数据和元数据将会被同时删除。</li>
    <li>External Table 只有一个过程，加载数据和创建表同时完成（CREATE EXTERNAL TABLE &#8230;&#8230;LOCATION），实际数据是存储在 LOCATION 后面指定的 HDFS 路径中，并不会移动到数据仓库目录中。当删除一个 External Table 时，仅删除</li>
</ul>
<div><br />
</div>
<div>
<p>由于 Hive 采用了 SQL 的查询语言 HQL，因此很容易将 Hive 理解为数据库。其实<br />
从结构上来看，Hive 和数据库除了拥有类似的查询语言，再无类似之处。本文将<br />
从多个方面来阐述 Hive 和数据库的差异。数据库可以用在 Online 的应用中，但是<br />
Hive 是为数据仓库而设计的，清楚这一点，有助于从应用角度理解 Hive 的特性。</p>
<p>&nbsp;</p>
<table width="385" align="center" border="1" style="height: 378px; ">
    <tbody>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>Hive 和数据库的比较</h4>
            </td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>查询语言</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>HQL</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>SQL</h4>
            </td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>数据存储位置</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">HDFS</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">Raw Device 或者 Local FS</td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>数据格式</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">用户定义</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">系统决定</td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>数据更新</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">支持</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">不支持</td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>索引</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">无</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">有</td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>执行</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">MapRedcue</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">Executor</td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>执行延迟</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">高</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">低</td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>可扩展性</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">高</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">低</td>
        </tr>
        <tr>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
            <h4>数据规模</h4>
            </td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">大</td>
            <td style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">小</td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<ol>
    <li>查询语言。由于 SQL 被广泛的应用在数据仓库中，因此，专门针对 Hive 的特性设计了类 SQL 的查询语言 HQL。熟悉 SQL 开发的开发者可以很方便的使用 Hive 进行开发。</li>
    <li>数据存储位置。Hive 是建立在 Hadoop 之上的，所有 Hive 的数据都是存储在 HDFS 中的。而数据库则可以将数据保存在块设备或者本地文件系统中。</li>
    <li>数据格式。Hive 中没有定义专门的数据格式，数据格式可以由用户指定，用户定义数据格式需要指定三个属性：列分隔符（通常为空格、&#8221;\\t&#8221;、&#8221;\\x001&#8243;）、行分隔符（&#8221;\\n&#8221;）以及读取文件数据的方法（Hive 中默认有三个文件格式 TextFile，SequenceFile 以及 RCFile）。由于在加载数据的过程中，不需要从用户数据格式到 Hive 定义的数据格式的转换，因此，Hive 在加载的过程中不会对数据本身进行任何修改，而只是将数据内容复制或者移动到相应的 HDFS 目录中。而在数据库中，不同的数据库有不同的存储引擎，定义了自己的数据格式。所有数据都会按照一定的组织存储，因此，数据库加载数据的过程会比较耗时。</li>
    <li>数据更新。由于 Hive 是针对数据仓库应用设计的，而数据仓库的内容是读多写少的。因此，Hive 中不支持对数据的改写和添加，所有的数据都是在加载的时候中确定好的。而数据库中的数据通常是需要经常进行修改的，因此可以使用<code><font face="新宋体">&nbsp;INSERT INTO ...&nbsp; VALUES&nbsp;</font></code>添加数据，使用<code><font face="新宋体">&nbsp;UPDATE ... SET&nbsp;</font></code>修改数据。</li>
    <li>索引。之前已经说过，Hive 在加载数据的过程中不会对数据进行任何处理，甚至不会对数据进行扫描，因此也没有对数据中的某些 Key 建立索引。Hive 要访问数据中满足条件的特定值时，需要暴力扫描整个数据，因此访问延迟较高。由于 MapReduce 的引入， Hive 可以并行访问数据，因此即使没有索引，对于大数据量的访问，Hive 仍然可以体现出优势。数据库中，通常会针对一个或者几个列建立索引，因此对于少量的特定条件的数据的访问，数据库可以有很高的效率，较低的延迟。由于数据的访问延迟较高，决定了 Hive 不适合在线数据查询。</li>
    <li>执行。Hive 中大多数查询的执行是通过 Hadoop 提供的 MapReduce 来实现的（类似 select * from tbl 的查询不需要 MapReduce）。而数据库通常有自己的执行引擎。</li>
    <li>执行延迟。之前提到，Hive 在查询数据的时候，由于没有索引，需要扫描整个表，因此延迟较高。另外一个导致 Hive 执行延迟高的因素是 MapReduce 框架。由于 MapReduce 本身具有较高的延迟，因此在利用 MapReduce 执行 Hive 查询时，也会有较高的延迟。相对的，数据库的执行延迟较低。当然，这个低是有条件的，即数据规模较小，当数据规模大到超过数据库的处理能力的时候，Hive 的并行计算显然能体现出优势。</li>
    <li>可扩展性。由于 Hive 是建立在 Hadoop 之上的，因此 Hive 的可扩展性是和 Hadoop 的可扩展性是一致的（世界上最大的 Hadoop 集群在 Yahoo!，2009年的规模在 4000 台节点左右）。而数据库由于 ACID 语义的严格限制，扩展行非常有限。目前最先进的并行数据库 Oracle 在理论上的扩展能力也只有 100 台左右。</li>
    <li>数据规模。由于 Hive 建立在集群上并可以利用 MapReduce 进行并行计算，因此可以支持很大规模的数据；对应的，数据库可以支持的数据规模较小。</li>
</ol>
<div><strong style="color: #5e58a2; font-size: 14px; ">Hive QL</strong></div>
</div>
<div><strong style="color: #5e58a2; font-size: 14px; "><span style="color: #333333; font-weight: normal; font-size: 12px; ">
<p>Hive 的官方文档中对查询语言有了很详细的描述，请参考：http://wiki.apache.org/hadoop/Hive/LanguageManual ，本文的内容大部分翻译自该页面，期间加入了一些在使用过程中需要注意到的事项。</p>
<h3>Create Table</h3>
<pre>CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type
[COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)]
INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
</pre>
<p>CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在，则抛出异常；用户可以用 IF NOT EXIST 选项来忽略这个异常。</p>
<p>EXTERNAL 关键字可以让用户创建一个外部表，在建表的同时指定一个指向实际数据的路径（LOCATION），Hive 创建内部表时，会将数据移动到数据仓库指向的路径；若创建外部表，仅记录数据所在的路径，不对数据的位置做任何改变。在删除表的时候，内部表的元数据和数据会被一起删除，而外部表只删除元数据，不删除数据。</p>
<p>LIKE 允许用户复制现有的表结构，但是不复制数据。</p>
<p>用户在建表的时候可以自定义 SerDe 或者使用自带的 SerDe。如果没有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED，将会使用自带的 SerDe。在建表的时候，用户还需要为表指定列，用户在指定表的列的同时也会指定自定义的 SerDe，Hive 通过 SerDe 确定表的具体的列的数据。</p>
<p>如果文件数据是纯文本，可以使用 STORED AS TEXTFILE。如果数据需要压缩，使用 STORED AS SEQUENCE 。</p>
<p>有分区的表可以在创建的时候使用 PARTITIONED BY 语句。一个表可以拥有一个或者多个分区，每一个分区单独存在一个目录下。而且，表和分区都可以对某个列进行 CLUSTERED BY 操作，将若干个列放入一个桶（bucket）中。也可以利用SORT BY 对数据进行排序。这样可以为特定应用提高性能。</p>
<p>表名和列名不区分大小写，SerDe 和属性名区分大小写。表和列的注释是字符串。</p>
<h3>Drop Table</h3>
<p>删除一个内部表的同时会同时删除表的元数据和数据。删除一个外部表，只删除元数据而保留数据。</p>
<h3>Alter Table</h3>
<p>Alter table 语句允许用户改变现有表的结构。用户可以增加列/分区，改变serde，增加表和 serde 熟悉，表本身重命名。</p>
<p><strong>Add Partitions</strong></p>
<pre>ALTER TABLE table_name ADD
partition_spec [ LOCATION 'location1' ]
partition_spec [ LOCATION 'location2' ] ...
partition_spec:
: PARTITION (partition_col = partition_col_value,
partition_col = partiton_col_value, ...)
</pre>
<p>用户可以用 ALTER TABLE ADD PARTITION 来向一个表中增加分区。当分区名是字符串时加引号。</p>
<pre>  ALTER TABLE page_view ADD
PARTITION (dt='2008-08-08', country='us')
location '/path/to/us/part080808'
PARTITION (dt='2008-08-09', country='us')
location '/path/to/us/part080809';
</pre>
<p><strong>DROP PARTITION</strong></p>
<pre>ALTER TABLE table_name DROP
partition_spec, partition_spec,...
</pre>
<p>用户可以用 ALTER TABLE DROP PARTITION 来删除分区。分区的元数据和数据将被一并删除。</p>
<pre>ALTER TABLE page_view
DROP PARTITION (dt='2008-08-08', country='us');
</pre>
<p><strong>RENAME TABLE</strong></p>
<pre>ALTER TABLE table_name RENAME TO new_table_name
</pre>
<p>这个命令可以让用户为表更名。数据所在的位置和分区名并不改变。换而言之，老的表名并未&#8220;释放&#8221;，对老表的更改会改变新表的数据。</p>
<p><strong>Change Column Name/Type/Position/Comment</strong></p>
<pre>ALTER TABLE table_name CHANGE [COLUMN]
col_old_name col_new_name column_type
[COMMENT col_comment]
[FIRST|AFTER column_name]
</pre>
<p>这个命令可以允许用户修改一个列的名称、数据类型、注释或者位置。</p>
<p>比如：</p>
<p><code><font face="新宋体">CREATE TABLE test_change (a int, b int, c int);</font></code></p>
<p><code><font face="新宋体">ALTER TABLE test_change CHANGE a a1 INT;</font></code>&nbsp;将 a 列的名字改为 a1.</p>
<p><code><font face="新宋体">ALTER TABLE test_change CHANGE a a1 STRING AFTER b;</font></code>&nbsp;将 a 列的名字改为 a1，a 列的数据类型改为 string，并将它放置在列 b 之后。新的表结构为： b int, a1 string, c int.</p>
<p><code><font face="新宋体">ALTER TABLE test_change CHANGE b b1 INT FIRST;</font></code>&nbsp;会将 b 列的名字修改为 b1, 并将它放在第一列。新表的结构为： b1 int, a string, c int.</p>
<p>注意：对列的改变只会修改 Hive 的元数据，而不会改变实际数据。用户应该确定保证元数据定义和实际数据结构的一致性。</p>
<p><strong>Add/Replace Columns</strong></p>
<pre>ALTER TABLE table_name ADD|REPLACE
COLUMNS (col_name data_type [COMMENT col_comment], ...)
</pre>
<p>ADD COLUMNS 允许用户在当前列的末尾增加新的列，但是在分区列之前。</p>
<p>REPLACE COLUMNS 删除以后的列，加入新的列。只有在使用 native 的 SerDE（DynamicSerDe or MetadataTypeColumnsetSerDe）的时候才可以这么做。</p>
<p><strong>Alter Table Properties</strong></p>
<pre>ALTER TABLE table_name SET TBLPROPERTIES table_properties
table_properties:
: (property_name = property_value, property_name = property_value, ... )
</pre>
<p>用户可以用这个命令向表中增加 metadata，目前 last_modified_user，last_modified_time 属性都是由 Hive 自动管理的。用户可以向列表中增加自己的属性。可以使用 DESCRIBE EXTENDED TABLE 来获得这些信息。</p>
<p><strong>Add Serde Properties</strong></p>
<pre>ALTER TABLE table_name
SET SERDE serde_class_name
[WITH SERDEPROPERTIES serde_properties]
ALTER TABLE table_name
SET SERDEPROPERTIES serde_properties
serde_properties:
: (property_name = property_value,
property_name = property_value, ... )
</pre>
<p>这个命令允许用户向 SerDe 对象增加用户定义的元数据。Hive 为了序列化和反序列化数据，将会初始化 SerDe 属性，并将属性传给表的 SerDe。如此，用户可以为自定义的 SerDe 存储属性。</p>
<p><strong>Alter Table File Format and Organization</strong></p>
<pre>ALTER TABLE table_name SET FILEFORMAT file_format
ALTER TABLE table_name CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name, ...)] INTO num_buckets BUCKETS
</pre>
<p>这个命令修改了表的物理存储属性。</p>
<h3>Loading files into table</h3>
<p>当数据被加载至表中时，不会对数据进行任何转换。Load 操作只是将数据复制/移动至 Hive 表对应的位置。</p>
<p><strong>Syntax:</strong></p>
<pre>LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE]
INTO TABLE tablename
[PARTITION (partcol1=val1, partcol2=val2 ...)]
</pre>
<p><strong>Synopsis:</strong></p>
<p>Load 操作只是单纯的复制/移动操作，将数据文件移动到 Hive 表对应的位置。</p>
<ul>
    <li>filepath 可以是：
    <ul>
        <li>相对路径，例如：project/data1</li>
        <li>绝对路径，例如： /user/hive/project/data1</li>
        <li>包含模式的完整 URI，例如：hdfs://namenode:9000/user/hive/project/data1</li>
    </ul>
    </li>
    <li>加载的目标可以是一个表或者分区。如果表包含分区，必须指定每一个分区的分区名。</li>
    <li>filepath 可以引用一个文件（这种情况下，Hive 会将文件移动到表所对应的目录中）或者是一个目录（在这种情况下，Hive 会将目录中的所有文件移动至表所对应的目录中）。</li>
    <li>如果指定了 LOCAL，那么：
    <ul>
        <li>load 命令会去查找本地文件系统中的 filepath。如果发现是相对路径，则路径会被解释为相对于当前用户的当前路径。用户也可以为本地文件指定一个完整的 URI，比如：file:///user/hive/project/data1.</li>
        <li>load 命令会将 filepath 中的文件复制到目标文件系统中。目标文件系统由表的位置属性决定。被复制的数据文件移动到表的数据对应的位置。</li>
    </ul>
    </li>
    <li>如果没有指定 LOCAL 关键字，如果 filepath 指向的是一个完整的 URI，hive 会直接使用这个 URI。 否则：
    <ul>
        <li>如果没有指定 schema 或者 authority，Hive 会使用在 hadoop 配置文件中定义的 schema 和 authority，fs.default.name 指定了 Namenode 的 URI。</li>
        <li>如果路径不是绝对的，Hive 相对于 /user/ 进行解释。</li>
        <li>Hive 会将 filepath 中指定的文件内容移动到 table （或者 partition）所指定的路径中。</li>
    </ul>
    </li>
    <li>如果使用了 OVERWRITE 关键字，则目标表（或者分区）中的内容（如果有）会被删除，然后再将 filepath 指向的文件/目录中的内容添加到表/分区中。</li>
    <li>如果目标表（分区）已经有一个文件，并且文件名和 filepath 中的文件名冲突，那么现有的文件会被新文件所替代。</li>
</ul>
<h3>SELECT</h3>
<p><strong>Syntax</strong></p>
<pre>SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[
CLUSTER BY col_list
| [DISTRIBUTE BY col_list]
[SORT BY col_list]
]
[LIMIT number]
</pre>
<ul>
    <li>一个SELECT语句可以是一个union查询或一个子查询的一部分。</li>
    <li>table_reference是查询的输入，可以是一个普通表、一个视图、一个join或一个子查询</li>
    <li>简单查询。例如，下面这一语句从t1表中查询所有列的信息。</li>
</ul>
<pre>SELECT * FROM t1
</pre>
<p><strong>WHERE Clause</strong></p>
<p>where condition 是一个布尔表达式。例如，下面的查询语句只返回销售记录大于 10，且归属地属于美国的销售代表。Hive 不支持在WHERE 子句中的 IN，EXIST 或子查询。</p>
<pre>SELECT * FROM sales WHERE amount &gt; 10 AND region = "US"
</pre>
<p><strong>ALL and DISTINCT Clauses</strong></p>
<p>使用ALL和DISTINCT选项区分对重复记录的处理。默认是ALL，表示查询所有记录。DISTINCT表示去掉重复的记录。</p>
<pre>hive&gt; SELECT col1, col2 FROM t1
1 3
1 3
1 4
2 5
hive&gt; SELECT DISTINCT col1, col2 FROM t1
1 3
1 4
2 5
hive&gt; SELECT DISTINCT col1 FROM t1
1
2
</pre>
<p><strong>基于Partition的查询</strong></p>
<p>一般 SELECT 查询会扫描整个表（除非是为了抽样查询）。但是如果一个表使用 PARTITIONED BY 子句建表，查询就可以利用分区剪枝（input pruning）的特性，只扫描一个表中它关心的那一部分。Hive 当前的实现是，只有分区断言出现在离 FROM 子句最近的那个WHERE 子句中，才会启用分区剪枝。例如，如果 page_views 表使用 date 列分区，以下语句只会读取分区为&#8216;2008-03-01&#8217;的数据。</p>
<pre> SELECT page_views.*
FROM page_views
WHERE page_views.date &gt;= '2008-03-01'
AND page_views.date
<p><strong>HAVING Clause</strong></p>
<p>Hive 现在不支持 HAVING 子句。可以将 HAVING 子句转化为一个字查询，例如：</p>
<pre>SELECT col1 FROM t1 GROUP BY col1 HAVING SUM(col2) &gt; 10
</pre>
<p>可以用以下查询来表达：</p>
<pre>SELECT col1 FROM (SELECT col1, SUM(col2) AS col2sum
FROM t1 GROUP BY col1) t2
WHERE t2.col2sum &gt; 10
</pre>
<p><strong>LIMIT Clause</strong></p>
<p>Limit 可以限制查询的记录数。查询的结果是随机选择的。下面的查询语句从 t1 表中随机查询5条记录：</p>
<pre>SELECT * FROM t1 LIMIT 5
</pre>
<p>Top k 查询。下面的查询语句查询销售记录最大的 5 个销售代表。</p>
<pre>SET mapred.reduce.tasks = 1
SELECT * FROM sales SORT BY amount DESC LIMIT 5
</pre>
<p><strong>REGEX Column Specification</strong></p>
<p>SELECT 语句可以使用正则表达式做列选择，下面的语句查询除了 ds 和 hr 之外的所有列：</p>
<pre>SELECT `(ds|hr)?+.+` FROM sales
</pre>
<h3>Join</h3>
<p><strong>Syntax</strong></p>
<pre>join_table:
table_reference JOIN table_factor [join_condition]
| table_reference {LEFT|RIGHT|FULL} [OUTER]
JOIN table_reference join_condition
| table_reference LEFT SEMI JOIN
table_reference join_condition
table_reference:
table_factor
| join_table
table_factor:
tbl_name [alias]
| table_subquery alias
| ( table_references )
join_condition:
ON equality_expression ( AND equality_expression )*
equality_expression:
expression = expression
</pre>
<p>Hive 只支持等值连接（equality joins）、外连接（outer joins）和（left semi joins???）。Hive 不支持所有非等值的连接，</p>
<p>因为非等值连接非常难转化到 map/reduce 任务。另外，Hive 支持多于 2 个表的连接。</p>
<p>写 join 查询时，需要注意几个关键点：<br />
1. 只支持等值join，例如：</p>
<pre>  SELECT a.* FROM a JOIN b ON (a.id = b.id)
SELECT a.* FROM a JOIN b
ON (a.id = b.id AND a.department = b.department)
</pre>
<p>是正确的，然而:</p>
<pre>  SELECT a.* FROM a JOIN b ON (a.id  b.id)
</pre>
<p>是错误的。</p>
<p>2. 可以 join 多于 2 个表，例如</p>
<pre>  SELECT a.val, b.val, c.val FROM a JOIN b
ON (a.key = b.key1) JOIN c ON (c.key = b.key2)
</pre>
<p>如果join中多个表的 join key 是同一个，则 join 会被转化为单个 map/reduce 任务，例如：</p>
<pre>  SELECT a.val, b.val, c.val FROM a JOIN b
ON (a.key = b.key1) JOIN c
ON (c.key = b.key1)
</pre>
<p>被转化为单个 map/reduce 任务，因为 join 中只使用了 b.key1 作为 join key。</p>
<pre>SELECT a.val, b.val, c.val FROM a JOIN b ON (a.key = b.key1)
JOIN c ON (c.key = b.key2)
</pre>
<p>而这一 join 被转化为 2 个 map/reduce 任务。因为 b.key1 用于第一次 join 条件，而 b.key2 用于第二次 join。</p>
<p>join 时，每次 map/reduce 任务的逻辑是这样的：reducer 会缓存 join 序列中除了最后一个表的所有表的记录，</p>
<p>再通过最后一个表将结果序列化到文件系统。这一实现有助于在 reduce 端减少内存的使用量。实践中，应该把最大的那个表写在最后</p>
<p>（否则会因为缓存浪费大量内存）。例如：</p>
<pre> SELECT a.val, b.val, c.val FROM a
JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key1)
</pre>
<p>所有表都使用同一个 join key（使用 1 次 map/reduce 任务计算）。Reduce 端会缓存 a 表和 b 表的记录，然后每次取得一个 c 表的记录就计算一次 join 结果，类似的还有：</p>
<pre>  SELECT a.val, b.val, c.val FROM a
JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key2)
</pre>
<p>这里用了 2 次 map/reduce 任务。第一次缓存 a 表，用 b 表序列化；第二次缓存第一次 map/reduce 任务的结果，然后用 c 表序列化。</p>
<p>LEFT，RIGHT 和 FULL OUTER 关键字用于处理 join 中空记录的情况，例如：</p>
<pre>  SELECT a.val, b.val FROM a LEFT OUTER
JOIN b ON (a.key=b.key)
</pre>
<p>对应所有 a 表中的记录都有一条记录输出。输出的结果应该是 a.val, b.val，当 a.key=b.key 时，而当 b.key 中找不到等值的&nbsp;</p>
<p>a.key 记录时也会输出 a.val, NULL。&#8220;FROM a LEFT OUTER JOIN b&#8221;这句一定要写在同一行——意思是 a 表在 b 表的<strong>左边</strong>，所以</p>
<p> a 表中的所有记录都被保留了；&#8220;a RIGHT OUTER JOIN b&#8221;会保留所有 b 表的记录。OUTER JOIN 语义应该是遵循标准 SQL spec的。</p>
<p>Join 发生在 WHERE 子句<strong>之前</strong>。如果你想限制 join 的输出，应该在 WHERE 子句中写过滤条件——或是在 join 子句中写。</p>
<p>这里面一个容易混淆的问题是表分区的情况：</p>
<pre>  SELECT a.val, b.val FROM a
LEFT OUTER JOIN b ON (a.key=b.key)
WHERE a.ds='2009-07-07' AND b.ds='2009-07-07'
</pre>
<p>会 join a 表到 b 表（OUTER JOIN），列出 a.val 和 b.val 的记录。WHERE 从句中可以使用其他列作为过滤条件。</p>
<p>但是，如前所述，如果 b 表中找不到对应 a 表的记录，b 表的所有列都会列出 NULL，<strong>包括 ds 列</strong>。也就是说，join&nbsp;</p>
<p>会过滤 b 表中不能找到匹配 a 表 join key 的所有记录。这样的话，LEFT OUTER 就使得查询结果与 WHERE 子句无关了。</p>
<p>解决的办法是在 OUTER JOIN 时使用以下语法：</p>
<pre>  SELECT a.val, b.val FROM a LEFT OUTER JOIN b
ON (a.key=b.key AND
b.ds='2009-07-07' AND
a.ds='2009-07-07')
</pre>
<p>这一查询的结果是预先在 join 阶段过滤过的，所以不会存在上述问题。这一逻辑也可以应用于 RIGHT 和 FULL 类型的 join 中。</p>
<p>Join 是不能交换位置的。无论是 LEFT 还是 RIGHT join，都是左连接的。</p>
<pre>  SELECT a.val1, a.val2, b.val, c.val
FROM a
JOIN b ON (a.key = b.key)
LEFT OUTER JOIN c ON (a.key = c.key)
</pre>
<p>先 join a 表到 b 表，丢弃掉所有 join key 中不匹配的记录，然后用这一中间结果和 c 表做 join。这一表述有一个不太明显的问题，</p>
<p>就是当一个 key 在 a 表和 c 表都存在，但是 b 表中不存在的时候：整个记录在第一次 join，即 a JOIN b 的时候都被丢掉了</p>
<p>（包括a.val1，a.val2和a.key），然后我们再和 c 表 join 的时候，如果 c.key 与 a.key 或 b.key 相等，就会得到这样的结果：</p>
<p>NULL, NULL, NULL, c.val。</p>
<p>LEFT SEMI JOIN 是 IN/EXISTS 子查询的一种更高效的实现。Hive 当前没有实现 IN/EXISTS 子查询，所以你可以用 LEFT SEMI JOIN&nbsp;</p>
<p>重写你的子查询语句。LEFT SEMI JOIN 的限制是， JOIN 子句中右边的表只能在 ON 子句中设置过滤条件，在 WHERE 子句、</p>
<p>SELECT 子句或其他地方过滤都不行。</p>
<pre>  SELECT a.key, a.value
FROM a
WHERE a.key in
(SELECT b.key
FROM B);
</pre>
<p>可以被重写为：</p>
<pre>   SELECT a.key, a.val
FROM a LEFT SEMI JOIN b on (a.key = b.key</pre>
<pre><br />
</pre>
<pre><span style="font-family: Arial, Helvetica, sans-serif; white-space: normal; "><strong style="color: #5e58a2; font-size: 14px; ">Hive 优化</strong></span></pre>
<pre><span style="font-family: Arial, Helvetica, sans-serif; white-space: normal; "><strong style="color: #5e58a2; font-size: 14px; "><br />
</strong></span></pre>
<pre><span style="font-family: Arial, Helvetica, sans-serif; white-space: normal; "><strong style="color: #5e58a2; font-size: 14px; "><span style="color: #333333; font-weight: normal; font-size: 12px; ">
<p>Hive 针对不同的查询进行了优化，优化可以通过配置进行控制，本文将介绍部分优化的策略以及优化控制选项。</p>
<h4>列裁剪（Column Pruning）</h4>
<p>在读数据的时候，只读取查询中需要用到的列，而忽略其他列。例如，对于查询：</p>
<pre>SELECT a,b FROM T WHERE e
<p>其中，T 包含 5 个列 (a,b,c,d,e)，列 c，d 将会被忽略，只会读取a, b, e 列</p>
<p>这个选项默认为真： <strong>hive.optimize.cp = true</strong></p>
<h4>分区裁剪（Partition Pruning）</h4>
<p>在查询的过程中减少不必要的分区。例如，对于下列查询：</p>
<pre>SELECT * FROM (SELECT c1, COUNT(1)
FROM T GROUP BY c1) subq
WHERE subq.prtn = 100;
SELECT * FROM T1 JOIN
(SELECT * FROM T2) subq ON (T1.c1=subq.c2)
WHERE subq.prtn = 100;
</pre>
<p>会在子查询中就考虑 subq.prtn = 100 条件，从而减少读入的分区数目。</p>
<p>此选项默认为真：<strong>hive.optimize.pruner=true</strong></p>
<h4>Join</h4>
<p>在使用写有 Join 操作的查询语句时有一条原则：应该将条目少的表/子查询放在 Join 操作符的左边。原因是在 Join 操作的 Reduce&nbsp;</p>
<p>阶段，位于 Join 操作符左边的表的内容会被加载进内存，将条目少的表放在左边，可以有效减少发生 OOM 错误的几率。</p>
<p>对于一条语句中有多个 Join 的情况，如果 Join 的条件相同，比如查询：</p>
<pre>INSERT OVERWRITE TABLE pv_users
SELECT pv.pageid, u.age FROM page_view p
JOIN user u ON (pv.userid = u.userid)
JOIN newuser x ON (u.userid = x.userid);
</pre>
</pre>
<ul>
    <li>如果 Join 的 key 相同，不管有多少个表，都会则会合并为一个 Map-Reduce</li>
    <li>一个 Map-Reduce 任务，而不是 &#8216;n&#8217; 个</li>
    <li>在做 OUTER JOIN 的时候也是一样</li>
</ul>
<p>如果 Join 的条件不相同，比如：</p>
<pre>  INSERT OVERWRITE TABLE pv_users
SELECT pv.pageid, u.age FROM page_view p
JOIN user u ON (pv.userid = u.userid)
JOIN newuser x on (u.age = x.age);
</pre>
<p>Map-Reduce 的任务数目和 Join 操作的数目是对应的，上述查询和以下查询是等价的：</p>
<pre>  INSERT OVERWRITE TABLE tmptable
SELECT * FROM page_view p JOIN user u
ON (pv.userid = u.userid);
INSERT OVERWRITE TABLE pv_users
SELECT x.pageid, x.age FROM tmptable x
JOIN newuser y ON (x.age = y.age);
</pre>
<h4>Map Join</h4>
<p>Join 操作在 Map 阶段完成，不再需要Reduce，前提条件是需要的数据在 Map 的过程中可以访问到。比如查询：</p>
<pre>  INSERT OVERWRITE TABLE pv_users
SELECT /*+ MAPJOIN(pv) */ pv.pageid, u.age
FROM page_view pv
JOIN user u ON (pv.userid = u.userid);
</pre>
<p>可以在 Map 阶段完成 Join，如图所示：</p>
<p><a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/hive-ql-mapjoin.jpg" style="color: #333333; text-decoration: none; "><img height="317" alt="" src="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/hive-ql-mapjoin.jpg" width="465" /></a></p>
<p>相关的参数为：</p>
<ul>
    <li><strong>hive.join.emit.interval = 1000</strong>&nbsp;How many rows in the right-most join operand Hive should buffer before emitting the join result.</li>
    <li><strong>hive.mapjoin.size.key = 10000</strong></li>
    <li><strong>hive.mapjoin.cache.numrows = 10000</strong></li>
</ul>
<h4>Group By</h4>
<ul>
    <li>Map 端部分聚合：
    <ul>
        <li>并不是所有的聚合操作都需要在 Reduce 端完成，很多聚合操作都可以先在 Map 端进行部分聚合，最后在 Reduce 端得出最终结果。</li>
        <li>基于 Hash</li>
        <li>参数包括：
        <ul>
            <li><strong>hive.map.aggr = true</strong>&nbsp;是否在 Map 端进行聚合，默认为 True</li>
            <li><strong>hive.groupby.mapaggr.checkinterval = 100000</strong>&nbsp;在 Map 端进行聚合操作的条目数目</li>
        </ul>
        </li>
    </ul>
    </li>
    <li>有数据倾斜的时候进行负载均衡
    <ul>
        <li><strong>hive.groupby.skewindata = false</strong></li>
        <li>当选项设定为 true，生成的查询计划会有两个 MR Job。第一个 MR Job 中，Map 的输出结果集合会随机分布到 Reduce 中，每个 Reduce 做部分聚合操作，并输出结果，这样处理的结果是相同的 Group By Key 有可能被分发到不同的 Reduce 中，从而达到负载均衡的目的；第二个 MR Job 再根据预处理的数据结果按照 Group By Key 分布到 Reduce 中（这个过程可以保证相同的 Group By Key 被分布到同一个 Reduce 中），最后完成最终的聚合操作。</li>
    </ul>
    </li>
</ul>
<h4>合并小文件</h4>
<p>文件数目过多，会给 HDFS 带来压力，并且会影响处理效率，可以通过合并 Map 和 Reduce 的结果文件来消除这样的影响：</p>
<ul>
    <li><strong>hive.merge.mapfiles = true</strong>&nbsp;是否和并 Map 输出文件，默认为 True</li>
    <li><strong>hive.merge.mapredfiles = false</strong>&nbsp;是否合并 Reduce 输出文件，默认为 False</li>
    <li><strong>hive.merge.size.per.task = 256*1000*1000</strong>&nbsp;合并文件的大小</li>
</ul>
<div><strong style="color: #5e58a2; font-size: 14px; ">Hive 的扩展特性</strong></div>
<div><strong style="color: #5e58a2; font-size: 14px; "><span style="color: #333333; font-weight: normal; line-height: normal; font-size: 12px; ">
<div id="newsin" style="width: 685px; height: auto; margin-top: 6px; margin-right: auto; margin-bottom: 0px; margin-left: auto; ">
<div class="textinit" style="width: 660px; height: auto; margin-top: 8px; margin-right: auto; margin-bottom: 0px; margin-left: auto; line-height: 22px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: 12px; ">
<div class="news_content">
<pre>  CREATE TABLE mylog ( user_id BIGINT, page_url STRING, unix_time INT)
STORED AS TEXTFILE;</pre>
<p>当用户的数据文件格式不能被当前 Hive 所识别的时候，可以自定义文件格式。可以参考 contrib/src/java/org/apache/hadoop/hive/contrib/fileformat/base64 中的例子。写完自定义的格式后，在创建表的时候指定相应的文件格式就可以：</p>
<pre>  CREATE TABLE base64_test(col1 STRING, col2 STRING)
STORED AS
INPUTFORMAT 'org.apache.hadoop.hive.contrib.
fileformat.base64.Base64TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.contrib.
fileformat.base64.Base64TextOutputFormat';</pre>
<h4>SerDe</h4>
<p>SerDe 是 Serialize/Deserilize 的简称，目的是用于序列化和反序列化。序列化的格式包括：</p>
<ul>
    <li>分隔符（tab、逗号、CTRL-A）</li>
    <li>Thrift 协议</li>
</ul>
<p>反序列化（内存内）：</p>
<ul>
    <li>Java Integer/String/ArrayList/HashMap</li>
    <li>Hadoop Writable 类</li>
    <li>用户自定义类</li>
</ul>
<p style="text-align: center; ">目前存在的 Serde 见下图：<br />
<a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/serde.jpg" style="color: #333333; text-decoration: none; "><br />
</a><a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/serde1.jpg" style="color: #333333; text-decoration: none; "><img height="275" alt="" src="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/serde1.jpg" width="563" /></a></p>
<p>其中，LazyObject 只有在访问到列的时候才进行反序列化。 BinarySortable：保留了排序的二进制格式。</p>
<p>当存在以下情况时，可以考虑增加新的 SerDe：</p>
<ul>
    <li>用户的数据有特殊的序列化格式，当前的 Hive 不支持，而用户又不想在将数据加载至 Hive 前转换数据格式。</li>
    <li>用户有更有效的序列化磁盘数据的方法。</li>
</ul>
<p>用户如果想为 Text 数据增加自定义 Serde ，可以参照 contrib/src/java/org/apache/hadoop/hive/contrib/serde2/RegexSerDe.java 中的例子。RegexSerDe 利用用户提供的正则表倒是来反序列化数据，例如：</p>
<pre>  CREATE TABLE apache_log(
host STRING,
identity STRING,
user STRING,
time STRING,
request STRING,
status STRING,
size STRING,
referer STRING,
agent STRING)
ROW FORMAT
SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
WITH SERDEPROPERTIES
( "input.regex" = "([^ ]*) ([^ ]*) ([^ ]*) (-|\\\\[[^\\\\]]*\\\\])
([^ \\"]*|\\"[^\\"]*\\") (-|[0-9]*) (-|[0-9]*)(?: ([^ \\"]*|\\"[^\\"]*\\")
([^ \\"]*|\\"[^\\"]*\\"))?",
"output.format.string" = "%1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s %9$s";)
STORED AS TEXTFILE;</pre>
<p>用户如果想为 Binary 数据增加自定义的 SerDE，可以参考例子：serde/src/java/org/apache/hadoop/hive/serde2/binarysortable，例如：</p>
<pre>  CREATE TABLE mythrift_table
ROW FORMAT SERDE
'org.apache.hadoop.hive.contrib.serde2.thrift.ThriftSerDe'
WITH SERDEPROPERTIES (
"serialization.class" = "com.facebook.serde.tprofiles.full",
"serialization.format" = "com.facebook.thrift.protocol.TBinaryProtocol";);</pre>
<h4>Map/Reduce 脚本（Transform）</h4>
<p>用户可以自定义 Hive 使用的 Map/Reduce 脚本，比如：</p>
<pre>  FROM (
SELECT TRANSFORM(user_id, page_url, unix_time)
USING 'page_url_to_id.py'
AS (user_id, page_id, unix_time)
FROM mylog
DISTRIBUTE BY user_id
SORT BY user_id, unix_time)
mylog2
SELECT TRANSFORM(user_id, page_id, unix_time)
USING 'my_python_session_cutter.py' AS (user_id, session_info);</pre>
<p>Map/Reduce 脚本通过 stdin/stdout 进行数据的读写，调试信息输出到 stderr。</p>
<h4>UDF（User-Defined-Function）</h4>
<p>用户可以自定义函数对数据进行处理，例如：</p>
<pre>  add jar build/ql/test/test-udfs.jar;
CREATE TEMPORARY FUNCTION testlength
AS 'org.apache.hadoop.hive.ql.udf.UDFTestLength';
SELECT testlength(src.value) FROM src;
DROP TEMPORARY FUNCTION testlength;</pre>
<p>UDFTestLength.java 为：</p>
<pre>  package org.apache.hadoop.hive.ql.udf;
public class UDFTestLength extends UDF {
public Integer evaluate(String s) {
if (s == null) {
return null;
}
return s.length();
}
}</pre>
<p>自定义函数可以重载：</p>
<pre>  add jar build/contrib/hive_contrib.jar;
CREATE TEMPORARY FUNCTION example_add
AS 'org.apache.hadoop.hive.contrib.udf.example.UDFExampleAdd';
SELECT example_add(1, 2) FROM src;
SELECT example_add(1.1, 2.2) FROM src;</pre>
<p>UDFExampleAdd.java:</p>
<pre>  public class UDFExampleAdd extends UDF {
public Integer evaluate(Integer a, Integer b) {
if (a = null || b = null)
return null;
return a + b;
}
public Double evaluate(Double a, Double b) {
if (a = null || b = null)
return null;
return a + b;
}
}</pre>
<p>%%</p>
<p>在使用 UDF 的时候，会自动进行类型转换，这个 java 或者 C 中的类型转换有些类似，比如:</p>
<pre>  SELECT example_add(1, 2.1) FROM src;</pre>
<p>的结果是 3.1，这是因为 UDF 将类型为 Int 的参数 &#8220;1&#8243; 转换为 double。</p>
<p>类型的隐式转换是通过 UDFResolver 来进行控制的，并且可以根据不同的 UDF 进行不同的控制。</p>
<p>UDF 还可以支持变长的参数，例如 UDFExampleAdd.java：</p>
<pre>  public class UDFExampleAdd extends UDF {
public Integer evaluate(Integer... a) {
int total = 0;
for (int i=0; i<a>
<p>使用例子为：</p>
<pre>  SELECT example_add(1, 2) FROM src;
SELECT example_add(1, 2, 3) FROM src;
SELECT example_add(1, 2, 3, 4.1) FROM src;</pre>
<p>综上，UDF 具有以下特性：</p>
<ul>
    <li>用 java 写 UDF 很容易。</li>
    <li>Hadoop 的 Writables/Text 具有较高性能。</li>
    <li>UDF 可以被重载。</li>
    <li>Hive 支持隐式类型转换。</li>
    <li>UDF 支持变长的参数。</li>
    <li>genericUDF 提供了较好的性能（避免了反射）。</li>
</ul>
<h4>UDAF（User-Defined Aggregation Funcation）</h4>
<p>例子：</p>
<pre>  SELECT page_url, count(1), count(DISTINCT user_id) FROM mylog;</pre>
<p>UDAFCount.java:</p>
<pre>  public class UDAFCount extends UDAF {
public static class Evaluator implements UDAFEvaluator {
private int mCount;
public void init() {
mcount = 0;
}
public boolean iterate(Object o) {
if (o!=null)
mCount++;
return true;
}
public Integer terminatePartial() {
return mCount;
}
public boolean merge(Integer o) {
mCount += o;
return true;
}
public Integer terminate() {
return mCount;
}
}</pre>
<p>UDAF 总结：</p>
<ul>
    <li>编写 UDAF 和 UDF 类似</li>
    <li>UDAF 可以重载</li>
    <li>UDAF 可以返回复杂类</li>
    <li>在使用 UDAF 的时候可以禁止部分聚合功能</li>
</ul>
<p style="text-align: center; ">UDF，UDAF 和 MR 脚本的对比：</p>
</a><a href="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/udf-udaf-mr.jpg" style="color: #333333; text-decoration: none; "><img height="283" alt="" src="http://rdc.taobao.com/blog/dw/wp-content/uploads/2010/04/udf-udaf-mr.jpg" width="556" /></a></pre>
</div>
</div>
</div>
</span></strong></div>
</span></strong></span></pre>
</pre>
</span></strong></div>
<p>&nbsp;</p>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/342354.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-05 15:03 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/05/342354.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hadoop Map/Reduce教程（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342264.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Tue, 04 Jan 2011 03:22:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342264.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342264.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342264.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342264.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342264.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://hadoop.apache.org/common/docs/r0.20.0/cn/mapred_tutorial.htmlHadoop Map/Reduce教程    目的    先决条件    概述    输入与输出    例子：WordCount v1.0            源代码        用法        解...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342264.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/342264.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-04 11:22 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342264.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hadoop云框架配置方案（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342261.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Tue, 04 Jan 2011 02:28:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342261.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342261.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342261.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342261.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342261.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://jeffxie.blog.51cto.com/1365360/295076虽然和GOOGLE的云计算框架相差很远，但是基本能够实现云框架还是可以的，我选择了hadoop,最近这个框架在网络上炒的很火，一部分IT高手加入了开发队列，本人也不例外(不过我不是高手,只是一个很普通的系统架构师而已)。好了废话少说，直接切入主题吧首先使用了五台机器来实现hadoop框架。...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342261.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/342261.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-04 10:28 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342261.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>云框架Hadoop之Hive基本操作（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342260.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Tue, 04 Jan 2011 02:25:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342260.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342260.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342260.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342260.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342260.html</trackback:ping><description><![CDATA[转自：http://jeffxie.blog.51cto.com/1365360/317518
<div><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">下面我们说明一下hive的简单查询应用。</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">在hadoop环境下，shell下直接输入hive,就会出现hive shell操作界面。关于HIVE里的SQL，我习惯性的将HIVE的SQL语句叫做HQL，事实上HQL有很多和SQL类似的地方,还有一点要说明的是HQL数据存储位置是在Hadoop上的HDFS。</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">&nbsp;&nbsp;&nbsp; HIVE的数据类型 。HiveQL只支持以下几种基本数据类型TINYINT, SMALLINT, INT, BIGINT, DOUBLE, STRING。</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">支持的复杂数据类型有Structs, Maps, Arrays。</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">创建表。 Hive不同于其他数据库，它只有一个默认数据库"default" ，所有的table都保持在里面。</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">&nbsp;&nbsp;&nbsp; 创建数据表:</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><code style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">CREATE TABLE test_change (a int, b int, c int);</code></span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">解释：创建一个叫test_change的表，其中有字段a,b,c</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">create table test(a int,b int,c int);</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">查看表</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">SHOW TABLES;</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">如果你不知道表后缀，那么你也可以这样</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">SHOW TABLES 'page.*';</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">查看表结构</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">DESCRIBE TABLE;</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">删除表</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">DROP TABLE</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">修改表名</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">ALTER TABLE old_table_name RENAME TO new_table_name;</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">导入数据:</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">load data local inpath "/root/test.sql" overwrite into table test;</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">这里指的注意的一点是，test.sql里的数据内容是需要分隔符的，HIVE里面有自己的默认分隔符，我想</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
</span><span  style="color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; ">尽量用它默认的，这样就不会出现大问题</span></div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/342260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-04 10:25 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342260.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDFS+MapReduce+Hive+HBase十分钟快速入门（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342258.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Tue, 04 Jan 2011 02:15:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342258.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342258.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342258.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342258.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342258.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://jeffxie.blog.51cto.com/1365360/3174591.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;前言本文的目的是让一个从未接触Hadoop的人，在很短的时间内快速上手，掌握编译、安装和简单的使用。2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hadoop家族截止2009-8-19日，整个Hadoop...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342258.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/342258.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2011-01-04 10:15 <a href="http://www.blogjava.net/hengheng123456789/archive/2011/01/04/342258.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HBase存储架构（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342074.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 31 Dec 2010 06:16:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342074.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342074.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342074.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342074.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342074.html</trackback:ping><description><![CDATA[转自：http://www.spnguru.com/?p=31
<div>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; font-weight: bold; background-position: initial initial; background-repeat: initial initial; ">HBase</strong><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; font-weight: bold; background-position: initial initial; background-repeat: initial initial; ">存储架构</strong></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">英文原文：<a href="http://www.larsgeorge.com/2009/10/hbase-architecture-101-storage.html" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #0066cc; background-position: initial initial; background-repeat: initial initial; ">http://www.larsgeorge.com/2009/10/hbase-architecture-101-storage.html</a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">HBase最隐秘的问题之一就是它的数据是如何存储的。虽然大多数用户都不会因为这个问题向你抱怨，但是如果你想学习哪些高级的配置选项并了解它们的意思，你可能就需要来了解一下这个存储问题了。&#8220;怎样才能把HBase调整到最适合我需求的状态？&#8221;你可能对于这样一系列类似的问题非常感兴趣。那么你就需要绕过这些问题来学习HBase的基础知识。另一个支持你学习这些基础知识的理由是有时候各种各样你想不到的灾难需要你恢复整个HBase。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">我首先学习了HBase中控制各种不同文件的独立的类，然后根据我对整个HBase存储系统的理解在脑海中构建HBase架构的图像。但是我发现想要在头脑中构建出一幅连贯的HBase架构图片很困难，于是我就把它画了出来。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><a href="http://www.spnguru.com/wp-content/uploads/2010/07/hbase-files2.png" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #0066cc; background-position: initial initial; background-repeat: initial initial; "><img class="alignnone size-full wp-image-34" title="hbase-files2" src="http://www.spnguru.com/wp-content/uploads/2010/07/hbase-files2.png" alt="" width="760" height="395" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; max-width: 640px; background-position: initial initial; background-repeat: initial initial; " /></a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">你可能注意到了这不是一个UML图或者调用图。这个图混合了类和他们管理控制的文件，将注意力集中到了本文要讨论的主题上。后面我将会讨论这张图所涉及到的细节，包括一些配置文件项是如何影响底层的文件存储系统的。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">好，我们现在来分析这张图里面到底包含了什么。首先HBase操控两种基本类型的文件，一种用于存储WAL的log，另一种用于存储具体的数据。这两种文件主要由HRegionServer来管理，但是在有的情况下HMaster会跳过HRegionServer，直接操作这两种文件。你可能注意到了，这些文件都被存储在HDFS上面，并且每个文件包含了多个数据块。配置文件中就有一个选项用来调整系统控制数据的大小。我们后面会详细讨论这个问题。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">接下来看一下数据的大致流程。假设你需要通过某个特定的RowKey查询一行记录，首先Client端会连接Zookeeper Qurom，通过Zookeeper，Client能获知哪个Server管理-ROOT- Region。接着Client访问管理-ROOT-的Server，进而获知哪个Server管理.META.表。这两个信息Client只会获取一次并缓存起来。在后续的操作中Client会直接访问管理.META.表的Server，并获取Region分布的信息。一旦Client获取了这一行的位置信息，比如这一行属于哪个Region，Client将会缓存这个信息并直接访问HRegionServer。久而久之Client缓存的信息渐渐增多，即使不访问.META.表也能知道去访问哪个HRegionServer。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">注意：当HBase启动的时候HMaster负责分配Region给HRegionServer，这其中当然也包括-ROOT-表和.META.表的Region。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">接下来HRegionServer打开这个Region并创建一个HRegion对象。当HRegion打开以后，它给每个table的每个HColumnFamily创建一个Store实例。每个Store实例拥有一个或者多个StoreFile实例。StoreFile对HFile做了轻量级的包装。除了Store实例以外，每个HRegion还拥有一个MemStore实例和一个HLog实例。现在我们就可以看看这些实例是如何在一起工作的，遵循什么样的规则以及这些规则的例外。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; text-decoration: underline; background-position: initial initial; background-repeat: initial initial; ">保留住Put</span></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">现在看一下数据是怎样被写到实际的存储中去的。Client发起了一个HTable.put(Put)请求给HRegionServer，HRegionServer会将请求匹配到某个具体的HRegion上面。紧接着的操作时决定是否写WAL log。是否写WAL log由Client传递的一个标志决定，你可以设置这个标志：Put.writeToWAL(boolean)。WAL log文件是一个标准的Hadoop SequenceFile（现在还在讨论是否应该把文件格式改成一个更适合HBase的格式）。在文件中存储了HLogKey，这些Keys包含了和实际数据对应的序列号，用途是当RegionServer崩溃以后能将WAL log中的数据同步到永久存储中去。做完这一步以后，Put数据会被保存到MemStore中，同时会检查MemStore是否已经满了，如果已经满了，则会触发一个Flush to Disk的请求。HRegionServer有一个独立的线程来处理Flush to Disk的请求，它负责将数据写成HFile文件并存到HDFS上。它也会存储最后写入的数据序列号，这样就可以知道哪些数据已经存入了永久存储的HDFS中。现在让我们来看看这些存储文件。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; text-decoration: underline; background-position: initial initial; background-repeat: initial initial; ">存储文件</span></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">HBase在HDFS上面的所有文件有一个可配置的根目录，默认根目录是/hbase。通过使用hadoop的DFS工具就可以看到这些文件夹的结构。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">在根目录下面你可以看到一个.logs文件夹，这里面存了所有由HLog管理的WAL log文件。在.logs目录下的每个文件夹对应一个HRegionServer，每个HRegionServer下面的每个log文件对应一个Region。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">有时候你会发现一些oldlogfile.log文件（在大多数情况下你可能看不到这个文件），这个文件在一种异常情况下会被产生。这个异常情况就是HMaster对log文件的访问情况产生了怀疑，它会产生一种称作&#8220;log splits&#8221;的结果。有时候HMaster会发现某个log文件没人管了，就是说任何一个HRegionServer都不会管理这个log文件（有可能是原来管理这个文件的HRegionServer挂了），HMaster会负责分割这个log文件（按照它们归属的Region），并把那些HLogKey写到一个叫做oldlogfile.log的文件中，并按照它们归属的Region直接将文件放到各自的Region文件夹下面。各个HRegion会从这个文件中读取数据并将它们写入到MemStore中去，并开始将数据Flush to Disk。然后就可以把这个oldlogfile.log文件删除了。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">注意：有时候你可能会发现另一个叫做oldlogfile.log.old的文件，这是由于HMaster做了重复分割log文件的操作并发现oldlogfile.log已经存在了。这时候就需要和HRegionServer以及HMaster协商到底发生了什么，以及是否可以把old的文件删掉了。从我目前遇到的情况来看，old文件都是空的并且可以被安全删除的。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">HBase的每个Table在根目录下面用一个文件夹来存储，文件夹的名字就是Table的名字。在Table文件夹下面每个Region也用一个文件夹来存储，但是文件夹的名字并不是Region的名字，而是Region的名字通过Jenkins Hash计算所得到的字符串。这样做的原因是Region的名字里面可能包含了不能在HDFS里面作为路径名的字符。在每个Region文件夹下面每个ColumnFamily也有自己的文件夹，在每个ColumnFamily文件夹下面就是一个个HFile文件了。所以整个文件夹结构看起来应该是这个样子的：</p>
<blockquote style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f1f1f1; quotes: none; border-top-color: #dddddd; border-right-color: #dddddd; border-bottom-color: #dddddd; border-left-color: #dddddd; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; background-position: initial initial; background-repeat: initial initial; ">/hbase/&lt;tablename&gt;/&lt;encoded-regionname&gt;/&lt;column-family&gt;/&lt;filename&gt;</p>
</blockquote>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">在每个Region文件夹下面你会发现一个.regioninfo文件，这个文件用来存储这个Region的Meta Data。通过这些Meta Data我们可以重建被破坏的.META.表，关于.regioninfo的应用你可以参考HBASE-7和HBASE-1867。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">有一件事情前面一直没有提到，那就是Region的分割。当一个Region的数据文件不断增长并超过一个最大值的时候（你可以配置这个最大值 hbase.hregion.max.filesize），这个Region会被切分成两个。这个过程完成的非常快，因为原始的数据文件并不会被改变，系统只是简单的创建两个Reference文件指向原始的数据文件。每个Reference文件管理原始文件一半的数据。Reference文件名字是一个ID，它使用被参考的Region的名字的Hash作为前缀。例如：1278437856009925445.3323223323。Reference文件只含有非常少量的信息，这些信息包括被分割的原始Region的Key以及这个文件管理前半段还是后半段。HBase使用HalfHFileReader类来访问Reference文件并从原始数据文件中读取数据。前面的架构图只并没有画出这个类，因为它只是临时使用的。只有当系统做Compaction的时候原始数据文件才会被分割成两个独立的文件并放到相应的Region目录下面，同时原始数据文件和那些Reference文件也会被清除。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">前面dump出来的文件结构也证实了这个过程，在每个Table的目录下面你可以看到一个叫做compaction.dir的目录。这个文件夹是一个数据交换区，用于存放split和compact Region过程中生成的临时数据。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; text-decoration: underline; background-position: initial initial; background-repeat: initial initial; ">HFile</span></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">现在我们将深入HBase存储架构的核心，探讨HBase具体的数据存储文件的结构。HFile就是这个数据存储文件的结构（Ryan Rawson就是靠它扬名立万的）。创建HFile这样一个文件结构的目的只有一个：快速高效的存储HBase的数据。HFile是基于Hadoop TFile的（参见 HADOOP-3315）。HFile模仿了Google Bigtable中SSTable的格式。原先HBase使用Hadoop的MapFile，但是这种文件已经被证明了效率差。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">现在让我们来看看这个文件结构到底是什么样的。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><a href="http://www.spnguru.com/wp-content/uploads/2010/07/hfile2.png" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #0066cc; background-position: initial initial; background-repeat: initial initial; "><img class="alignnone size-full wp-image-36" title="hfile2" src="http://www.spnguru.com/wp-content/uploads/2010/07/hfile2.png" alt="" width="785" height="227" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; max-width: 640px; background-position: initial initial; background-repeat: initial initial; " /></a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">首先这个文件是不定长的，长度固定的只有其中的两块：Trailer和FileInfo。正如图中所示的，Trailer中有指针指向其他数据块的起始点。Index数据块记录了每个Data块和Meta块的起始点。Data块和Meta块都是可有可无的，但是对于大部分的HFile，你都可以看到Data块。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">那么每个块的大小是如何确定的呢？这个值可以在创建一个Table的时候通过HColumnDescriptor（实际上应该称作FamilyDescriptor）来设定。这里我们可以看一个例子：</p>
<blockquote style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f1f1f1; quotes: none; border-top-color: #dddddd; border-right-color: #dddddd; border-bottom-color: #dddddd; border-left-color: #dddddd; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; background-position: initial initial; background-repeat: initial initial; ">{NAME =&gt; &#8216;docs&#8217;, FAMILIES =&gt; [{NAME =&gt; &#8216;cache&#8217;, COMPRESSION =&gt; &#8216;NONE&#8217;, VERSIONS =&gt; &#8217;3&#8242;, TTL =&gt; &#8217;2147483647&#8242;, BLOCKSIZE =&gt; &#8217;65536&#8242;, IN_MEMORY =&gt; &#8216;false&#8217;, BLOCKCACHE =&gt; &#8216;false&#8217;}, {NAME =&gt; &#8216;contents&#8217;, COMPRESSION =&gt; &#8216;NONE&#8217;, VERSIONS =&gt; &#8217;3&#8242;, TTL =&gt; &#8217;2147483647&#8242;, BLOCKSIZE =&gt; &#8217;65536&#8242;, IN_MEMORY =&gt; &#8216;false&#8217;, BLOCKCACHE =&gt; &#8216;false&#8217;}, &#8230;</p>
</blockquote>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">从这里可以看出这是一个叫做docs的Table，它有两个Family：cache和contents，这两个Family对应的HFile的数据块的大小都是64K。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">关于如何设定数据块的大小，我们应用一段HFile源码中的注释：</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">我们推荐将数据块的大小设置为8KB至1MB。大的数据块比较适合顺序的查询（比如Scan），但不适合随机查询，想想看，每一次随机查询可能都需要你去解压缩一个大的数据块。小的数据块适合随机的查询，但是需要更多的内存来保存数据块的索引(Data Index)，而且创建文件的时候也可能比较慢，因为在每个数据块的结尾我们都要把压缩的数据流Flush到文件中去（引起更多的Flush操作）。并且由于压缩器内部还需要一定的缓存，最小的数据块大小应该在20KB &#8211; 30KB左右。可能从前面的描述你会发现数据块（Data Block）是数据压缩的一个单位。后面我们会深入Data Block内部去了解它的详细构造。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">在HBase的配置文件中你会看到一个参数：hfile.min.blocksize.size，这个参数看上去只会用在数据迁移或者通过工具直接创建HFile的过程中。（貌似HBase创建HFile不会使用这个参数，HBase使用的是.META.表中记录的那个值）。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">呼——，到现在为止解释的还不错吧。好了，让我们继续。有时候我们可能会想知道一个HFile是否正常以及它里面包含了什么内容。没问题，已经有一个应用程序来做这件事了。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">HFile.main()本身就提供了一个用来dump HFile的工具。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><a href="http://www.spnguru.com/wp-content/uploads/2010/07/s1.jpg" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #0066cc; background-position: initial initial; background-repeat: initial initial; "><img class="alignnone size-full wp-image-38" title="s1" src="http://www.spnguru.com/wp-content/uploads/2010/07/s1.jpg" alt="" width="560" height="154" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; max-width: 640px; background-position: initial initial; background-repeat: initial initial; " /></a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">这里有一个dump文件的例子：</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><a href="http://www.spnguru.com/wp-content/uploads/2010/07/s2.jpg" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #0066cc; background-position: initial initial; background-repeat: initial initial; "><img class="alignnone size-full wp-image-39" title="s2" src="http://www.spnguru.com/wp-content/uploads/2010/07/s2.jpg" alt="" width="619" height="449" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; max-width: 640px; background-position: initial initial; background-repeat: initial initial; " /></a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">第一部分是存储具体数据的KeyValue对，每个数据块除了开头的Magic以外就是一个个KeyValue对拼接而成。后面会详细介绍每个KeyValue对的内部构造。第二部分是Tailer块的具体内容，最后一部分是FileInfo块的具体内容。Dump HFile还有一个作用就是检查HFile是否正常。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; text-decoration: underline; background-position: initial initial; background-repeat: initial initial; ">KeyValue</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; text-decoration: underline; background-position: initial initial; background-repeat: initial initial; ">对</span></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">HFile里面的每个KeyValue对就是一个简单的byte数组。但是这个byte数组里面包含了很多项，并且有固定的结构。我们来看看里面的具体结构：</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; "><a href="http://www.spnguru.com/wp-content/uploads/2010/07/KeyValue2.png" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #0066cc; background-position: initial initial; background-repeat: initial initial; "><img class="alignnone size-full wp-image-40" title="KeyValue2" src="http://www.spnguru.com/wp-content/uploads/2010/07/KeyValue2.png" alt="" width="789" height="132" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; max-width: 640px; background-position: initial initial; background-repeat: initial initial; " /></a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">开始是两个固定长度的数值，分别表示Key的长度和Value的长度。紧接着是Key，开始是固定长度的数值，表示RowKey的长度，紧接着是RowKey，然后是固定长度的数值，表示Family的长度，然后是Family，接着是Qualifier，然后是两个固定长度的数值，表示Time Stamp和Key Type。Value部分没有这么复杂的结构，就是纯粹的数据。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">java.org.apache.hadoop.hbase.KeyValue是用来处理这个KeyValue，你可能会发现在这个类里面有两个方法：getKey和getRow。getKey当然是用来获取Key的内容，那么getRow是什么？其实是用来获取RowKey的。RowKey不是HBase的基本元素吗？是的，这个类已经不是纯粹的Key&amp;Value处理，它已经打上了HBase的烙印。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 24px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #333333; font-family: Georgia, 'Bitstream Charter', serif; font-size: 16px; line-height: 24px; ">好了，这篇文章就到此为止了，它对HBase的存储架构做了一个大致的介绍。希望这篇文章对于那些想更深入的挖掘HBase细节的人来说，能作为一个起点。</p>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/342074.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2010-12-31 14:16 <a href="http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342074.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HBase与BigTable的比较(转)</title><link>http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342060.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 31 Dec 2010 05:55:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342060.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/342060.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342060.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/342060.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/342060.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://tangay.javaeye.com/blog/724487HBase是Google的BigTable架构的一个开源实现。但是我个人觉得，要做到充分了解下面两点还是有点困难的：一 HBase涵盖了BigTable规范的哪些部分？二 HBase与BigTable仍然有哪些区别？下面我将对这两个系统做些比较。在做比较之前，我要指出一个事实：HBase是非...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342060.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/342060.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2010-12-31 13:55 <a href="http://www.blogjava.net/hengheng123456789/archive/2010/12/31/342060.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HBase入门（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2010/12/30/341957.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Thu, 30 Dec 2010 02:25:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2010/12/30/341957.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/341957.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2010/12/30/341957.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/341957.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/341957.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转自：http://labs.chinamobile.com/mblog/4110_22332?wralxianxrnx	HBase是Hadoop的一个子项目,HBase采用了Google BigTable的稀疏的,面向列的数据库实现方式的理论,建立在hadoop的hdfs上,一方面里用了hdfs的高可靠性和可伸缩行,另外一方面里用了BigTable的高效数据组织形式.可以说HBase为海...&nbsp;&nbsp;<a href='http://www.blogjava.net/hengheng123456789/archive/2010/12/30/341957.html'>阅读全文</a><img src ="http://www.blogjava.net/hengheng123456789/aggbug/341957.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2010-12-30 10:25 <a href="http://www.blogjava.net/hengheng123456789/archive/2010/12/30/341957.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hadoop开发入门资料__值得收藏参考（转）</title><link>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341475.html</link><dc:creator>哼哼</dc:creator><author>哼哼</author><pubDate>Fri, 24 Dec 2010 08:25:00 GMT</pubDate><guid>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341475.html</guid><wfw:comment>http://www.blogjava.net/hengheng123456789/comments/341475.html</wfw:comment><comments>http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341475.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hengheng123456789/comments/commentRss/341475.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hengheng123456789/services/trackbacks/341475.html</trackback:ping><description><![CDATA[<span style="background-color: yellow; "></span><span style="background-color: yellow; "><span style="background-color: yellow; "></span></span><span style="background-color: yellow; "><span style="background-color: yellow; "><span style="background-color: yellow; "></span></span></span><span style="background-color: #f6fcfe; "><span style="background-color: yellow; "><span style="background-color: yellow; "><span style="background-color: yellow; "></span></span></span></span><span style="background-color: #f6fcfe; "><span style="background-color: #f6fcfe; "><span style="background-color: yellow; "><span style="background-color: yellow; "><span style="background-color: yellow; "><span style="background-color: yellow; "><span style="background-color: #f6fcfe; ">转自：http://hi.baidu.com/webcell/blog/item/2154b1015874bf0a728da5a0.html</span>
<div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; "></font></span></p>
<span style="background-color: #f6fcfe; ">
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>&nbsp;是Google&nbsp;<a href="http://avindev.googlepages.com/mapreduce.doc" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><font color="#006600" style="line-height: normal; ">MapReduce</font></a>&nbsp;的一个Java实现。MapReduce是一种简化的分布式编程模式，让程序自动分布到一个由普通机器组成的超大集群上并发执行。就如同java程序员可以不考虑内存泄露一样， MapReduce的run-time系统会解决输入数据的分布细节，跨越机器集群的程序执行调度，处理机器的失效，并且管理机器之间的通讯请求。这样的模式允许程序员可以不需要有什么并发处理或者分布式系统的经验，就可以处理超大的分布式系统得资源。</p>
<h2 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp; 一、概论</h2>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp; 作为<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>程序员，他要做的事情就是：<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 1、定义Mapper，处理输入的Key-Value对，输出中间结果。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 2、定义Reducer，可选，对中间结果进行规约，输出最终结果。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 3、定义InputFormat 和OutputFormat，可选，InputFormat将每行输入文件的内容转换为Java类供Mapper函数使用，不定义时默认为String。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 4、定义main函数，在里面定义一个Job并运行它。</p>
</span>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp; 然后的事情就交给系统了。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 1.基本概念：<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>的HDFS实现了google的GFS文件系统，NameNode作为文件系统的负责调度运行在master，DataNode运行在每个机器上。同时<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>实现了Google的MapReduce，JobTracker作为MapReduce的总调度运行在master，TaskTracker则运行在每个机器上执行Task。<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 2.main()函数，创建JobConf，定义Mapper，Reducer，Input/OutputFormat 和输入输出文件目录，最后把Job提交給JobTracker，等待Job结束。<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 3.JobTracker，创建一个InputFormat的实例，调用它的getSplits()方法，把输入目录的文件拆分成FileSplist作为Mapper task 的输入，生成Mapper task加入Queue。<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 4.TaskTracker 向 JobTracker索求下一个Map/Reduce。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Mapper Task先从InputFormat创建RecordReader，循环读入FileSplits的内容生成Key与Value，传给Mapper函数，处理完后中间结果写成SequenceFile.<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Reducer Task 从运行Mapper的TaskTracker的Jetty上使用http协议获取所需的中间内容（33%），Sort/Merge后（66%），执行Reducer函数，最后按照OutputFormat写入结果目录。</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TaskTracker 每10秒向JobTracker报告一次运行情况，每完成一个Task10秒后，就会向JobTracker索求下一个Task。</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nutch项目的全部数据处理都构建在<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>之上，详见<a href="http://wiki.apache.org/lucene-hadoop-data/attachments/HadoopPresentations/attachments/yahoo-sds.pdf" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><font color="#006600" style="line-height: normal; ">Scalable Computing with&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span></font></a>&nbsp;。</p>
<h2 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 二、程序员编写的代码</h2>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp; 我们做一个简单的分布式的Grep，简单对输入文件进行逐行的正则匹配，如果符合就将该行打印到输出文件。因为是简单的全部输出，所以我们只要写Mapper函数，不用写Reducer函数，也不用定义Input/Output Format。</p>
<div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; color: #555b6e; "><span style="line-height: normal; color: #0000ff; ">package</span>&nbsp;<span style="line-height: normal; ">demo.<font style="line-height: normal; background-color: #ffff00; "><span class="hilite1" style="line-height: normal; ">hadoop</span><br style="line-height: normal; " />
</font></span><span style="line-height: normal; "><br style="line-height: normal; " />
public</span>&nbsp;<span style="line-height: normal; ">class</span>&nbsp;<span style="line-height: normal; ">HadoopGrep {<br style="line-height: normal; " />
<br style="line-height: normal; " />
</span><span style="line-height: normal; ">public</span>&nbsp;<span style="line-height: normal; ">static</span>&nbsp;<span style="line-height: normal; ">class</span>&nbsp;<span style="line-height: normal; ">RegMapper&nbsp;</span><span style="line-height: normal; ">extends</span>&nbsp;<span style="line-height: normal; ">MapReduceBase&nbsp;</span><span style="line-height: normal; ">implements</span>&nbsp;<span style="line-height: normal; ">Mapper {<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">private</span>&nbsp;<span style="line-height: normal; ">Pattern pattern;<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">public</span>&nbsp;<span style="line-height: normal; ">void</span>&nbsp;<span style="line-height: normal; ">configure(JobConf job) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; pattern&nbsp;</span><span style="line-height: normal; ">=</span>&nbsp;<span style="line-height: normal; ">Pattern.compile(job.get(</span>&nbsp;<span style="line-height: normal; ">"</span>&nbsp;<span style="line-height: normal; ">mapred.mapper.regex</span>&nbsp;<span style="line-height: normal; ">"</span>&nbsp;<span style="line-height: normal; ">));<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">public</span>&nbsp;<span style="line-height: normal; ">void</span>&nbsp;<span style="line-height: normal; ">map(WritableComparable key, Writable value, OutputCollector output, Reporter reporter)<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">throws</span>&nbsp;<span style="line-height: normal; ">IOException {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; String text&nbsp;</span><span style="line-height: normal; ">=</span>&nbsp;<span style="line-height: normal; ">((Text) value).toString();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; Matcher matcher&nbsp;</span><span style="line-height: normal; ">=</span>&nbsp;<span style="line-height: normal; ">pattern.matcher(text);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">if</span>&nbsp;<span style="line-height: normal; ">(matcher.find()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; output.collect(key, value);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
}<br style="line-height: normal; " />
<br style="line-height: normal; " />
</span><span style="line-height: normal; ">private</span>&nbsp;<span style="line-height: normal; ">HadoopGrep () {<br style="line-height: normal; " />
}&nbsp;</span><span style="line-height: normal; ">//</span>&nbsp;<span style="line-height: normal; ">singleton</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
</span><span style="line-height: normal; "><br style="line-height: normal; " />
</span><span style="line-height: normal; ">public</span>&nbsp;<span style="line-height: normal; ">static</span>&nbsp;<span style="line-height: normal; ">void</span>&nbsp;<span style="line-height: normal; ">main(String[] args)&nbsp;</span><span style="line-height: normal; ">throws</span>&nbsp;<span style="line-height: normal; ">Exception {<br style="line-height: normal; " />
&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp; JobConf grepJob&nbsp;</span><span style="line-height: normal; ">=</span>&nbsp;<span style="line-height: normal; ">new</span>&nbsp;<span style="line-height: normal; ">JobConf(HadoopGrep.</span>&nbsp;<span style="line-height: normal; ">class</span>&nbsp;<span style="line-height: normal; ">);<br style="line-height: normal; " />
&nbsp;&nbsp; grepJob.setJobName(</span>&nbsp;<span style="line-height: normal; ">"</span>&nbsp;<span style="line-height: normal; ">grep-search</span>&nbsp;<span style="line-height: normal; ">"</span>&nbsp;<span style="line-height: normal; ">);<br style="line-height: normal; " />
&nbsp;&nbsp; grepJob.set(</span>&nbsp;<span style="line-height: normal; ">"</span>&nbsp;<span style="line-height: normal; ">mapred.mapper.regex</span>&nbsp;<span style="line-height: normal; ">"</span>&nbsp;<span style="line-height: normal; ">, args[</span>&nbsp;<span style="line-height: normal; ">2</span>&nbsp;<span style="line-height: normal; ">]);<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp; grepJob.setInputPath(</span>&nbsp;<span style="line-height: normal; ">new</span>&nbsp;<span style="line-height: normal; ">Path(args[</span>&nbsp;<span style="line-height: normal; ">0</span>&nbsp;<span style="line-height: normal; ">]));<br style="line-height: normal; " />
&nbsp;&nbsp; grepJob.setOutputPath(</span>&nbsp;<span style="line-height: normal; ">new</span>&nbsp;<span style="line-height: normal; ">Path(args[</span>&nbsp;<span style="line-height: normal; ">1</span>&nbsp;<span style="line-height: normal; ">]));<br style="line-height: normal; " />
&nbsp;&nbsp; grepJob.setMapperClass(RegMapper.</span>&nbsp;<span style="line-height: normal; ">class</span>&nbsp;<span style="line-height: normal; ">);<br style="line-height: normal; " />
&nbsp;&nbsp; grepJob.setReducerClass(IdentityReducer.</span>&nbsp;<span style="line-height: normal; ">class</span>&nbsp;<span style="line-height: normal; ">);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp; JobClient.runJob(grepJob);<br style="line-height: normal; " />
}<br style="line-height: normal; " />
}<br style="line-height: normal; " />
</span></div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RegMapper类的configure()函数接受由main函数传入的查找字符串，map() 函数进行正则匹配，key是行数，value是文件行的内容，符合的文件行放入中间结果。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main()函数定义由命令行参数传入的输入输出目录和匹配字符串，Mapper函数为RegMapper类，Reduce函数是什么都不做，直接把中间结果输出到最终结果的的IdentityReducer类，运行Job。</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong style="line-height: normal; ">整个代码非常简单，丝毫没有分布式编程的任何细节。</strong></p>
<h2 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 三.运行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>程序</h2>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>这方面的文档写得不全面，综合参考<a href="http://wiki.apache.org/lucene-hadoop/GettingStartedWithHadoop" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><font color="#006600" style="line-height: normal; ">GettingStartedWithHadoop</font></a>&nbsp;与<a href="http://wiki.apache.org/nutch/NutchHadoopTutorial" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; text-decoration: underline; "><span style="line-height: normal; color: #800080; ">Nutch&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>&nbsp;Tutorial</span><font color="#006600" style="line-height: normal; ">&nbsp;</font></span></a>两篇后，再碰了很多钉子才终于完整的跑起来了，记录如下：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
<br style="line-height: normal; " />
<strong style="line-height: normal; ">3.1 local运行模式<br style="line-height: normal; " />
</strong><br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 完全不进行任何分布式计算，不动用任何namenode,datanode的做法，适合一开始做调试代码。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 解压<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>，其中conf目录是配置目录，<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>的配置文件在<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-default.xml，如果要修改配置，不是直接修改该文件，而是修改<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-site.xml，将该属性在<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-site.xml里重新赋值。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-default.xml的默认配置已经是local运行，不用任何修改，配置目录里唯一必须修改的是<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-env.sh 里<span style="line-height: normal; ">JAVA_HOME</span>&nbsp;的位置。</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 将编译好的HadoopGrep与RegMapper.class 放入<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/build/classes/demo/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/目录 找一个比较大的log文件放入一个目录，然后运行</p>
<div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; color: #555b6e; "><span style="line-height: normal; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span></span>&nbsp;<span style="line-height: normal; ">/</span>&nbsp;<span style="line-height: normal; ">bin</span>&nbsp;<span style="line-height: normal; ">/</span>&nbsp;<span style="line-height: normal; "><span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;demo.<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>.HadoopGrep log文件所在目录 任意的输出目录 grep的字符串</span></div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查看输出目录的结果，查看<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/logs/里的运行日志。&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; 在重新运行前，先删掉输出目录。</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><strong style="line-height: normal; ">3.2 单机集群运行模式</strong></p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 现在来搞一下只有单机的集群.假设以完成3.1中的设置，本机名为hadoopserver<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 第1步.&nbsp;&nbsp;&nbsp; 然后修改<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-site.xml ，加入如下内容：</p>
<div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; color: #555b6e; "><span style="line-height: normal; color: #0000ff; ">&lt;</span>&nbsp;<span style="line-height: normal; ">property</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">name</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; ">fs.default.name</span>&nbsp;<span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">name</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">value</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; ">hadoopserver:9000</span>&nbsp;<span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">value</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
</span><span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">property</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
</span><span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">property</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">name</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; ">mapred.job.tracker</span>&nbsp;<span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">name</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">value</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; ">hadoopserver:9001</span>&nbsp;<span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">value</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
</span><span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">property</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
</span><span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">property</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">name</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; ">dfs.replication</span>&nbsp;<span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">name</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
&nbsp;&nbsp;</span>&nbsp;<span style="line-height: normal; ">&lt;</span>&nbsp;<span style="line-height: normal; ">value</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; ">1</span>&nbsp;<span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">value</span>&nbsp;<span style="line-height: normal; ">&gt;</span>&nbsp;<span style="line-height: normal; "><br style="line-height: normal; " />
</span><span style="line-height: normal; ">&lt;/</span>&nbsp;<span style="line-height: normal; ">property</span>&nbsp;<span style="line-height: normal; ">&gt;</span></div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 从此就将运行从local文件系统转向了<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>的hdfs系统，mapreduce的jobtracker也从local的进程内操作变成了分布式的任务系统，9000，9001两个端口号是随便选择的两个空余端口号。<br style="line-height: normal; " />
<br style="line-height: normal; " />
另外，如果你的/tmp目录不够大，可能还要修改<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>.tmp.dir属性。</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><br style="line-height: normal; " />
第2步. 增加ssh不输入密码即可登陆。<br style="line-height: normal; " />
<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 因为<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>需要不用输入密码的ssh来进行调度，在不su的状态下，在自己的home目录运行ssh-keygen -t rsa ,然后一路回车生成密钥，再进入.ssh目录,cp id_rsa.pub authorized_keys<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 详细可以man 一下ssh, 此时执行ssh hadoopserver，不需要输入任何密码就能进入了。</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">3.格式化namenode，执行<br style="line-height: normal; " />
&nbsp;&nbsp; bin/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;namenode -format<br style="line-height: normal; " />
<br style="line-height: normal; " />
4.启动<font style="line-height: normal; background-color: #ffff00; "><span class="hilite1" style="line-height: normal; ">Hadoop</span><br style="line-height: normal; " />
</font>&nbsp;&nbsp;&nbsp;&nbsp; 执行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/start-all.sh, 在本机启动namenode,datanode,jobtracker,tasktracker<br style="line-height: normal; " />
<br style="line-height: normal; " />
5.现在将待查找的log文件放入hdfs,。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 执行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;dfs 可以看到它所支持的文件操作指令。<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 执行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;dfs put log文件所在目录 in ，则log文件目录已放入hdfs的/user/user-name/in 目录中</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">6.现在来执行Grep操作<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;demo.<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>.HadoopGrep in out<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查看<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/logs/里的运行日志，重新执行前。运行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;dfs rmr out 删除out目录。<br style="line-height: normal; " />
<br style="line-height: normal; " />
7.运行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/stop-all.sh 结束</p>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "><strong style="line-height: normal; ">3.3 集群运行模式<br style="line-height: normal; " />
</strong>假设已执行完3.2的配置，假设第2台机器名是hadoopserver2<br style="line-height: normal; " />
1.创建与hadoopserver同样的执行用户，将<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; "><span style="background-color: yellow; "><span style="background-color: yellow; "><span style="background-color: #f6fcfe; "><span style="background-color: #f6fcfe; ">hadoop</span></span></span></span></font></span>解压到相同的目录。<br style="line-height: normal; " />
<br style="line-height: normal; " />
2.同样的修改haoop-env.sh中的JAVA_HOME 及修改与3.2同样<span style="background-color: #f6fcfe; "><span style="background-color: #f6fcfe; ">的<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; "><span style="background-color: #f6fcfe; ">hadoop</span></font></span>-sit</span></span>e.xml<br style="line-height: normal; " />
<br style="line-height: normal; " />
3. 将hadoopserver中的/home/username/.ssh/authorized_keys 复制到hadoopserver2,保证hadoopserver可以无需密码登陆hadoopserver2<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; scp /home/username/.ssh/authorized_keys&nbsp;<a href="mailto:username@hadoopserver2:/home/username/.ssh/authorized_keys" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><font color="#006600" style="line-height: normal; ">username@hadoopserver2:/home/username/.ssh/authorized_keys</font></a>&nbsp;<br style="line-height: normal; " />
<br style="line-height: normal; " />
4.修改<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-server的<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/conf/slaves文件, 增加集群的节点，将localhost改为<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-server<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-server2<br style="line-height: normal; " />
<br style="line-height: normal; " />
5.在<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-server执行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/start-all.sh<br style="line-height: normal; " />
&nbsp;&nbsp; 将会在<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-server启动namenode,datanode,jobtracker,tasktracker<br style="line-height: normal; " />
&nbsp;&nbsp; 在<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>-server2启动datanode 和tasktracker<br style="line-height: normal; " />
<br style="line-height: normal; " />
6.现在来执行Grep操作<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;demo.<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>.HadoopGrep in out<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; 重新执行前,运行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;dfs rmr out 删除out目录<br style="line-height: normal; " />
<br style="line-height: normal; " />
7.运行<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>/bin/stop-all.sh 结束。</p>
<h2 style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">四、效率</h2>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; ">&nbsp;&nbsp; 经测试，<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">Hadoop</font></span>并不是万用灵丹，很取决于文件的大小和数量，处理的复杂度以及群集机器的数量，相连的带宽，当以上四者并不大时，<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>优势并不明显。<br style="line-height: normal; " />
&nbsp;&nbsp; 比如，不用<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>用java写的简单grep函数处理100M的log文件只要4秒，用了<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>&nbsp;local的方式运行是14秒，用了<span class="hilite1" style="line-height: normal; "><font style="line-height: normal; background-color: #ffff00; ">hadoop</font></span>单机集群的方式是30秒，用双机集群10M网口的话更慢，慢到不好意思说出来的地步。</p>
</div>
</span>
<div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "></p>
</div>
</span>
<div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "></p>
</div>
</span>
<div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "></p>
</div>
</span>
<div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "></p>
</div>
</span>
<div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "></p>
</div>
</span>
<div>
<p style="line-height: normal; color: #555b6e; font-family: Arial; font-size: 12px; "></p>
</div>
<img src ="http://www.blogjava.net/hengheng123456789/aggbug/341475.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hengheng123456789/" target="_blank">哼哼</a> 2010-12-24 16:25 <a href="http://www.blogjava.net/hengheng123456789/archive/2010/12/24/341475.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>