﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-昊天-随笔分类-大数据</title><link>http://www.blogjava.net/hao446tian/category/55153.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 30 Dec 2016 04:10:27 GMT</lastBuildDate><pubDate>Fri, 30 Dec 2016 04:10:27 GMT</pubDate><ttl>60</ttl><item><title>HBase数据模型【转】</title><link>http://www.blogjava.net/hao446tian/archive/2016/12/30/432218.html</link><dc:creator>昊天</dc:creator><author>昊天</author><pubDate>Fri, 30 Dec 2016 03:44:00 GMT</pubDate><guid>http://www.blogjava.net/hao446tian/archive/2016/12/30/432218.html</guid><wfw:comment>http://www.blogjava.net/hao446tian/comments/432218.html</wfw:comment><comments>http://www.blogjava.net/hao446tian/archive/2016/12/30/432218.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hao446tian/comments/commentRss/432218.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hao446tian/services/trackbacks/432218.html</trackback:ping><description><![CDATA[<h1><a href="http://www.cnblogs.com/skyl/p/4798630.html" data-mce-href="http://www.cnblogs.com/skyl/p/4798630.html" style="color: #326ca6 !important; text-decoration: none; border-bottom-color: #000000;">HBase数据模型</a></h1><div style="color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><p style="margin: 0px;"><span data-mce-style="color: #ff0000;" style="color: #ff0000;"><img confluence-external-resource"="" src="http://images2015.cnblogs.com/blog/480293/201509/480293-20150929140714324-1441668670.png" data-image-src="http://images2015.cnblogs.com/blog/480293/201509/480293-20150929140714324-1441668670.png" data-mce-src="http://images2015.cnblogs.com/blog/480293/201509/480293-20150929140714324-1441668670.png" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;" alt="" /></span></p><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><span data-mce-style="color: #ff0000;" style="color: #ff0000;">Row Key</span></p><ol style="margin: 10px 0px 0px;"><li>类似于MySQL中的主键，HBase<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">天然自带的</span>，在创建时<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">不需要显示指定</span>。</li><li>HBase不支持条件查询和Order by等查询，读取记录只有三种方式：<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">&#9312;.</span>&nbsp;通过单个Row key访问【<span data-mce-style="color: #008000;" style="color: #008000;">速度很快,因为存在着索引机制</span>】<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">&#9313;.</span>&nbsp;给定Row key的range&nbsp;<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">&#9314;.</span>&nbsp;全表扫描</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Row key按照字典序存储</span>，要充分考虑排序存储这个特性，将经常一起读取的行存储放到一起(位置相关性)。<span data-mce-style="color: #0000ff;" style="color: #0000ff;">设计方法参见</span><span data-mce-style="color: #0000ff;" style="color: #0000ff;"><a href="http://www.cnblogs.com/skyl/p/4802051.html" data-mce-href="http://www.cnblogs.com/skyl/p/4802051.html" data-mce-style="text-decoration: underline;" style="color: #326ca6 !important;"><span data-mce-style="color: #0000ff;" style="color: #0000ff;">HBase表设计</span></a></span></li></ol><ul data-mce-style="margin-left: 30.0px;" style="margin: 10px 0px 0px 30px;"><li>字典序对int排序的结果是1,10,100,11,2,20,21,&#8230;,9。要保持整形的自然序，行键<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">必须用0作左填充</span>。</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">行的一次读写是原子操作</span>&nbsp;(不论一次读写多少列)，使得多用户不能并发对同一个行进行更新操作。</li></ul><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><span data-mce-style="color: #ff0000;" style="color: #ff0000;">Column Family</span></p><ol style="margin: 10px 0px 0px;"><li>建表时手动指定，包含一个或者多个列</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">列族中的数据都是以二进制的形式保存在hdfs上，没有数据类型</span></li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">增加</span>新的列族：先disable 'users'&nbsp;<span data-mce-style="color: #0000ff;" style="color: #0000ff;">/</span><span data-mce-style="color: #008000;" style="color: #008000;">&nbsp;alter 'users','info'</span><span data-mce-style="color: #0000ff;" style="color: #0000ff;">&nbsp;/&nbsp;</span>enable 'users'。</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">删除</span>列族方式：先disable 'users'&nbsp;<span data-mce-style="color: #0000ff;" style="color: #0000ff;">/&nbsp;</span><span data-mce-style="color: #008000;" style="color: #008000;">alter 'users',{NAME=&gt;'info',METHOD=&gt;'delete'}</span><span data-mce-style="color: #0000ff;" style="color: #0000ff;">&nbsp;/</span>&nbsp;enable 'users'</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">不能重命名</span>列族：通常做法是使用API创建一个有着期望名称的新的列族，然后将数据复制过去，最后再删除旧的列族。</li><li><span data-mce-style="color: #0000ff;" style="color: #0000ff;">每个列族存储在HDFS上的一个单独文件中，空值不会被保存。</span></li><li>同一Column Family的Columns会群聚在一个存储文件上，并依Column key排序，因此设计时：<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">读写相关性较高的数据，存在同一列族中</span>。</li></ol><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><span data-mce-style="color: #ff0000;" style="color: #ff0000;">Column</span></p><ol style="margin: 10px 0px 0px;"><li>列名在<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">添加数据时</span><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">动态添加</span>，无需在建表时指定。<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">没有具体的数据类型，以二进制方式存储在HDFS上。</span></li><li>设置列值：put 'users','xiaoming','info:age','18'</li><li>读取列值：get 'users','info:age'&nbsp;</li></ol><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><span data-mce-style="color: #ff0000;" style="color: #ff0000;">TimeStamp</span></p><ol style="margin: 10px 0px 0px;"><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">默认值使用系统时间戳</span>，如果应用程序要避免数据时间戳冲突，就必须自己生成具有唯一性的时间戳。</li><li>每个cell中，不同版本的数据按照时间<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">倒序排列</span>，即最新的数据排在最前面。</li><li>为了避免数据存在过多版本造成的的管理 (包括存贮和索引)负担，HBase提供了两种数据版本回收方式。<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">一是保存数据的最后n个版本</span>，<span data-mce-style="color: #0000ff;" style="color: #0000ff;">二是保存最近一段时间内的版本</span>（即设置<span data-mce-style="color: #008000;" style="color: #008000;">HColumnDescriptor.setTimeToLive();&nbsp;</span>比如最近七天）。<span data-mce-style="color: #ff0000;" style="color: #ff0000;">用户可以针对每个列族单独进行设置</span>。</li></ol><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><span data-mce-style="color: #ff0000;" style="color: #ff0000;">Cell</span></p><ol style="margin: 10px 0px 0px;"><li>HBase中通过"&nbsp;<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">tableName + RowKey + ColumnKey&nbsp;</span>"确定的唯一存贮单元称为Cell。</li><li><span data-mce-style="color: #0000ff;" style="color: #0000ff;">每个Cell都保存着同一份数据的多个版本</span>，每个版本通过时间戳<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Time Stamp</span>来索引。</li><li>Cell的每个值通过4个键唯一索引，<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">tableName + RowKey + ColumnKey + Timestamp =&gt;&nbsp;<span data-mce-style="color: #0000ff;" style="color: #0000ff;">value</span></span></li></ol><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;"><span data-mce-style="color: #0000ff;" style="color: #0000ff;"><span data-mce-style="color: #ff0000;" style="color: #ff0000;">存储类型</span></span></span></p><ul data-mce-style="margin-left: 30.0px;" style="margin: 10px 0px 0px 30px;"><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">TableName</span>&nbsp;是字符串</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">RowKey</span>&nbsp;和&nbsp;<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">ColumnFamily</span>&nbsp;是二进制值（Java 类型 byte[]）</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Timestamp</span>&nbsp;是一个 64 位整数（Java 类型 long）</li><li><span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Value</span>&nbsp;是一个字节数组（Java类型 byte[]）。&nbsp;</li></ul><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><br /></p><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;">HBase是一种专门为半结构化数据（semistructured）和水平可扩展性（horizontalscalability）设计的数据库。<br />它把数据存储在表里。在表里，数据按照一个四维坐标系统来组织：行键、列族、列限定符和时间版本。<br />HBase 是无模式数据库，只需要提前定义列族。它也是无类型数据库，把所有数据不加解释地按照字节数组存储。<br />有5个基本命令用来访问HBase中的数据，即<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Get</span>、<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Put</span>、<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Delete</span>、<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Scan</span>&nbsp;和<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Increment</span>。<br />基于非行键值查询HBase的唯一办法是通过带过滤器的扫描<span data-mce-style="color: #ff00ff;" style="color: #ff00ff;">Scan</span>。</p><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><br /></p><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;">HBase 不是一个ACID 兼容数据库。但是HBase 提供一些保证，当你的应用系统访问HBase系统时，你可以用其来使你的应用系统的行为更加合理。这些保证具体如下。<br />1．操作是低级原子不可分的。换句话说，指定行的Put()要么整体成功要么整体失败回到操作开始前的状态，永远不会部分行写入而另一部分没有。这个要素和操作执行的列族的数量无关。<br />2．行间操作不是原子性的。不能保证所有操作整体成功或者失败。所有单行操作如上一点所述是原子性的。<br />3．checkAnd*和increment*操作是原子不可分的。<br />4．对于给定行的多个写操作，总是以每个写操作为整体彼此独立的。这是第一点的延伸。<br />5．对于给定行的任何Get()操作，返回系统当时所保存的完整行。<br />6．全表扫描不是对某个时间点表的快照的扫描。如果扫描已经开始，但是在行R 被扫描器对象读出之前，行R 被改变了，那么扫描器读出行R 更新后的版本。但是扫描器读出的数据是一致的，得到行R 更新后的完整行。</p><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;"><br /></p><p data-mce-style="margin-left: auto;" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px;">当你搭建使用HBase 的应用系统时，这些背景信息是你需要注意的要点。</p><ul style="margin: 10px 0px 0px;"><li>数据模型从逻辑上可以分类为键值存储或有序映射的映射。物理数据模型是基于列族的列式数据库，单个记录以键值形式存储。HBase 把数据记录保存在HFile里，这是一种不能更改的文件格式。因为记录一旦写入就不能修改，新值将保存在新HFile里。在读取数据和数据合并时，数据视图需要在内存中重新衔接。</li></ul><ul style="margin: 10px 0px 0px;"><li>HBase Java API 通过HTableInterface 来使用表。表连接可以直接通过构造HTable 实例来建立。使用HTable 实例系统开销大，优选方式是使用HTablePool，因为它可以重复使用连接。表通过HbaseAdmin 、HTableDescriptor 和HColumnDescriptor 类的实例来新建和操作。5 个命令通过相应的命令对象来使用：Get、Put、Delete、Scan 和Increment。命令送到HtableInterface 实例来执行。递增Increment 有另外一种用法，使用HTableInterface.incrementColumnValue()方法。执行Get、Scan 和Increment 命令的结果返回到Result和ResultScanner 对象的实例。一个KeyValue 实例代表一条返回记录。所有这些操作也可以通过HBase Shell 以命令行方式执行。&nbsp;</li></ul><ul style="margin: 10px 0px 0px;"><li>预期的数据访问模式对HBase 的模式设计有很大的影响。理想情况下，HBase 中的表根据预期的模式来组织。行键是HBase 中唯一的全局索引坐标，因此查询经常通过行键扫描实现。复合行键是支持这种扫描的常见做法。行键值经常希望是均衡分布的。诸如MD5 或SHA1 等散列算法通常用来实现这种均衡分布。</li></ul></div><img src ="http://www.blogjava.net/hao446tian/aggbug/432218.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hao446tian/" target="_blank">昊天</a> 2016-12-30 11:44 <a href="http://www.blogjava.net/hao446tian/archive/2016/12/30/432218.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HBase技术介绍【转】</title><link>http://www.blogjava.net/hao446tian/archive/2016/12/30/432217.html</link><dc:creator>昊天</dc:creator><author>昊天</author><pubDate>Fri, 30 Dec 2016 03:43:00 GMT</pubDate><guid>http://www.blogjava.net/hao446tian/archive/2016/12/30/432217.html</guid><wfw:comment>http://www.blogjava.net/hao446tian/comments/432217.html</wfw:comment><comments>http://www.blogjava.net/hao446tian/archive/2016/12/30/432217.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hao446tian/comments/commentRss/432217.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hao446tian/services/trackbacks/432217.html</trackback:ping><description><![CDATA[<h1>HBase简介</h1><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HBase &#8211; Hadoop Database，是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统，利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HBase是Google Bigtable的开源实现，类似Google Bigtable利用GFS作为其文件存储系统，HBase利用Hadoop HDFS作为其文件存储系统；Google运行MapReduce来处理Bigtable中的海量数据，HBase同样利用Hadoop MapReduce来处理HBase中的海量数据；Google Bigtable利用 Chubby作为协同服务，HBase利用Zookeeper作为对应。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://pic002.cnblogs.com/images/2012/392238/2012072720183629.jpg" data-image-src="http://pic002.cnblogs.com/images/2012/392238/2012072720183629.jpg" data-mce-src="http://pic002.cnblogs.com/images/2012/392238/2012072720183629.jpg" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">上图描述了Hadoop EcoSystem中的各层系统，其中HBase位于结构化存储层，Hadoop HDFS为HBase提供了高可靠性的底层存储支持，Hadoop MapReduce为HBase提供了高性能的计算能力，Zookeeper为HBase提供了稳定服务和failover机制。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">此外，Pig和Hive还为HBase提供了高层语言支持，使得在HBase上进行数据统计处理变的非常简单。 Sqoop则为HBase提供了方便的RDBMS数据导入功能，使得传统数据库数据向HBase中迁移变的非常方便。</p><h1>HBase访问接口</h1><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Native Java API，最常规和高效的访问方式，适合Hadoop MapReduce Job并行批处理HBase表数据</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HBase Shell，HBase的命令行工具，最简单的接口，适合HBase管理使用</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thrift Gateway，利用Thrift序列化技术，支持C++，PHP，Python等多种语言，适合其他异构系统在线访问HBase表数据</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; REST Gateway，支持REST 风格的Http API访问HBase, 解除了语言限制</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">5.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pig，可以使用Pig Latin流式编程语言来操作HBase中的数据，和Hive类似，本质最终也是编译成MapReduce Job来处理HBase表数据，适合做数据统计</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">6.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Hive，当前Hive的Release版本尚没有加入对HBase的支持，但在下一个版本Hive 0.7.0中将会支持HBase，可以使用类似SQL语言来访问HBase</p><h1>HBase数据模型</h1><h2>Table &amp; Column Family</h2><table style="border-collapse: collapse; margin: 10px 0px 0px; overflow-x: auto; color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; widows: auto;"><tbody><tr><td rowspan="2" style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><strong>Row Key</strong></td><td rowspan="2" style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><strong>Timestamp</strong></td><td colspan="2" style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><strong>Column Family</strong></td></tr><tr><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><strong>URI</strong></td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><strong>Parser</strong></td></tr><tr><td rowspan="3" style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">r1</td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">t3</td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">url=<a href="http://www.taobao.com/" data-mce-href="http://www.taobao.com" style="color: #326ca6 !important; text-decoration: none;">http://www.taobao.com</a></td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">title=天天特价</td></tr><tr><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">t2</td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">host=<a href="http://taobao.com/" data-mce-href="http://taobao.com" style="color: #326ca6 !important; text-decoration: none;">taobao.com</a></td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><br /></td></tr><tr><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">t1</td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><br /></td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><br /></td></tr><tr><td rowspan="2" style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">r2</td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">t5</td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">url=<a href="http://www.alibaba.com/" data-mce-href="http://www.alibaba.com" style="color: #326ca6 !important; text-decoration: none;">http://www.alibaba.com</a></td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">content=每天&#8230;</td></tr><tr><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">t4</td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;">host=<a href="http://alibaba.com/" data-mce-href="http://alibaba.com" style="color: #326ca6 !important; text-decoration: none;">alibaba.com</a></td><td style="white-space: pre-wrap; border-style: solid; border-color: #dddddd; padding: 7px 10px; vertical-align: top;"><br /></td></tr></tbody></table><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">&#216;&nbsp; Row Key: 行键，Table的主键，Table中的记录按照Row Key排序</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">&#216;&nbsp; Timestamp: 时间戳，每次数据操作对应的时间戳，可以看作是数据的version number</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">&#216;&nbsp; Column Family：列簇，Table在水平方向有一个或者多个Column Family组成，一个Column Family中可以由任意多个Column组成，即Column Family支持动态扩展，无需预先定义Column的数量以及类型，所有Column均以二进制格式存储，用户需要自行进行类型转换。</p><h2>Table &amp; Region</h2><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">当Table随着记录数不断增加而变大后，会逐渐分裂成多份splits，成为regions，一个region由[startkey,endkey)表示，不同的region会被Master分配给相应的RegionServer进行管理：</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://pic002.cnblogs.com/images/2012/392238/2012072720233561.jpg" data-image-src="http://pic002.cnblogs.com/images/2012/392238/2012072720233561.jpg" data-mce-src="http://pic002.cnblogs.com/images/2012/392238/2012072720233561.jpg" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><h2>-ROOT- &amp;&amp; .META. Table</h2><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HBase中有两张特殊的Table，-ROOT-和.META.</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">&#216;&nbsp; .META.：记录了用户表的Region信息，.META.可以有多个regoin</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">&#216;&nbsp; -ROOT-：记录了.META.表的Region信息，-ROOT-只有一个region</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">&#216;&nbsp; Zookeeper中记录了-ROOT-表的location</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://pic002.cnblogs.com/images/2012/392238/2012072720334342.jpg" data-image-src="http://pic002.cnblogs.com/images/2012/392238/2012072720334342.jpg" data-mce-src="http://pic002.cnblogs.com/images/2012/392238/2012072720334342.jpg" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">Client访问用户数据之前需要首先访问zookeeper，然后访问-ROOT-表，接着访问.META.表，最后才能找到用户数据的位置去访问，中间需要多次网络操作，不过client端会做cache缓存。</p><h1>MapReduce on HBase</h1><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">在HBase系统上运行批处理运算，最方便和实用的模型依然是MapReduce，如下图：</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://pic002.cnblogs.com/images/2012/392238/2012072720345464.jpg" data-image-src="http://pic002.cnblogs.com/images/2012/392238/2012072720345464.jpg" data-mce-src="http://pic002.cnblogs.com/images/2012/392238/2012072720345464.jpg" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HBase Table和Region的关系，比较类似HDFS File和Block的关系，HBase提供了配套的TableInputFormat和TableOutputFormat API，可以方便的将HBase Table作为Hadoop MapReduce的Source和Sink，对于MapReduce Job应用开发人员来说，基本不需要关注HBase系统自身的细节。</p><h1>HBase系统架构</h1><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://pic002.cnblogs.com/images/2012/392238/2012072720355658.jpg" data-image-src="http://pic002.cnblogs.com/images/2012/392238/2012072720355658.jpg" data-mce-src="http://pic002.cnblogs.com/images/2012/392238/2012072720355658.jpg" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><h2>Client</h2><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HBase Client使用HBase的RPC机制与HMaster和HRegionServer进行通信，对于管理类操作，Client与HMaster进行RPC；对于数据读写类操作，Client与HRegionServer进行RPC</p><h2>Zookeeper</h2><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">Zookeeper Quorum中除了存储了-ROOT-表的地址和HMaster的地址，HRegionServer也会把自己以Ephemeral方式注册到Zookeeper中，使得HMaster可以随时感知到各个HRegionServer的健康状态。此外，Zookeeper也避免了HMaster的单点问题，见下文描述</p><h2>HMaster</h2><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HMaster没有单点问题，HBase中可以启动多个HMaster，通过Zookeeper的Master Election机制保证总有一个Master运行，HMaster在功能上主要负责Table和Region的管理工作：</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 管理用户对Table的增、删、改、查操作</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 管理HRegionServer的负载均衡，调整Region分布</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在Region Split后，负责新Region的分配</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在HRegionServer停机后，负责失效HRegionServer 上的Regions迁移</p><h2>HRegionServer</h2><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HRegionServer主要负责响应用户I/O请求，向HDFS文件系统中读写数据，是HBase中最核心的模块。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://pic002.cnblogs.com/images/2012/392238/2012072720363620.jpg" data-image-src="http://pic002.cnblogs.com/images/2012/392238/2012072720363620.jpg" data-mce-src="http://pic002.cnblogs.com/images/2012/392238/2012072720363620.jpg" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HRegionServer内部管理了一系列HRegion对象，每个HRegion对应了Table中的一个Region，HRegion中由多个HStore组成。每个HStore对应了Table中的一个Column Family的存储，可以看出每个Column Family其实就是一个集中的存储单元，因此最好将具备共同IO特性的column放在一个Column Family中，这样最高效。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HStore存储是HBase存储的核心了，其中由两部分组成，一部分是MemStore，一部分是StoreFiles。MemStore是Sorted Memory Buffer，用户写入的数据首先会放入MemStore，当MemStore满了以后会Flush成一个StoreFile（底层实现是HFile），当StoreFile文件数量增长到一定阈值，会触发Compact合并操作，将多个StoreFiles合并成一个StoreFile，合并过程中会进行版本合并和数据删除，因此可以看出HBase其实只有增加数据，所有的更新和删除操作都是在后续的compact过程中进行的，这使得用户的写操作只要进入内存中就可以立即返回，保证了HBase I/O的高性能。当StoreFiles Compact后，会逐步形成越来越大的StoreFile，当单个StoreFile大小超过一定阈值后，会触发Split操作，同时把当前Region Split成2个Region，父Region会下线，新Split出的2个孩子Region会被HMaster分配到相应的HRegionServer上，使得原先1个Region的压力得以分流到2个Region上。下图描述了Compaction和Split的过程：</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://hi.csdn.net/attachment/201110/31/0_1320033404REoO.gif" data-image-src="http://hi.csdn.net/attachment/201110/31/0_1320033404REoO.gif" data-mce-src="http://hi.csdn.net/attachment/201110/31/0_1320033404REoO.gif" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">在理解了上述HStore的基本原理后，还必须了解一下HLog的功能，因为上述的HStore在系统正常工作的前提下是没有问题的，但是在分布式系统环境中，无法避免系统出错或者宕机，因此一旦HRegionServer意外退出，MemStore中的内存数据将会丢失，这就需要引入HLog了。每个HRegionServer中都有一个HLog对象，HLog是一个实现Write Ahead Log的类，在每次用户操作写入MemStore的同时，也会写一份数据到HLog文件中（HLog文件格式见后续），HLog文件定期会滚动出新的，并删除旧的文件（已持久化到StoreFile中的数据）。当HRegionServer意外终止后，HMaster会通过Zookeeper感知到，HMaster首先会处理遗留的 HLog文件，将其中不同Region的Log数据进行拆分，分别放到相应region的目录下，然后再将失效的region重新分配，领取 到这些region的HRegionServer在Load Region的过程中，会发现有历史HLog需要处理，因此会Replay HLog中的数据到MemStore中，然后flush到StoreFiles，完成数据恢复。</p><h2>HBase存储格式</h2><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HBase中的所有数据文件都存储在Hadoop HDFS文件系统上，主要包括上述提出的两种文件类型：</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HFile， HBase中KeyValue数据的存储格式，HFile是Hadoop的二进制格式文件，实际上StoreFile就是对HFile做了轻量级包装，即StoreFile底层就是HFile</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HLog File，HBase中WAL（Write Ahead Log） 的存储格式，物理上是Hadoop的Sequence File</p><h3>HFile</h3><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">下图是HFile的存储格式：</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://hi.csdn.net/attachment/201110/31/0_1320033407UxOg.gif" data-image-src="http://hi.csdn.net/attachment/201110/31/0_1320033407UxOg.gif" data-mce-src="http://hi.csdn.net/attachment/201110/31/0_1320033407UxOg.gif" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">首先HFile文件是不定长的，长度固定的只有其中的两块：Trailer和FileInfo。正如图中所示的，Trailer中有指针指向其他数据块的起始点。File Info中记录了文件的一些Meta信息，例如：AVG_KEY_LEN, AVG_VALUE_LEN, LAST_KEY, COMPARATOR, MAX_SEQ_ID_KEY等。Data Index和Meta Index块记录了每个Data块和Meta块的起始点。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">Data Block是HBase I/O的基本单元，为了提高效率，HRegionServer中有基于LRU的Block Cache机制。每个Data块的大小可以在创建一个Table的时候通过参数指定，大号的Block有利于顺序Scan，小号Block利于随机查询。每个Data块除了开头的Magic以外就是一个个KeyValue对拼接而成, Magic内容就是一些随机数字，目的是防止数据损坏。后面会详细介绍每个KeyValue对的内部构造。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HFile里面的每个KeyValue对就是一个简单的byte数组。但是这个byte数组里面包含了很多项，并且有固定的结构。我们来看看里面的具体结构：</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://hi.csdn.net/attachment/201110/31/0_1320033409u1Dx.gif" data-image-src="http://hi.csdn.net/attachment/201110/31/0_1320033409u1Dx.gif" data-mce-src="http://hi.csdn.net/attachment/201110/31/0_1320033409u1Dx.gif" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">开始是两个固定长度的数值，分别表示Key的长度和Value的长度。紧接着是Key，开始是固定长度的数值，表示RowKey的长度，紧接着是RowKey，然后是固定长度的数值，表示Family的长度，然后是Family，接着是Qualifier，然后是两个固定长度的数值，表示Time Stamp和Key Type（Put/Delete）。Value部分没有这么复杂的结构，就是纯粹的二进制数据了。</p><h3>HLogFile</h3><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;"><img confluence-external-resource"="" src="http://hi.csdn.net/attachment/201110/31/0_13200334124004.gif" data-image-src="http://hi.csdn.net/attachment/201110/31/0_13200334124004.gif" data-mce-src="http://hi.csdn.net/attachment/201110/31/0_13200334124004.gif" style="cursor: move; max-width: calc(100% - 4px); margin-left: 2px; margin-right: 2px; vertical-align: text-bottom;"  alt="" /></p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">上图中示意了HLog文件的结构，其实HLog文件就是一个普通的Hadoop Sequence File，Sequence File 的Key是HLogKey对象，HLogKey中记录了写入数据的归属信息，除了table和region名字外，同时还包括&nbsp;sequence number和timestamp，timestamp是&#8220;写入时间&#8221;，sequence number的起始值为0，或者是最近一次存入文件系统中sequence number。</p><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">HLog Sequece File的Value是HBase的KeyValue对象，即对应HFile中的KeyValue，可参见上文描述。</p><h1>结束</h1><p style="margin: 10px 0px 0px; word-wrap: break-word; color: #333333; font-family: Arial, sans-serif; line-height: 20px; widows: auto;">本文对HBase技术在功能和设计上进行了大致的介绍，由于篇幅有限，本文没有过多深入地描述HBase的一些细节技术。目前一淘的存储系统就是基于HBase技术搭建的，后续将介绍&#8220;一淘分布式存储系统&#8221;，通过实际案例来更多的介绍HBase应用。</p><img src ="http://www.blogjava.net/hao446tian/aggbug/432217.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hao446tian/" target="_blank">昊天</a> 2016-12-30 11:43 <a href="http://www.blogjava.net/hao446tian/archive/2016/12/30/432217.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Kafka剖析（一）：Kafka背景及架构介绍【转】</title><link>http://www.blogjava.net/hao446tian/archive/2016/12/30/432216.html</link><dc:creator>昊天</dc:creator><author>昊天</author><pubDate>Fri, 30 Dec 2016 03:41:00 GMT</pubDate><guid>http://www.blogjava.net/hao446tian/archive/2016/12/30/432216.html</guid><wfw:comment>http://www.blogjava.net/hao446tian/comments/432216.html</wfw:comment><comments>http://www.blogjava.net/hao446tian/archive/2016/12/30/432216.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hao446tian/comments/commentRss/432216.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hao446tian/services/trackbacks/432216.html</trackback:ping><description><![CDATA[<p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">Kafka是由LinkedIn开发的一个分布式的消息系统，使用Scala编写，它以可水平扩展和高吞吐率而被广泛使用。目前越来越多的开源分布式处理系统如Cloudera、Apache Storm、Spark都支持与Kafka集成。InfoQ一直在紧密关注<a href="http://www.infoq.com/cn/kafka/" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">Kafka的应用以及发展</a>，&#8220;Kafka剖析&#8221;专栏将会从架构设计、实现、应用场景、性能等方面深度解析Kafka。</p><h2>背景介绍</h2><h3>Kafka创建背景</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">Kafka是一个消息系统，原本开发自LinkedIn，用作LinkedIn的活动流（Activity Stream）和运营数据处理管道（Pipeline）的基础。现在它已被<a href="https://cwiki.apache.org/confluence/display/KAFKA/Powered+By" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">多家不同类型的公司</a>&nbsp;作为多种类型的数据管道和消息系统使用。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">活动流数据是几乎所有站点在对其网站使用情况做报表时都要用到的数据中最常规的部分。活动数据包括页面访问量（Page View）、被查看内容方面的信息以及搜索情况等内容。这种数据通常的处理方式是先把各种活动以日志的形式写入某种文件，然后周期性地对这些文件进行统计分析。运营数据指的是服务器的性能数据（CPU、IO使用率、请求时间、服务日志等等数据)。运营数据的统计方法种类繁多。</p><div style="margin: 0px; border: 0px; height: 0px; clear: both; font-size: 0px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"></div><div visible=""  stacked"="" style="margin: 20px 0px 20px 20px; border: 1px solid #dfdfdf; float: right; width: 315px; overflow: hidden; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; line-height: 25px; widows: auto; background-color: #ffffff;"><div only2=""  f_sponsorship"="" data-place="EMBEDDED" jsh="{&quot;topic&quot;:&quot;语言 &amp; 开发&quot;,&quot;id&quot;:1191,&quot;title&quot;:&quot;付钱拉&quot;}" style="margin: 0px; padding-top: 10px; border: 0px; float: left; width: 315px; clear: left;"><a href="http://www.infoq.com/infoq/url.action?i=15069&amp;t=f" target="_blank" style="margin: 0px; border: 0px; padding: 0px; color: #000000 !important; outline: none !important;"></a><div style="margin: 0px; padding-right: 10px; padding-left: 10px; border: 0px;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 295px;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;"><br /><br /></span></p></div></div><div style="margin: 0px; border: 0px; height: 0px; clear: both; font-size: 0px;"></div></div><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">近年来，活动和运营数据处理已经成为了网站软件产品特性中一个至关重要的组成部分，这就需要一套稍微更加复杂的基础设施对其提供支持。</p><h3>Kafka简介</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">Kafka是一种分布式的，基于发布/订阅的消息系统。主要设计目标如下：</p><ul style="margin: 0px 0px 15px 10px; padding: 0px; border: 0px; clear: left; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; line-height: 25px; widows: auto; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">以时间复杂度为O(1)的方式提供消息持久化能力，即使对TB级以上数据也能保证常数时间复杂度的访问性能。</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">高吞吐率。即使在非常廉价的商用机器上也能做到单机支持每秒100K条以上消息的传输。</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">支持Kafka Server间的消息分区，及分布式消费，同时保证每个Partition内的消息顺序传输。</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">同时支持离线数据处理和实时数据处理。</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">Scale out：支持在线水平扩展。</li></ul><h3>为何使用消息系统</h3><ul style="margin: 0px 0px 15px 10px; padding: 0px; border: 0px; clear: left; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; line-height: 25px; widows: auto; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">解耦</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">在项目启动之初来预测将来项目会碰到什么需求，是极其困难的。消息系统在处理过程中间插入了一个隐含的、基于数据的接口层，两边的处理过程都要实现这一接口。这允许你独立的扩展或修改两边的处理过程，只要确保它们遵守同样的接口约束。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">冗余</span><div id="lowerFullwidthVCR" style="margin: 0px; border: 0px;"></div><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">有些情况下，处理数据的过程会失败。除非数据被持久化，否则将造成丢失。消息队列把数据进行持久化直到它们已经被完全处理，通过这一方式规避了数据丢失风险。许多消息队列所采用的"插入-获取-删除"范式中，在把一个消息从队列中删除之前，需要你的处理系统明确的指出该消息已经被处理完毕，从而确保你的数据被安全的保存直到你使用完毕。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">扩展性</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">因为消息队列解耦了你的处理过程，所以增大消息入队和处理的频率是很容易的，只要另外增加处理过程即可。不需要改变代码、不需要调节参数。扩展就像调大电力按钮一样简单。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">灵活性 &amp; 峰值处理能力</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">在访问量剧增的情况下，应用仍然需要继续发挥作用，但是这样的突发流量并不常见；如果为以能处理这类峰值访问为标准来投入资源随时待命无疑是巨大的浪费。使用消息队列能够使关键组件顶住突发的访问压力，而不会因为突发的超负荷的请求而完全崩溃。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">可恢复性</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">系统的一部分组件失效时，不会影响到整个系统。消息队列降低了进程间的耦合度，所以即使一个处理消息的进程挂掉，加入队列中的消息仍然可以在系统恢复后被处理。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">顺序保证</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">在大多使用场景下，数据处理的顺序都很重要。大部分消息队列本来就是排序的，并且能保证数据会按照特定的顺序来处理。Kafka保证一个Partition内的消息的有序性。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">缓冲</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">在任何重要的系统中，都会有需要不同的处理时间的元素。例如，加载一张图片比应用过滤器花费更少的时间。消息队列通过一个缓冲层来帮助任务最高效率的执行&#8212;&#8212;&#8212;写入队列的处理会尽可能的快速。该缓冲有助于控制和优化数据流经过系统的速度。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">异步通信</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">很多时候，用户不想也不需要立即处理消息。消息队列提供了异步处理机制，允许用户把一个消息放入队列，但并不立即处理它。想向队列中放入多少消息就放多少，然后在需要的时候再去处理它们。</p></li></ul><h3>常用Message Queue对比</h3><ul style="margin: 0px 0px 15px 10px; padding: 0px; border: 0px; clear: left; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; line-height: 25px; widows: auto; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">RabbitMQ</span></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">RabbitMQ是使用Erlang编写的一个开源的消息队列，本身支持很多的协议：AMQP，XMPP, SMTP, STOMP，也正因如此，它非常重量级，更适合于企业级的开发。同时实现了Broker构架，这意味着消息在发送给客户端时先在中心队列排队。对路由，负载均衡或者数据持久化都有很好的支持。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Redis</span></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">Redis是一个基于Key-Value对的NoSQL数据库，开发维护很活跃。虽然它是一个Key-Value数据库存储系统，但它本身支持MQ功能，所以完全可以当做一个轻量级的队列服务来使用。对于RabbitMQ和Redis的入队和出队操作，各执行100万次，每10万次记录一次执行时间。测试数据分为128Bytes、512Bytes、1K和10K四个不同大小的数据。实验表明：入队时，当数据比较小时Redis的性能要高于RabbitMQ，而如果数据大小超过了10K，Redis则慢的无法忍受；出队时，无论数据大小，Redis都表现出非常好的性能，而RabbitMQ的出队性能则远低于Redis。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">ZeroMQ</span></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">ZeroMQ号称最快的消息队列系统，尤其针对大吞吐量的需求场景。ZeroMQ能够实现RabbitMQ不擅长的高级/复杂的队列，但是开发人员需要自己组合多种技术框架，技术上的复杂度是对这MQ能够应用成功的挑战。ZeroMQ具有一个独特的非中间件的模式，你不需要安装和运行一个消息服务器或中间件，因为你的应用程序将扮演这个服务器角色。你只需要简单的引用ZeroMQ程序库，可以使用NuGet安装，然后你就可以愉快的在应用程序之间发送消息了。但是ZeroMQ仅提供非持久性的队列，也就是说如果宕机，数据将会丢失。其中，Twitter的Storm 0.9.0以前的版本中默认使用ZeroMQ作为数据流的传输（Storm从0.9版本开始同时支持ZeroMQ和Netty作为传输模块）。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">ActiveMQ</span></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">ActiveMQ是Apache下的一个子项目。 类似于ZeroMQ，它能够以代理人和点对点的技术实现队列。同时类似于RabbitMQ，它少量代码就可以高效地实现高级应用场景。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Kafka/Jafka</span></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">Kafka是Apache下的一个子项目，是一个高性能跨语言分布式发布/订阅消息队列系统，而Jafka是在Kafka之上孵化而来的，即Kafka的一个升级版。具有以下特性：快速持久化，可以在O(1)的系统开销下进行消息持久化；高吞吐，在一台普通的服务器上既可以达到10W/s的吞吐速率；完全的分布式系统，Broker、Producer、Consumer都原生自动支持分布式，自动实现负载均衡；支持Hadoop数据并行加载，对于像Hadoop的一样的日志数据和离线分析系统，但又要求实时处理的限制，这是一个可行的解决方案。Kafka通过Hadoop的并行加载机制统一了在线和离线的消息处理。Apache Kafka相对于ActiveMQ是一个非常轻量级的消息系统，除了性能非常好之外，还是一个工作良好的分布式系统。</p></li></ul><h2>Kafka架构</h2><h3>Terminology</h3><ul style="margin: 0px 0px 15px 10px; padding: 0px; border: 0px; clear: left; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; line-height: 25px; widows: auto; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Broker</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">Kafka集群包含一个或多个服务器，这种服务器被称为broker</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Topic</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">每条发布到Kafka集群的消息都有一个类别，这个类别被称为Topic。（物理上不同Topic的消息分开存储，逻辑上一个Topic的消息虽然保存于一个或多个broker上但用户只需指定消息的Topic即可生产或消费数据而不必关心数据存于何处）</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Partition</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">Parition是物理上的概念，每个Topic包含一个或多个Partition.</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Producer</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">负责发布消息到Kafka broker</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Consumer</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">消息消费者，向Kafka broker读取消息的客户端。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">Consumer Group</span><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">每个Consumer属于一个特定的Consumer Group（可为每个Consumer指定group name，若不指定group name则属于默认的group）。</p></li></ul><h3>Kafka拓扑结构</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310020.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">如上图所示，一个典型的Kafka集群中包含若干Producer（可以是web前端产生的Page View，或者是服务器日志，系统CPU、Memory等），若干broker（Kafka支持水平扩展，一般broker数量越多，集群吞吐率越高），若干Consumer Group，以及一个<a href="http://zookeeper.apache.org/" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">Zookeeper</a>集群。Kafka通过Zookeeper管理集群配置，选举leader，以及在Consumer Group发生变化时进行rebalance。Producer使用push模式将消息发布到broker，Consumer使用pull模式从broker订阅并消费消息。</p><h3>Topic &amp; Partition</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">Topic在逻辑上可以被认为是一个queue，每条消费都必须指定它的Topic，可以简单理解为必须指明把这条消息放进哪个queue里。为了使得Kafka的吞吐率可以线性提高，物理上把Topic分成一个或多个Partition，每个Partition在物理上对应一个文件夹，该文件夹下存储这个Partition的所有消息和索引文件。若创建topic1和topic2两个topic，且分别有13个和19个分区，则整个集群上会相应会生成共32个文件夹（本文所用集群共8个节点，此处topic1和topic2 replication-factor均为1），如下图所示。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310021.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">每个日志文件都是一个log entrie序列，每个log entrie包含一个4字节整型数值（值为N+5），1个字节的"magic value"，4个字节的CRC校验码，其后跟N个字节的消息体。每条消息都有一个当前Partition下唯一的64字节的offset，它指明了这条消息的起始位置。磁盘上存储的消息格式如下：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, 'Andale Mono', monospace; color: #314e64; width: 597.796875px; overflow: auto; clear: none; widows: auto; float: none !important; background-color: #f5f2f0; background-position: -29px 0px;">message length ： 4 bytes (value: 1+4+n) "magic" value ： 1 byte  crc ： 4 bytes  payload ： n bytes </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">这个log entries并非由一个文件构成，而是分成多个segment，每个segment以该segment第一条消息的offset命名并以&#8220;.kafka&#8221;为后缀。另外会有一个索引文件，它标明了每个segment下包含的log entry的offset范围，如下图所示。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310022.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">因为每条消息都被append到该Partition中，属于顺序写磁盘，因此效率非常高（经验证，顺序写磁盘效率比随机写内存还要高，这是Kafka高吞吐率的一个很重要的保证）。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310023.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">对于传统的message queue而言，一般会删除已经被消费的消息，而Kafka集群会保留所有的消息，无论其被消费与否。当然，因为磁盘限制，不可能永久保留所有数据（实际上也没必要），因此Kafka提供两种策略删除旧数据。一是基于时间，二是基于Partition文件大小。例如可以通过配置$KAFKA_HOME/config/server.properties，让Kafka删除一周前的数据，也可在Partition文件超过1GB时删除旧数据，配置如下所示。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, 'Andale Mono', monospace; color: #314e64; width: 597.796875px; overflow: auto; clear: none; widows: auto; float: none !important; background-color: #f5f2f0; background-position: -29px 0px;">　　 # The minimum age of a log file to be eligible for deletion log.retention.hours=168 # The maximum size of a log segment file. When this size is reached a new log segment will be created. log.segment.bytes=1073741824 # The interval at which log segments are checked to see if they can be deleted according to the retention policies log.retention.check.interval.ms=300000 # If log.cleaner.enable=true is set the cleaner will be enabled and individual logs can then be marked for log compaction. log.cleaner.enable=false </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">这里要注意，因为Kafka读取特定消息的时间复杂度为O(1)，即与文件大小无关，所以这里删除过期文件与提高Kafka性能无关。选择怎样的删除策略只与磁盘以及具体的需求有关。另外，Kafka会为每一个Consumer Group保留一些metadata信息&#8212;&#8212;当前消费的消息的position，也即offset。这个offset由Consumer控制。正常情况下Consumer会在消费完一条消息后递增该offset。当然，Consumer也可将offset设成一个较小的值，重新消费一些消息。因为offet由Consumer控制，所以Kafka broker是无状态的，它不需要标记哪些消息被哪些消费过，也不需要通过broker去保证同一个Consumer Group只有一个Consumer能消费某一条消息，因此也就不需要锁机制，这也为Kafka的高吞吐率提供了有力保障。</p><h3>Producer消息路由</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">Producer发送消息到broker时，会根据Paritition机制选择将其存储到哪一个Partition。如果Partition机制设置合理，所有消息可以均匀分布到不同的Partition里，这样就实现了负载均衡。如果一个Topic对应一个文件，那这个文件所在的机器I/O将会成为这个Topic的性能瓶颈，而有了Partition后，不同的消息可以并行写入不同broker的不同Partition里，极大的提高了吞吐率。可以在$KAFKA_HOME/config/server.properties中通过配置项num.partitions来指定新建Topic的默认Partition数量，也可在创建Topic时通过参数指定，同时也可以在Topic创建之后通过Kafka提供的工具修改。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">在发送一条消息时，可以指定这条消息的key，Producer根据这个key和Partition机制来判断应该将这条消息发送到哪个Parition。Paritition机制可以通过指定Producer的paritition. class这一参数来指定，该class必须实现kafka.producer.Partitioner接口。本例中如果key可以被解析为整数则将对应的整数与Partition总数取余，该消息会被发送到该数对应的Partition。（每个Parition都会有个序号,序号从0开始）</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, 'Andale Mono', monospace; color: #314e64; width: 597.796875px; overflow: auto; clear: none; widows: auto; float: none !important; background-color: #f5f2f0; background-position: -29px 0px;">import kafka.producer.Partitioner; import kafka.utils.VerifiableProperties;  public class JasonPartitioner&lt;T&gt; implements Partitioner {      public JasonPartitioner(VerifiableProperties verifiableProperties) {}      @Override     public int partition(Object key, int numPartitions) {         try {             int partitionNum = Integer.parseInt((String) key);             return Math.abs(Integer.parseInt((String) key) % numPartitions);         } catch (Exception e) {             return Math.abs(key.hashCode() % numPartitions);         }     } } </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">如果将上例中的类作为partition.class，并通过如下代码发送20条消息（key分别为0，1，2，3）至topic3（包含4个Partition）。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, 'Andale Mono', monospace; color: #314e64; width: 597.796875px; overflow: auto; clear: none; widows: auto; float: none !important; background-color: #f5f2f0; background-position: -29px 0px;">public void sendMessage() throws InterruptedException{ 　　for(int i = 1; i &lt;= 5; i++){ 　　      List messageList = new ArrayList&lt;KeyedMessage&lt;String, String&gt;&gt;(); 　　      for(int j = 0; j &lt; 4; j++）{ 　　          messageList.add(new KeyedMessage&lt;String, String&gt;("topic2", j+"", "The " + i + " message for key " + j)); 　　      } 　　      producer.send(messageList);     } 　　producer.close(); } </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">则key相同的消息会被发送并存储到同一个partition里，而且key的序号正好和Partition序号相同。（Partition序号从0开始，本例中的key也从0开始）。下图所示是通过Java程序调用Consumer后打印出的消息列表。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310024.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><h3>Consumer Group</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">（本节所有描述都是基于Consumer hight level API而非low level API）。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">使用Consumer high level API时，同一Topic的一条消息只能被同一个Consumer Group内的一个Consumer消费，但多个Consumer Group可同时消费这一消息。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310025.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">这是Kafka用来实现一个Topic消息的广播（发给所有的Consumer）和单播（发给某一个Consumer）的手段。一个Topic可以对应多个Consumer Group。如果需要实现广播，只要每个Consumer有一个独立的Group就可以了。要实现单播只要所有的Consumer在同一个Group里。用Consumer Group还可以将Consumer进行自由的分组而不需要多次发送消息到不同的Topic。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">实际上，Kafka的设计理念之一就是同时提供离线处理和实时处理。根据这一特性，可以使用Storm这种实时流处理系统对消息进行实时在线处理，同时使用Hadoop这种批处理系统进行离线处理，还可以同时将数据实时备份到另一个数据中心，只需要保证这三个操作所使用的Consumer属于不同的Consumer Group即可。下图是Kafka在Linkedin的一种简化部署示意图。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310026.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">下面这个例子更清晰地展示了Kafka Consumer Group的特性。首先创建一个Topic (名为topic1，包含3个Partition)，然后创建一个属于group1的Consumer实例，并创建三个属于group2的Consumer实例，最后通过Producer向topic1发送key分别为1，2，3的消息。结果发现属于group1的Consumer收到了所有的这三条消息，同时group2中的3个Consumer分别收到了key为1，2，3的消息。如下图所示。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161220-0322/resource/articles/kafka-analysis-part-1/zh/resources/0310027.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;" alt="" /></p><h3>Push vs. Pull</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">作为一个消息系统，Kafka遵循了传统的方式，选择由Producer向broker push消息并由Consumer从broker pull消息。一些logging-centric system，比如Facebook的<a href="https://github.com/facebookarchive/scribe" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">Scribe</a>和Cloudera的<a href="http://flume.apache.org/" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">Flume</a>，采用push模式。事实上，push模式和pull模式各有优劣。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">push模式很难适应消费速率不同的消费者，因为消息发送速率是由broker决定的。push模式的目标是尽可能以最快速度传递消息，但是这样很容易造成Consumer来不及处理消息，典型的表现就是拒绝服务以及网络拥塞。而pull模式则可以根据Consumer的消费能力以适当的速率消费消息。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">对于Kafka而言，pull模式更合适。pull模式可简化broker的设计，Consumer可自主控制消费消息的速率，同时Consumer可以自己控制消费方式&#8212;&#8212;即可批量消费也可逐条消费，同时还能选择不同的提交方式从而实现不同的传输语义。</p><h3>Kafka delivery guarantee</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">有这么几种可能的delivery guarantee：</p><ul style="margin: 0px 0px 15px 10px; padding: 0px; border: 0px; clear: left; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; line-height: 25px; widows: auto; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">At most once 消息可能会丢，但绝不会重复传输</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">At least one 消息绝不会丢，但可能会重复传输</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">Exactly once 每条消息肯定会被传输一次且仅传输一次，很多时候这是用户所想要的。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">当Producer向broker发送消息时，一旦这条消息被commit，因数replication的存在，它就不会丢。但是如果Producer发送数据给broker后，遇到网络问题而造成通信中断，那Producer就无法判断该条消息是否已经commit。虽然Kafka无法确定网络故障期间发生了什么，但是Producer可以生成一种类似于主键的东西，发生故障时幂等性的重试多次，这样就做到了Exactly once。截止到目前(Kafka 0.8.2版本，2015-03-04)，这一Feature还并未实现，有希望在Kafka未来的版本中实现。（所以目前默认情况下一条消息从Producer到broker是确保了At least once，可通过设置Producer异步发送实现At most once）。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">接下来讨论的是消息从broker到Consumer的delivery guarantee语义。（仅针对Kafka consumer high level API）。Consumer在从broker读取消息后，可以选择commit，该操作会在Zookeeper中保存该Consumer在该Partition中读取的消息的offset。该Consumer下一次再读该Partition时会从下一条开始读取。如未commit，下一次读取的开始位置会跟上一次commit之后的开始位置相同。当然可以将Consumer设置为autocommit，即Consumer一旦读到数据立即自动commit。如果只讨论这一读取消息的过程，那Kafka是确保了Exactly once。但实际使用中应用程序并非在Consumer读取完数据就结束了，而是要进行进一步处理，而数据处理与commit的顺序在很大程度上决定了消息从broker和consumer的delivery guarantee semantic。</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">读完消息先commit再处理消息。这种模式下，如果Consumer在commit后还没来得及处理消息就crash了，下次重新开始工作后就无法读到刚刚已提交而未处理的消息，这就对应于At most once</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">读完消息先处理再commit。这种模式下，如果在处理完消息之后commit之前Consumer crash了，下次重新开始工作时还会处理刚刚未commit的消息，实际上该消息已经被处理过了。这就对应于At least once。在很多使用场景下，消息都有一个主键，所以消息的处理往往具有幂等性，即多次处理这一条消息跟只处理一次是等效的，那就可以认为是Exactly once。（笔者认为这种说法比较牵强，毕竟它不是Kafka本身提供的机制，主键本身也并不能完全保证操作的幂等性。而且实际上我们说delivery guarantee 语义是讨论被处理多少次，而非处理结果怎样，因为处理方式多种多样，我们不应该把处理过程的特性&#8212;&#8212;如是否幂等性，当成Kafka本身的Feature）</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">如果一定要做到Exactly once，就需要协调offset和实际操作的输出。精典的做法是引入两阶段提交。如果能让offset和操作输入存在同一个地方，会更简洁和通用。这种方式可能更好，因为许多输出系统可能不支持两阶段提交。比如，Consumer拿到数据后可能把数据放到HDFS，如果把最新的offset和数据本身一起写到HDFS，那就可以保证数据的输出和offset的更新要么都完成，要么都不完成，间接实现Exactly once。（目前就high level API而言，offset是存于Zookeeper中的，无法存于HDFS，而low level API的offset是由自己去维护的，可以将之存于HDFS中）</p></li></ul><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 25px; clear: none; width: 610px; font-family: Helvetica, 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; widows: auto; background-color: #ffffff;">总之，Kafka默认保证At least once，并且允许通过设置Producer异步提交来实现At most once。而Exactly once要求与外部存储系统协作，幸运的是Kafka提供的offset可以非常直接非常容易得使用这种方式。</p><img src ="http://www.blogjava.net/hao446tian/aggbug/432216.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hao446tian/" target="_blank">昊天</a> 2016-12-30 11:41 <a href="http://www.blogjava.net/hao446tian/archive/2016/12/30/432216.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>