﻿<?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-Change Dir-随笔分类-数据</title><link>http://blogjava.net/changedi/category/53573.html</link><description>先知cd——热爱生活是一切艺术的开始</description><language>zh-cn</language><lastBuildDate>Thu, 26 Feb 2015 04:20:13 GMT</lastBuildDate><pubDate>Thu, 26 Feb 2015 04:20:13 GMT</pubDate><ttl>60</ttl><item><title>Leetcode-Database-181~183-3个easy题目连发</title><link>http://www.blogjava.net/changedi/archive/2015/02/06/422790.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Fri, 06 Feb 2015 05:39:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/02/06/422790.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422790.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/02/06/422790.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422790.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422790.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri" lang="en-US">181</span><span style="font-family:SimSun" lang="zh-CN">题目地址：</span><span style="font-family:Calibri" lang="zh-CN">https://oj.leetcode.com/problems/employees-earning-more-than-their-managers/</span></p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">181</span><span style="font-family:SimSun">题又是一个简单题目，给定一个</span><span style="font-family:Calibri">Employee</span><span style="font-family: SimSun">表，里面存储了雇员的工资信息，包括名字、工资、经理</span><span style="font-family: Calibri">ID</span><span style="font-family:SimSun">，题目要求写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">查找出所有那些自身工资比经理还高的雇员的名字。</span></p><p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+-------+--------+-----------+<br /> | Id | Name&nbsp; | Salary | ManagerId |<br /> +----+-------+--------+-----------+<br /> | 1&nbsp; | Joe&nbsp;&nbsp; | 70000&nbsp; | 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | 2&nbsp; | Henry | 80000&nbsp; | 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | 3&nbsp; | Sam&nbsp;&nbsp; | 60000&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | 4&nbsp; | Max&nbsp;&nbsp; | 90000&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> +----+-------+--------+-----------+</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个题目很简单，现有表不能做就是因为现有的一行记录里没有包含经理的工资信息，但是有经理的</span><span style="font-family:Calibri">ID</span><span style="font-family:SimSun">，那么我们做一下关联，把工资信息拿到，再过滤就好了，于是思路</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">如下：</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />Name&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;Employee<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span><br />o1.Name<br />,o1.Salary&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;s<br />,o2.Salary&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;m<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee<br />)o1<br /><span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.ManagerId<span style="color: #808080; ">=</span>o2.Id)<br />)t<br /><span style="color: #0000FF; ">where</span>&nbsp;s<span style="color: #808080; ">&gt;</span>m</div><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">其中</span><span style="font-family:Calibri">s</span><span style="font-family:SimSun">是自己的工资，</span><span style="font-family: Calibri">m</span><span style="font-family:SimSun">是经理的工资</span><span style="font-family:Calibri">~~</span><span style="font-family:SimSun">一目了然</span></p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">182</span><span style="font-family:SimSun">题目地址：</span><span style="font-family:Calibri">https://oj.leetcode.com/problems/duplicate-emails/</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">182</span><span style="font-family:宋体">也是</span><span style="font-family:Calibri">Easy</span><span style="font-family: 宋体">级别题目，题目描述就是写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">，把</span><span style="font-family:Calibri">Person</span><span style="font-family: 宋体">表中有重复</span><span style="font-family:Calibri">Email</span><span style="font-family:宋体">的记录拉出来。</span></p><p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+---------+<br /> | Id | Email&nbsp;&nbsp; |<br /> +----+---------+<br /> | 1&nbsp; | a@b.com |<br /> | 2&nbsp; | c@d.com |<br /> | 3&nbsp; | a@b.com |<br /> +----+---------+</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">很容易想到的思路：按照</span><span style="font-family:Calibri">email</span><span style="font-family: 宋体">做聚合，把</span><span style="font-family:Calibri">count&gt;1</span><span style="font-family:宋体">的取出来，对应</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">如下：</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;Email<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;Email<br />&nbsp;&nbsp;&nbsp;&nbsp;,<span style="color: #FF00FF; ">count</span>(Id)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;cnt<br /><span style="color: #0000FF; ">from</span>&nbsp;Person<br /><span style="color: #0000FF; ">group</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;Email<br />)t<br /><span style="color: #0000FF; ">where</span>&nbsp;cnt<span style="color: #808080; ">&gt;</span><span style="color: #800000; font-weight: bold; ">1</span></div><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">183</span><span style="font-family:宋体">题目地址：</span><span style="font-family:Calibri">https://oj.leetcode.com/problems/customers-who-never-order/</span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">183</span><span style="font-family:宋体">题，一个网站包含两张表，一张</span><span style="font-family:Calibri">Customers</span><span style="font-family: 宋体">表存放客户数据，一张</span><span style="font-family:Calibri">Orders</span><span style="font-family:宋体">表存放产生订单的客户</span><span style="font-family: Calibri">ID</span><span style="font-family:宋体">，题目要求写</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">查出没有在网站产生过订单的客户。说白了就是查询在</span><span style="font-family:Calibri">Customers</span><span style="font-family:宋体">里而不在</span><span style="font-family:Calibri">Orders</span><span style="font-family: 宋体">里的数据，</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">如下：</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />o1.Name&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;Customers<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Customers<br />)o1<br /><span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Orders<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.Id<span style="color: #808080; ">=</span>o2.CustomerId&nbsp;)<br /><span style="color: #0000FF; ">where</span>&nbsp;o2.CustomerId&nbsp;<span style="color: #0000FF; ">is</span>&nbsp;<span style="color: #0000FF; ">null</span><br />&nbsp;</div><p style="margin:0in;font-family:Calibri;font-size:10.5pt"><br /></p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:宋体">个简单题目，夯实</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">基础</span><span style="font-family:Calibri">~~</span></p><p style="margin:0in;font-size:10.5pt">                                                                                                                          </p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:16.0pt"><span style="font-family:Calibri"></span><span style="font-family:SimSun"></span></p><div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422790.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-02-06 13:39 <a href="http://www.blogjava.net/changedi/archive/2015/02/06/422790.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-180-Consecutive Numbers-Medium</title><link>http://www.blogjava.net/changedi/archive/2015/01/29/422554.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Thu, 29 Jan 2015 11:01:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/29/422554.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422554.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/29/422554.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422554.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422554.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">题目地址：</span><span style="font-family:Calibri"><a href="https://oj.leetcode.com/problems/consecutive-numbers/">https://oj.leetcode.com/problems/consecutive-numbers/</a></span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这个题目是要求写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">，查询出表中连续出现三次的记录。表结构非常简单如下：</span></p><p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+-----+<br /> | Id | Num |<br /> +----+-----+<br /> | 1&nbsp; |&nbsp; 1&nbsp; |<br /> | 2&nbsp; |&nbsp; 1&nbsp; |<br /> | 3&nbsp; |&nbsp; 1&nbsp; |<br /> | 4&nbsp; |&nbsp; 2&nbsp; |<br /> | 5&nbsp; |&nbsp; 1&nbsp; |<br /> | 6&nbsp; |&nbsp; 2&nbsp; |<br /> | 7&nbsp; |&nbsp; 2&nbsp; |<br /> +----+-----+</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这个</span><span style="font-family:Calibri">Logs</span><span style="font-family: 宋体">表里，只有</span><span style="font-family:Calibri">Id</span><span style="font-family:宋体">和</span><span style="font-family:Calibri">Num</span><span style="font-family:宋体">字段，而题目就是要找出连续出现</span><span style="font-family:Calibri">3</span><span style="font-family:宋体">次的</span><span style="font-family:Calibri">Num</span><span style="font-family:宋体">，对于这个表，答案就是</span><span style="font-family: Calibri">1</span><span style="font-family:宋体">了。</span></p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">思路很直观暴力的一个想法就是</span><span style="font-family:Calibri">Logs</span><span style="font-family: 宋体">表自己关联</span><span style="font-family:Calibri">3</span><span style="font-family:宋体">次，关联条件依次是</span><span style="font-family: Calibri">Id+1</span><span style="font-family:宋体">，这样就可以把连续记录关联出来了</span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">我的代码如下：</p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br /><span style="color: #0000FF; ">distinct</span>&nbsp;o1.Num<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs<br />)o1<br /><span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.Num<span style="color: #808080; ">=</span>o2.Num&nbsp;<span style="color: #808080; ">and</span>&nbsp;o1.Id<span style="color: #808080; ">=</span>o2.Id<span style="color: #808080; ">+</span><span style="color: #800000; font-weight: bold; ">1</span>)<br /><span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs<br />)o3<br /><span style="color: #0000FF; ">on</span>(o2.Num<span style="color: #808080; ">=</span>o3.Num&nbsp;<span style="color: #808080; ">and</span>&nbsp;o2.Id<span style="color: #808080; ">=</span>o3.Id<span style="color: #808080; ">+</span><span style="color: #800000; font-weight: bold; ">1</span>)</div><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这个题目虽然可以这样解掉，但是很自然的会联想，如果</span><span style="font-family:Calibri">3</span><span style="font-family:宋体">变成</span><span style="font-family:Calibri">n</span><span style="font-family:宋体">呢，题目变为求连续出现</span><span style="font-family: Calibri">n</span><span style="font-family:宋体">次的记录，那该如何解？显然暴力解法是不可行的。鉴于能力有限，我从</span><span style="font-family:Calibri">discuss</span><span style="font-family: 宋体">区找到了一个很赞的解法，通过定义变量，很巧妙的解了这个扩展的问题，原作者</span><a href="https://oj.leetcode.com/discuss/user/kent-huang"><span style="font-family: Calibri">kent-huang</span></a></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">代码如下：</p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #0000FF; ">DISTINCT</span>&nbsp;num&nbsp;<br /><span style="color: #0000FF; ">FROM</span>&nbsp;(<br />&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;num,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">case</span>&nbsp;<span style="color: #0000FF; ">when</span>&nbsp;<span style="color: #008000; ">@record</span>&nbsp;<span style="color: #808080; ">=</span>&nbsp;num&nbsp;<span style="color: #0000FF; ">then</span>&nbsp;<span style="color: #008000; ">@count</span>:<span style="color: #808080; ">=</span><span style="color: #008000; ">@count</span><span style="color: #808080; ">+</span><span style="color: #800000; font-weight: bold; ">1</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">when</span>&nbsp;<span style="color: #008000; ">@record</span>&nbsp;<span style="color: #808080; ">&lt;&gt;</span>&nbsp;<span style="color: #008000; ">@record</span>:<span style="color: #808080; ">=</span>num&nbsp;<span style="color: #0000FF; ">then</span>&nbsp;<span style="color: #008000; ">@count</span>:<span style="color: #808080; ">=</span><span style="color: #800000; font-weight: bold; ">1</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">end</span>&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;n&nbsp;<br />&nbsp;&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs&nbsp;,(<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">@count</span>:<span style="color: #808080; ">=</span><span style="color: #800000; font-weight: bold; ">0</span>,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">@record</span>:<span style="color: #808080; ">=</span>(<span style="color: #0000FF; ">SELECT</span>&nbsp;num&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs&nbsp;limit&nbsp;<span style="color: #800000; font-weight: bold; ">0</span>,<span style="color: #800000; font-weight: bold; ">1</span>)<br />&nbsp;&nbsp;)&nbsp;r&nbsp;<br />)&nbsp;a&nbsp;<br /><span style="color: #0000FF; ">where</span>&nbsp;a.n<span style="color: #808080; ">&gt;=</span><span style="color: #800000; font-weight: bold;">3</span></div><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">简单分析一下，作者通过定义两个变量</span><span style="font-family:Calibri">record</span><span style="font-family: 宋体">和</span><span style="font-family:Calibri">count</span><span style="font-family:宋体">来控制记录和对应的</span><span style="font-family: Calibri">rank</span><span style="font-family:宋体">值，首先通过一个</span><span style="font-family:Calibri">select @count:=0,@record:=(SELECT num from Logs limit 0,1)</span><span style="font-family:宋体">语句来初始化这两个变量</span><span style="font-family:Calibri">count=0</span><span style="font-family: 宋体">，</span><span style="font-family:Calibri">record=</span><span style="font-family:宋体">表里第一条记录的</span><span style="font-family:Calibri">num</span><span style="font-family:宋体">。接下来通过普通查询，将</span><span style="font-family:Calibri">Logs</span><span style="font-family: 宋体">表里每一条记录查出来，和</span><span style="font-family:Calibri">record</span><span style="font-family:宋体">对比，如果相同，则</span><span style="font-family: Calibri">count</span><span style="font-family:宋体">自增</span><span style="font-family:Calibri">1</span><span style="font-family:宋体">，如果不同，那么新的</span><span style="font-family:Calibri">record</span><span style="font-family:宋体">被赋值，同时</span><span style="font-family:Calibri">count</span><span style="font-family:宋体">置</span><span style="font-family:Calibri">1</span><span style="font-family:宋体">，很漂亮的自定义变量用</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">实现了我们直觉上需要用逻辑代码来完成的功能。而且这个代码的一大优势是不需要用到Id字段~~非常棒</span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt">                                                        </p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">还有好的思路，请一定分享给我</span><span style="font-family:Calibri">~~:)</span></p><p style="margin:0in;font-family:Calibri;font-size:16.0pt"></p><div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422554.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-29 19:01 <a href="http://www.blogjava.net/changedi/archive/2015/01/29/422554.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-178-Rank Scores-Medium</title><link>http://www.blogjava.net/changedi/archive/2015/01/28/422512.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 28 Jan 2015 08:50:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/28/422512.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422512.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/28/422512.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422512.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422512.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">题目地址：</span><a href="https://oj.leetcode.com/problems/rank-scores/"><span style="font-family: Calibri">https://oj.leetcode.com/problems/rank-scores/</span></a></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个问题很有趣，写一个类似</span><span style="font-family:Calibri">oracle</span><span style="font-family:SimSun">里的窗口函数</span><span style="font-family: Calibri">rank()</span><span style="font-family:SimSun">，具体描述一下，有一张数据表</span><span style="font-family:Calibri">Scores</span><span style="font-family: SimSun">，里面有两个字段</span><span style="font-family:Calibri">Id</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">Score</span><span style="font-family:SimSun">，具体结构如下：</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+-------+<br /> | Id | Score |<br /> +----+-------+<br /> | 1&nbsp; | 3.50&nbsp; |<br /> | 2&nbsp; | 3.65&nbsp; |<br /> | 3&nbsp; | 4.00&nbsp; |<br /> | 4&nbsp; | 3.85&nbsp; |<br /> | 5&nbsp; | 4.00&nbsp; |<br /> | 6&nbsp; | 3.65&nbsp; |<br /> +----+-------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">任务是要写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">来给</span><span style="font-family:Calibri">Score</span><span style="font-family:SimSun">字段打一个</span><span style="font-family:Calibri">rank</span><span style="font-family: SimSun">标识，条件是按照</span><span style="font-family:Calibri">Score</span><span style="font-family:SimSun">从大到小排序，相等情况时</span><span style="font-family:Calibri">rank</span><span style="font-family: SimSun">相同，且</span><span style="font-family:Calibri">rank</span><span style="font-family:SimSun">之间没有&#8220;洞&#8221;，即</span><span style="font-family: Calibri">rank</span><span style="font-family:SimSun">字段是连续值。显然这个任务比</span><span style="font-family:Calibri">rank</span><span style="font-family: SimSun">窗口函数容易一些，但是也是一个棘手的问题。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">题目具体给出了输出示例：</p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+-------+------+<br /> | Score | Rank |<br /> +-------+------+<br /> | 4.00&nbsp; | 1&nbsp;&nbsp;&nbsp; |<br /> | 4.00&nbsp; | 1&nbsp;&nbsp;&nbsp; |<br /> | 3.85&nbsp; | 2&nbsp;&nbsp;&nbsp; |<br /> |</span><span style="background:whitesmoke">&nbsp;3.65&nbsp; | 3&nbsp;&nbsp;&nbsp; |<br /> </span><span style="background:whitesmoke">| 3.65&nbsp; | 3&nbsp;&nbsp;&nbsp; |<br /> | 3.50&nbsp; | 4&nbsp;&nbsp;&nbsp; |<br /> +-------+------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">平常说实话</span><span style="font-family:Calibri">rank</span><span style="font-family:SimSun">函数或者</span><span style="font-family:Calibri">row_number</span><span style="font-family:SimSun">函数用多了，很少考虑实现，面对这个问题，硬着头皮用笛卡尔积的</span><span style="font-family:Calibri">join</span><span style="font-family: SimSun">解决了，若是在</span><span style="font-family:Calibri">hive</span><span style="font-family:SimSun">中，</span><span style="font-family:Calibri">strict</span><span style="font-family:SimSun">模式可能拒绝笛卡尔积的</span><span style="font-family:Calibri">join</span><span style="font-family: SimSun">，这时还是求助于窗口函数</span><span style="font-family:Calibri">rank</span><span style="font-family:SimSun">吧</span><span style="font-family:Calibri">~~</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">实现代码如下：</p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />o1.Score<br />,<span style="color: #FF00FF; ">count</span>(o2.Score)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;Rank<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Scores<br />)o1<br /><span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #0000FF; ">distinct</span>&nbsp;Score&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Scores<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.Score<span style="color: #808080; ">&lt;=</span>o2.Score)<br /><span style="color: #0000FF; ">group</span>&nbsp;<span style="color: #0000FF; ">by</span><br />o1.Id<br /><span style="color: #0000FF; ">order</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;o1.Score&nbsp;<span style="color: #0000FF; ">desc</span></div>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422512.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-28 16:50 <a href="http://www.blogjava.net/changedi/archive/2015/01/28/422512.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-177-Nth Highest Salary-Medium</title><link>http://www.blogjava.net/changedi/archive/2015/01/27/422482.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 27 Jan 2015 08:59:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/27/422482.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422482.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/27/422482.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422482.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422482.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">题目地址：</span><a href="https://oj.leetcode.com/problems/nth-highest-salary/"><span style="font-family:Calibri">https://oj.leetcode.com/problems/nth-highest-salary/</span></a></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个题目其实是</span><span style="font-family:Calibri">176</span><span style="font-family:SimSun">的扩展，刚才不是要找第二大的</span><span style="font-family:Calibri">salary</span><span style="font-family: SimSun">吗，那好，现在直接扩展到任意，第</span><span style="font-family:Calibri">N</span><span style="font-family:SimSun">大，而且这次是要写一个</span><span style="font-family:Calibri">Function</span><span style="font-family: SimSun">，</span><span style="font-family:Calibri">N</span><span style="font-family:SimSun">作为参数。表还是之前的</span><span style="font-family: Calibri">Employee</span><span style="font-family:SimSun">表。</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+--------+<br /> | Id | Salary |<br /> +----+--------+<br /> | 1&nbsp; | 100&nbsp;&nbsp;&nbsp; |<br /> | 2&nbsp; | 200&nbsp;&nbsp;&nbsp; |<br /> | 3&nbsp; | 300&nbsp;&nbsp;&nbsp; |<br /> +----+--------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">我不知道为什么这个题目的通过率那么低，但是事实是使用</span><span style="font-family:Calibri">176</span><span style="font-family:SimSun">题那篇文章的&#8220;错误&#8221;做法即可解掉这个题目。注意</span><span style="font-family:Calibri">limit</span><span style="font-family: SimSun">是从</span><span style="font-family:Calibri">0</span><span style="font-family:SimSun">开始，所以变量要默认自减</span><span style="font-family:Calibri">1</span><span style="font-family:SimSun">。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">CREATE</span>&nbsp;<span style="color: #0000FF; ">FUNCTION</span>&nbsp;getNthHighestSalary(N&nbsp;<span style="font-weight: bold;">INT</span>)&nbsp;<span style="color: #0000FF; ">RETURNS</span>&nbsp;<span style="font-weight: bold;">INT</span><br /><span style="color: #0000FF; ">BEGIN</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">declare</span>&nbsp;n1&nbsp;<span style="font-weight: bold;">int</span>;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">set</span>&nbsp;n1&nbsp;<span style="color: #808080; ">=</span>&nbsp;N<span style="color: #808080; ">-</span><span style="color: #800000; font-weight: bold; ">1</span>;<br />&nbsp;&nbsp;<span style="color: #0000FF; ">RETURN</span>&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;Write&nbsp;your&nbsp;MySQL&nbsp;query&nbsp;statement&nbsp;below.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Salary<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">from</span>(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #0000FF; ">distinct</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)t<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">order</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">desc</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;limit&nbsp;n1,<span style="color: #800000; font-weight: bold; ">1</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;);<br /><span style="color: #0000FF; ">END</span></div>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">大写的部分是题目已经给的框架，我们只需要在</span><span style="font-family:Calibri">Return</span><span style="font-family:SimSun">语句里填写内容即可，我</span><span style="font-family:Calibri">declare</span><span style="font-family: SimSun">了一个变量，不知道是否是一个</span><span style="font-family:Calibri">hack</span><span style="font-family:SimSun">手段，但是</span><span style="font-family:Calibri">it works</span><span style="font-family: SimSun">.</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">当然本着在大数据平台下sql的经验，如果换做是在hive下写这个，思路是什么呢？</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">，我建议直接写一个</span><span style="font-family:Calibri">UDAF</span><span style="font-family: SimSun">解决。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">，不具备</span><span style="font-family:Calibri">UDAF</span><span style="font-family: SimSun">能力的话，可以在一个</span><span style="font-family:Calibri">key</span><span style="font-family:SimSun">下做</span><span style="font-family:Calibri">sort by</span><span style="font-family: SimSun">后，把他们</span><span style="font-family:Calibri">group_concat</span><span style="font-family:SimSun">或者</span><span style="font-family:Calibri">wm_concat</span><span style="font-family:SimSun">起来，然后</span><span style="font-family:Calibri">get</span><span style="font-family:SimSun">第</span><span style="font-family:Calibri">n</span><span style="font-family:SimSun">个</span><span style="font-family:Calibri">item</span><span style="font-family:SimSun">即可，当然这会利用到</span><span style="font-family:Calibri">hive</span><span style="font-family: SimSun">的默认的几个</span><span style="font-family:Calibri">UDAF</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">UDF</span><span style="font-family:SimSun">。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">思路肯定有很多，欢迎大家一起来</span><span style="font-family:Calibri">share~</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422482.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-27 16:59 <a href="http://www.blogjava.net/changedi/archive/2015/01/27/422482.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-176-Second Highest Salary-Easy</title><link>http://www.blogjava.net/changedi/archive/2015/01/27/422478.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 27 Jan 2015 08:38:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/27/422478.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422478.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/27/422478.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422478.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422478.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">leetcode</span><span style="font-family:SimSun">地址：</span><span style="font-family:Calibri"><a href="https://oj.leetcode.com/problems/second-highest-salary/">https://oj.leetcode.com/problems/second-highest-salary/</a></span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个问题很有趣，是要求我们写个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">来查询</span><span style="font-family:Calibri">Employee</span><span style="font-family:SimSun">表里第二高的工资，如果没有第二高的，那么返回</span><span style="font-family:Calibri">null</span><span style="font-family: SimSun">。</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+--------+<br /> | Id | Salary |<br /> +----+--------+<br /> | 1&nbsp; | 100&nbsp;&nbsp;&nbsp; |<br /> | 2&nbsp; | 200&nbsp;&nbsp;&nbsp; |<br /> | 3&nbsp; | 300&nbsp;&nbsp;&nbsp; |<br /> +----+--------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">看到这个问题，可能很多人会想，这很简单啊，写个</span><span style="font-family:Calibri">order by desc</span><span style="font-family:SimSun">，然后找到第二个即可。</span></p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee&nbsp;<span style="color: #0000FF; ">order</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">desc</span>&nbsp;limit&nbsp;<span style="color: #800000; font-weight: bold; ">1</span>,<span style="color: #800000; font-weight: bold; ">1</span></div>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">试试提交呗？</span><span style="font-family:Calibri">Wrong answer</span><span style="font-family:SimSun">，为什么？看条件约束啊，没有第二要返回</span><span style="font-family:Calibri">null</span><span style="font-family: SimSun">，我看到</span><span style="font-family:Calibri">null</span><span style="font-family:SimSun">的第一直觉是通过</span><span style="font-family: Calibri">join</span><span style="font-family:SimSun">搞到</span><span style="font-family:Calibri">null</span><span style="font-family: SimSun">值，于是有了下面的</span><span style="font-family:Calibri">ac sql</span><span style="font-family:SimSun">：</span></p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">max</span>(Salary)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;SecondHighestSalary<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span><br />o1.<span style="color: #808080; ">*</span><br />,<span style="color: #FF00FF; ">case</span>&nbsp;<span style="color: #0000FF; ">when</span>&nbsp;o2.s&nbsp;<span style="color: #0000FF; ">is</span>&nbsp;<span style="color: #0000FF; ">null</span>&nbsp;<span style="color: #0000FF; ">then</span>&nbsp;<span style="color: #800000; font-weight: bold; ">1</span>&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #800000; font-weight: bold; ">0</span>&nbsp;<span style="color: #0000FF; ">end</span>&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;nt<br /><span style="color: #0000FF; ">from</span><br />(<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee)o1<br /><span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span><br />(<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #FF00FF; ">max</span>(Salary)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;s&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee)o2<br /><span style="color: #0000FF; ">on</span>(o1.Salary<span style="color: #808080; ">=</span>o2.s)<br />)t<br /><span style="color: #0000FF; ">where</span>&nbsp;nt<span style="color: #808080; ">=</span><span style="color: #800000; font-weight: bold; ">1</span></div>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">思路简单说就是通过全表左外联最大</span><span style="font-family:Calibri">salary</span><span style="font-family:SimSun">，从关联不到的</span><span style="font-family: Calibri">salary</span><span style="font-family:SimSun">里再找最大不就是第二大吗？</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">最后的结果是</span><span style="font-family:Calibri">894ms</span><span style="font-family:SimSun">，当然我坚信有很多更快更高效的结果。</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div> <div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422478.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-27 16:38 <a href="http://www.blogjava.net/changedi/archive/2015/01/27/422478.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-175-Combine Two Tables-Easy</title><link>http://www.blogjava.net/changedi/archive/2015/01/27/422477.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 27 Jan 2015 08:23:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/27/422477.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422477.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/27/422477.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422477.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422477.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">大概上周看到</span><span style="font-family:Calibri">leetcode</span><span style="font-family:SimSun">开始做数据相关的挑战题目，目前是基于</span><span style="font-family:Calibri">MySQL</span><span style="font-family: SimSun">的</span><span style="font-family:Calibri">Sql</span><span style="font-family:SimSun">测试题目。作为一个现在</span><span style="font-family: Calibri">hive sql</span><span style="font-family:SimSun">占掉大部分工作时间的码农，还是可以选择来练练手，今天立帖把这些题目一一解决。</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">第一题的描述：题目地址：<span style="font-family: verdana, 'courier new';"><a href="https://oj.leetcode.com/problems/combine-two-tables/">https://oj.leetcode.com/problems/combine-two-tables/</a></span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">一张表叫做</span><span style="font-family:Calibri">Person</span><span style="font-family:SimSun">，主键是</span><span style="font-family:Calibri">PersonId</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+-------------+---------+<br /> | Column Name | Type&nbsp;&nbsp;&nbsp; |<br /> +-------------+---------+<br /> | PersonId&nbsp;&nbsp;&nbsp; | int&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | FirstName&nbsp;&nbsp; | varchar |<br /> | LastName&nbsp;&nbsp;&nbsp; | varchar |<br /> +-------------+---------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">另一张表叫做</span><span style="font-family:Calibri">Address</span><span style="font-family:SimSun">，主键是</span><span style="font-family:Calibri">AddressId</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+-------------+---------+<br /> | Column Name | Type&nbsp;&nbsp;&nbsp; |<br /> +-------------+---------+<br /> | AddressId&nbsp;&nbsp; | int&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | PersonId&nbsp;&nbsp;&nbsp; | int&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | City&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | varchar |<br /> | State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | varchar |<br /> +-------------+---------+</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">题目要求写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">完成查询任务：把</span><span style="font-family: Calibri">Person</span><span style="font-family:SimSun">表中每个人的</span><span style="font-family:Calibri">FirstName</span><span style="font-family: SimSun">，</span><span style="font-family:Calibri">LastName</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">City</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">State</span><span style="font-family: SimSun">都查询出来。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这明显是个非常简单的</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">，只要拿</span><span style="font-family:Calibri">Person</span><span style="font-family:SimSun">表做左表进行</span><span style="font-family:Calibri">left outer join</span><span style="font-family:SimSun">即可（当然做右表进行</span><span style="font-family: Calibri">right join</span><span style="font-family:SimSun">也行）</span></p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #0000FF; ">select</span><br /><span style="color: #008080; ">&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o1.FirstName<br /><span style="color: #008080; ">&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,o1.LastName<br /><span style="color: #008080; ">&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,o2.City<br /><span style="color: #008080; ">&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,o2.State<br /><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #0000FF; ">from</span>(<br /><span style="color: #008080; ">&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Person<br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;)o1<br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span>(<br /><span style="color: #008080; ">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Address<br /><span style="color: #008080; ">11</span>&nbsp;)o2<br /><span style="color: #008080; ">12</span>&nbsp;<span style="color: #0000FF; ">on</span>(o1.PersonId&nbsp;<span style="color: #808080; ">=</span>&nbsp;o2.PersonId)</div>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">最后，啰嗦几句，在我们现在的软件开发过程中，数据处理应该是一个工程师必备的技能，身在大公司，可能</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">的编写有很多的限制，有的甚至不需要工程师来编写，直接交给</span><span style="font-family:Calibri">DBA</span><span style="font-family:SimSun">就行了。在这样的环境下，开发工程师往往丢掉了数据库的基本功。而在大数据的浪潮下，在</span><span style="font-family:Calibri">hive\pig\ODPS</span><span style="font-family:SimSun">下编写</span><span style="font-family:Calibri"> sql </span><span style="font-family:SimSun">也成了数据开发工程师的基本技能，数据开发不能仅仅停留在写</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">实现功能，最重要的是理解</span><span style="font-family:Calibri">Hadoop</span><span style="font-family:SimSun">生态下，各种</span><span style="font-family: Calibri">sql</span><span style="font-family:SimSun">语句的原理。就像开发一样，要写出最高效的</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">来处理数据。我相信</span><span style="font-family:Calibri">leetcode</span><span style="font-family:SimSun">对于</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">的挑战会是一个不错的平台，大家加油</span><span style="font-family:Calibri">~~</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div> <div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div> <div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422477.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-27 16:23 <a href="http://www.blogjava.net/changedi/archive/2015/01/27/422477.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>初探IMEI【译】</title><link>http://www.blogjava.net/changedi/archive/2014/11/27/420789.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Thu, 27 Nov 2014 09:30:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/11/27/420789.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/420789.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/11/27/420789.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/420789.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/420789.html</trackback:ping><description><![CDATA[<p>一直想清楚的理解IMEI是什么,但是怎么也找不到合适的下笔思路,最终还是把imei.org上的这篇介绍翻译过来，做个记录。 </p><p>原文地址：<a href="http://imei.org/2013/05/imei-number-decode/">http://imei.org/2013/05/imei-number-decode/</a> 原文标题：<strong>What Is IMEI Number and How To Decode It</strong> </p><p>译文： </p><p>&#8220; </p><p>你可能听说过IMEI，也知道它是移动设备的标识ID，但是你知道它的具体功用吗？IMEI自手机出产后就伴随其一生，究竟什么是IMEI，如何理解它的数字含义？ </p><p>智能手机是全世界范围内偷窃事件的&#8220;主角&#8221;，不要低估IMEI的重要性，把IMEI备份一下是一个明智的决定。本文会解释为什么我们的手机需要IMEI，同时会列出如何找到并记录IMEI的技术方法。 </p><h2>IMEI是什么？</h2> <p>IMEI的全称是International Mobile Equipment Identity，每台设备都必备的一个唯一标识，用来区分设备与设备。当你购买一台新的手机设备时，你会在收据上看到IMEI；当你需要修理手机时，手机服务商可能会要求你提供IMEI。标准的IMEI是一个14位数字，同时也有IMEI/SV这样的16位数字形式（仅新设备有），SV是由软件设计的。当然苹果的iPhone GSM有 15位数字，而CDMA是14位数字。不管如何，IMEI的设计动机不仅仅是一个ID标识，它也可以用来阻止网络访问，还可以用IMEI来进行运营商解锁。 </p><p>如果你的手机失窃，你可以将情况上报到你的手机服务商，他们会阻止这台手机进行一切的网络访问，同时警察也可以通过IMEI来识别丢失的设备。 </p><h2>IMEI怎么查看？</h2> <p>多数手机在你输入拨号*#06#后就能看到IMEI了，当然也有其他的一些方法： </p><ul> <li>iOS：Settings-&gt;General-&gt;About（设置-&gt;通用-&gt;关于手机），这样也可以看到有IMEI（需要自己下滑寻找）。iPhone手机同样可以在sim卡托盘上找到IMEI，当然如果你不是使用原生的托盘就看不到了。</li></ul> <ul> <li>Android：Settings-&gt;About（设置-&gt;关于），这里可以看到IMEI，序列号和其他的信息。 </li><li>老的Sony或者索爱：输入 * Right * Left Left * Left * </li><li>新的索爱或者Blackberry：Options-&gt;Status（选项-&gt;状态）</li></ul> <p>你可以使用<a href="http://imei.org/check-iphone-carrier/">http://imei.org/check-iphone-carrier/</a> 服务通过输入IMEI来获得手机的运营商信息 </p><h2>如何解释IMEI：</h2> <p>2004年以来，统一的IMEI格式为：AA-BBBBBB-CCCCCC-D，这是一个15位数字号，其中： </p><ul> <li>AA：两位数字号，表示Reporting Body Identifier，用来表示由TAC（Type Allocation Code）分配的GSMA。 </li><li>BBBBBB：TAC(FAC)的剩余部分。 </li><li>CCCCCC：机器序列号（SNR） </li><li>D：Luhn 检测位</li></ul> <p>举例来说，现在iPhone 5的TAC为01-332700，而三星的Galaxy S2为35-853704，而C部分的SNR是由手机制造商自定义的生产序列号，最后一位校验位是通过算法来生成的。 </p><p>IMEI是手机相关的，与sim卡无关，当你的手机被偷后，无论手机重置还是更换sim卡，IMEI都是不变的，这时你是可以通过联系你的手机服务商来锁住手机的服务的。如果这行不通，可以联系你所在地区的运营商来锁定IMEI对于运营商网络的访问。 </p><p>有时候IMEI是变化的，尽管这不合法。有些窃贼有能力将合法的新的IMEI安装到你的手机里从而重新启用这个手机。另外鉴于犯罪动机，还有人会利用IMEI来监听设备。 </p><p>综上，IMEI是手机的重要的唯一性ID，你需要去备份并记住它。 </p><p>&#8221; </p><p>翻译结束。 </p><p>最后附加一些说明。 </p><p>关于TAC：http://en.wikipedia.org/wiki/Type_Allocation_Code </p><p>关于最后一位校验位的算法和python code： </p><p>以14位IMEI为例，校验位假设是C </p><ol> <li>从后向前，记录下每隔两位的数字，记为列表A </li><li>从后向前，记录下除A以外的数字，记为列表B </li><li>将A中的数字都乘以2，如果一个数字乘以2后大于10，那么把这个结果拆为两个数字（个位数一个，十位数一个），记为列表A' </li><li>把A'和B中的所有数字求和，记为S </li><li>计算S*9%10，记为F，如果F等于C，校验通过，否则IMEI有问题。</li></ol> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">if</span> len(arg0)==15:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>             check_bit = <span style="color: #0000ff">int</span>(arg0[-1])</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>             i = len(arg0)-2</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>             l,r = [],[]</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>             <span style="color: #0000ff">while</span> i&gt;=0:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>                 m = <span style="color: #0000ff">int</span>(arg0[i])*2</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>                 <span style="color: #0000ff">if</span> m&lt;10:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>                     l.append(m)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>                 <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>                     l.append(m%10)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>                     l.append(m/10)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>                 r.append(<span style="color: #0000ff">int</span>(arg0[i-1]))</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>                 i-=2</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>             l.reverse()</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>             r.reverse()</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>             <span style="color: #0000ff">if</span> sum((sum(l),sum(r)))*9%10==check_bit:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>                 <span style="color: #0000ff">return</span> True</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>         <span style="color: #0000ff">return</span> False</pre><!--CRLF--></div></div> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/420789.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-11-27 17:30 <a href="http://www.blogjava.net/changedi/archive/2014/11/27/420789.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hbase配置项粗解（3）</title><link>http://www.blogjava.net/changedi/archive/2014/03/31/411742.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 31 Mar 2014 09:18:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/03/31/411742.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/411742.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/03/31/411742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/411742.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/411742.html</trackback:ping><description><![CDATA[<p>HBase的配置 完结篇：</p> <p>hbase.rpc.server.engine：hbase 做rpc server的调度管理类，实现自org.apache.hadoop.ipc.RpcServerEngine，默认是org.apache.hadoop.hbase.ipc.ProtobufRpcServerEngine；</p> <p>hbase.rpc.timeout：Hbase client发起远程调用时的超时时限，使用ping来确认连接，但是最终会抛出一个TimeoutException，默认值是60000；</p> <p>hbase.rpc.shortoperation.timeout：另一个版本的hbase.rpc.timeout，控制短操作的超时时限，比如region server 汇报master的操作的超时时限可以设置小，这样有利于master的failover，默认是10000；</p> <p>hbase.ipc.client.tcpnodelay：默认是true，具体就是在tcp socket连接时设置 no delay；</p> <p>hbase.master.keytab.file：kerberos keytab 文件的全路径名，用来为HMaster做log，无默认值；</p> <p>hbase.master.kerberos.principal：运行HMaster进程时需要kerberos的principal name，这个配置就是这个name的值，形如：<a href="mailto:hbase/_HOST@EXAMPLE.COM">hbase/_HOST@EXAMPLE.COM</a>；</p> <p>hbase.regionserver.keytab.file：kerberos keytab 文件的全路径名，用来为HRegionServer做log，无默认值；</p> <p>hbase.regionserver.kerberos.principal：运行HRegionServer进程时需要kerberos的principal name，这个配置就是这个name的值，形如：<a href="mailto:hbase/_HOST@EXAMPLE.COM">hbase/_HOST@EXAMPLE.COM</a>；</p> <p>hadoop.policy.file：RPC服务器做权限认证时需要的安全策略配置文件，在Hbase security开启后使用，默认是habse-policy.xml；</p> <p>hbase.superuser：Hbase security 开启后的超级用户配置，一系列由逗号隔开的user或者group；</p> <p>hbase.auth.key.update.interval：Hbase security开启后服务端更新认证key的间隔时间：默认是86400000毫秒；</p> <p>hbase.auth.token.max.lifetime：Hbase security开启后，认证token下发后的生存周期，默认是604800000毫秒；</p> <p>hbase.ipc.client.fallback-to-simple-auth-allowed：client使用安全连接去链接一台非安全服务器时，服务器提示client切换到SASL SIMPLE认证模式（非安全），如果设置为true，则client同意切换到非安全连接，如果false，则退出连接；</p> <p>hbase.coprocessor.region.classes：逗号分隔的Coprocessores列表，会被加载到默认所有表上。在自己实现了一个Coprocessor后，将其添加到Hbase的classpath并加入全限定名。也可以延迟加载，由HTableDescriptor指定；</p> <p>hbase.rest.port：Hbase REST服务器的端口，默认是8080；</p> <p>hbase.rest.readonly：定义REST服务器启动的模式，有两种方式，false：所有http方法都将被通过-GET/PUT/POST/DELETE，true：只有get方法ok。默认值是false；</p> <p>hbase.rest.threads.max：REST服务器线程池的最大线程数，池满的话新请求会自动排队，限制这个配置可以控制服务器的内存量，预防OOM，默认是100；</p> <p>hbase.rest.threads.min：同上类似，最小线程数，为了确保服务器的服务状态，默认是2；</p> <p>hbase.rest.support.proxyuser：使REST服务器支持proxy-user 模式，默认是false；</p> <p>hbase.defaults.for.version.skip：是否跳过hbase.defaults.for.version的检查，默认是false；</p> <p>hbase.coprocessor.master.classes：由HMaster进程加载的coprocessors，逗号分隔，全部实现org.apache.hadoop.hbase.coprocessor.MasterObserver，同coprocessor类似，加入classpath及全限定名；</p> <p>hbase.coprocessor.abortonerror：如果coprocessor加载失败或者初始化失败或者抛出Throwable对象，则主机退出。设置为false会让系统继续运行，但是coprocessor的状态会不一致，所以一般debug时才会设置为false，默认是true；</p> <p>hbase.online.schema.update.enable：设置true来允许在线schema变更，默认是true；</p> <p>hbase.table.lock.enable：设置为true来允许在schema变更时zk锁表，锁表可以组织并发的schema变更导致的表状态不一致，默认是true；</p> <p>hbase.thrift.minWorkerThreads：线程池的core size，在达到这里配置的量级后，新线程才会再新的连接创立时创建，默认是16；</p> <p>hbase.thrift.maxWorkerThreads：顾名思义，最大线程数，达到这个数字后，服务器开始drop连接，默认是1000；</p> <p>hbase.thrift.maxQueuedRequests：Thrift连接队列的最大数，如果线程池满，会先在这个队列中缓存请求，缓存上限就是该配置，默认是1000；</p> <p>hbase.thrift.htablepool.size.max：Thrift服务器上table pool的最大上限，默认是1000；</p> <p>hbase.offheapcache.percentage：JVM参数-XX:MaxDirectMemorySize的百分比值，默认是0，即不开启堆外分配；</p> <p>hbase.data.umask.enable：开启后，文件在regionserver写入时会 有权限相关设定，默认是false不开启；</p> <p>hbase.data.umask：开启上面一项配置后，文件的权限umask，默认是000；</p> <p>hbase.metrics.showTableName：是否为每个指标显示表名前缀，默认是true；</p> <p>hbase.metrics.exposeOperationTimes：是否进行关于操作在使用时间维度的指标报告，比如GET PUT DELETE INCREMENT等，默认是true；</p> <p>hbase.snapshot.enabled：是否允许snapshot被使用、存储和克隆，默认是true；</p> <p>hbase.snapshot.restore.take.failsafe.snapshot：在restore过程中，如果失败则启用snapshot替换，成功则删除掉snapshot，默认开启true；</p> <p>hbase.snapshot.restore.failsafe.name：刚才所说过程中snapshot的名字，默认是hbase-failsafe-{snapshot.name}-{restore.timestamp}；</p> <p>hbase.server.compactchecker.interval.multiplier：检查是否需要compact的时间间隔，一般情况是在比如memstore flush后或者其他事件触发compact的，但是有时也需要不同的compact策略，所以需要周期性的检查具体间隔=hbase.server.compactchecker.interval.multiplier * hbase.server.thread.wakefrequency，默认1000；</p> <p>hbase.lease.recovery.timeout：在dfs 租约超时时限，超时则放弃，默认是900000；</p> <p>hbase.lease.recovery.dfs.timeout：dfs恢复租约调用的超时时限，默认是64000；</p><img src ="http://www.blogjava.net/changedi/aggbug/411742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-03-31 17:18 <a href="http://www.blogjava.net/changedi/archive/2014/03/31/411742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hbase配置项粗解（2）</title><link>http://www.blogjava.net/changedi/archive/2014/01/03/408449.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Fri, 03 Jan 2014 10:34:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/01/03/408449.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/408449.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/01/03/408449.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/408449.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/408449.html</trackback:ping><description><![CDATA[<p>hbase的配置接上篇</p> <p>hbase.client.write.buffer：htable客户端写缓冲区大小，默认是2097152BYTE，这个缓冲区就是为了写数据的临时存放，设置大了，浪费客户端和服务端的存储，设置小了，如果写的数据多，太多的RPC又带来网络开销，官方给的一个服务端存储耗费评估计算是：hbase.client.write.buffer*hbase.regionserver.handler.count，服务端的rs的处理handler个数也很关键；</p> <p>hbase.client.pause：pause时长，在hbase发生get或其他操作fail掉的时候进行pause的时间长度，默认是100；</p> <p>hbase.client.retries.number：发生操作fail时的重试次数，结合上一个指标一起来控制总的重试时间，默认是35；</p> <p>hbase.client.max.total.tasks：一个HTable实例可以提交给集群的最大并发任务数，默认是100；</p> <p>hbase.client.max.perserver.tasks：一个HTable实例给一台regionserver提交的最大并发任务数，默认是5；</p> <p>hbase.client.max.perregion.tasks：客户端连接一台region的最大连接数，换句话说，当你有这么多个连接在region时，新的操作不被发送直到有操作完成，默认是1；</p> <p>hbase.client.scanner.caching：做scanner的next操作时（如果再本地client没找到）缓存的数据行数，这个值的设置也需要权衡，缓存的多则快，但吃内存，缓存的少则需要多的拉数据， 需要注意的事项是如果两次调用的时间差大于scanner的timeout，则不要设置该值，默认是100；</p> <p>hbase.client.keyvalue.maxsize：一个KeyValue实例的最大大小，这是存储文件中一个entry的容量上限，合理的设置这个值可以控制regionserver的split，split不会拆keyvalue，所以把keyvalue的大小设置为regionserver大小的一个比例分数（可除）是个不错的选择，默认是10485760；</p> <p>hbase.client.scanner.timeout.period：结合刚才的caching做的一个，scanner的超时时间，默认是60000毫秒；</p> <p>hbase.client.localityCheck.threadPoolSize：做localityCheck的线程池大小，默认是2；</p> <p>hbase.bulkload.retries.number：做bulk load的最大重试次数，默认是0，即代表不断重试；</p> <p>hbase.balancer.period：Master运行balancer的周期，默认是300000毫秒；</p> <p>hbase.regions.slop：如果有regionserver的region数目超过average+(average*slop)，则rebalance，默认是0.2；</p> <p>hbase.server.thread.wakefrequency：服务线程的sleep时间，默认10000毫秒，比如log roller；</p> <p>hbase.server.versionfile.writeattempts：退出前写 version file的重试次数，默认3，每次尝试的间隔由上一个参数控制；</p> <p>hbase.hregion.memstore.flush.size：Memstore写磁盘的flush阈值，超过这个大小就flush，默认是134217728；</p> <p>hbase.hregion.preclose.flush.size：如果一个region的memstore的大小等于或超过这个参数的量，在关闭region时（放置关闭flag），要提前flush，然后region关闭下线，默认大小是5242880；</p> <p>hbase.hregion.memstore.block.multiplier：如果memstore的大小满足hbase.hregion.block.memstore * hbase.hregion.flush.size个byte，那么阻塞update，这个配置可以避免不必要的长时间split或者compact，甚至是OOME，默认是2；</p> <p>hbase.hregion.memstore.mslab.enabled：开启MemStore-Local Allocation Buffer，这个配置可以避免在高写入的情况下的堆内存碎片，可以降低在大堆情况下的stop-the-world GC频率，默认是true；</p> <p>hbase.hregion.max.filesize：HStoreFile的最大尺寸，换句话说，当一个region里的列族的任意一个HStoreFile超过这个大小，那么region进行split，默认是10737418240；</p> <p>hbase.hregion.majorcompaction：一个region的所有HStoreFile进行major compact的时间周期，默认是604800000 毫秒（7天）；</p> <p>hbase.hregion.majorcompaction.jitter：major compaction的发生抖动范围，这么理解比较容易，就是说上一个参数不是一个严格周期，会有个抖动，这个参数就是这个抖动的比例，默认是0.5；</p> <p>hbase.hstore.compactionThreshold：一个HStore存储HStoreFile的个数阈值，超过这个阈值则所有的HStoreFile会被写到一个新的HStore，需要平衡取舍，默认是3；</p> <p>hbase.hstore.blockingStoreFiles：一个HStore存储HStoreFile阻塞update的阈值，超过这个阈值，HStore就进行compaction，直到做完才允许update，默认是10；</p> <p>hbase.hstore.blockingWaitTime：一个更强力的配置，配合上一个参数，当HStore阻塞update时，超过这个时间限制，阻塞取消，就算compaction没有完成，update也不会再被阻塞，默认是90000毫秒；</p> <p>hbase.hstore.compaction.max：每个minor compaction的HStoreFile个数上限，默认是10；</p> <p>hbase.hstore.compaction.kv.max：在flushing或者compacting时允许的最大keyvalue个数，如果有大的KeyValue或者OOME的话则配置一个小的值，如果行数多且小则配置大值，默认是10；</p> <p>hbase.storescanner.parallel.seek.threads：如果并行查找开启的线程池大小，默认是10；</p> <p>hfile.block.cache.size：一个配置比例，允许最大堆的对应比例的内存作为HFile和HStoreFile的block cache，默认是0.4，即40%，设置为0则disable这个比例，不推荐这么做；</p> <p>hfile.block.index.cacheonwrite：在index写入的时候允许put无根（non-root）的多级索引块到block cache里，默认是false；</p> <p>hfile.index.block.max.size：在多级索引的树形结构里，如果任何一层的block index达到这个配置大小，则block写出，同时替换上新的block，默认是131072；</p> <p>hfile.format.version：新文件的HFile 格式版本，设置为1来测试向后兼容，默认是2；</p> <p>hfile.block.bloom.cacheonwrite：对于组合布隆过滤器的内联block开启cache-on-write，默认是false；</p> <p>io.storefile.bloom.block.size：一个联合布隆过滤器的单一块（chunk）的大小，这个值是一个逼近值，默认是131072；</p> <p>hbase.rs.cacheblocksonwrite：当一个HFile block完成时是否写入block cache，默认是false；</p><img src ="http://www.blogjava.net/changedi/aggbug/408449.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-01-03 18:34 <a href="http://www.blogjava.net/changedi/archive/2014/01/03/408449.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hbase配置项粗解（1）</title><link>http://www.blogjava.net/changedi/archive/2013/12/09/407372.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 09 Dec 2013 12:07:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/12/09/407372.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/407372.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/12/09/407372.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/407372.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/407372.html</trackback:ping><description><![CDATA[<p>继之前写的设计和使用tip，这里补充一下hbase所有的配置项。之前涉及的一个项目在hbase设计上存在缺陷，当进入时已经存在着很多rowkey设计和读写问题，现在重读hbase文档，把所有的配置项整理一遍。</p> <p>hbase.tmp.dir：本地文件系统的临时目录，默认是${java.io.tmpdir}/hbase-${user.name}；</p> <p>hbase.rootdir：hbase持久化的目录，被所有regionserver共享，默认${hbase.tmp.dir}/hbase，一般设置为hdfs://namenode.example.org:9000/hbase类似，带全限定名；</p> <p>hbase.cluster.distributed：hbase集群模式运作与否的标志，默认是false，开启需要设置为true，false时启动hbase会在一个jvm中运行hbase和zk；</p> <p>hbase.zookeeper.quorum：重要的也是必须设置的，启动zk的服务器列表，逗号分隔，cluster模式下必须设置，默认是localhost，hbase客户端也需要设置这个值去访问zk；</p> <p>hbase.local.dir：本地文件系统被用在本地存储的目录，默认${hbase.tmp.dir}/local/；</p> <p>hbase.master.port：hbase master绑定的端口，默认是60000；</p> <p>hbase.master.info.port：hbase master web 界面的端口，默认是60010，设置为-1可以禁用ui；</p> <p>hbase.master.info.bindAddress：master web界面的绑定地址，默认是0.0.0.0；</p> <p>hbase.master.logcleaner.plugins：清理日志的插件列表，逗号分隔，被LogService调用的LogCleanerDelegate，可以自定义，顺序执行，清理WAL和HLog；默认<code>org.apache.hadoop.hbase.master.cleaner.TimeToLiveLogCleaner</code></p> <p>hbase.master.logcleaner.ttl：HLog在.oldlogdir目录中生存的最长时间，过期则被Master起线程回收，默认是600000；</p> <p>hbase.master.hfilecleaner.plugins：HFile的清理插件列表，逗号分隔，被HFileService调用，可以自定义，默认org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner</p> <p>hbase.master.catalog.timeout：Catalog Janitor从master到META的超时时间，我们知道这个Janitor是定时的去META扫描表目录，来决定回收无用的regions，默认是600000；</p> <p>fail.fast.expired.active.master：如果master过期，那么不需要从zk恢复，直接终止，默认是false；</p> <p>hbase.master.dns.interface：master的dns接口，向该接口提供ip，默认是default；</p> <p>hbase.master.dns.nameserver：master使用的dns主机名或者ip，默认是default；</p> <p>hbase.regionserver.port：regionserver绑定的端口，默认是60020；</p> <p>hbase.regionserver.info.port：regionserver的web界面端口，-1取消界面，默认是60030；</p> <p>hbase.regionserver.info.bindAddress：regionserver的web绑定，默认是0.0.0.0；</p> <p>hbase.regionserver.info.port.auto：master或者regionserver是否自动搜索绑定的端口，默认是false；</p> <p>hbase.regionserver.handler.count：regionserver上rpc listener的个数，<a href="http://kenwublog.com/hbase-performance-tuning">http://kenwublog.com/hbase-performance-tuning</a>把这个配置称为io线程数，其实雷同，就是说在regionserver上一个处理rpc的handler，默认是30；</p> <p>hbase.regionserver.msginterval：regionserver向master发消息的间隔，默认3000毫秒；</p> <p>hbase.regionserver.optionallogflushinterval：如果没有足够的entry触发同步，那么过了这个间隔后HLog将被同步到HDFS，默认是1000毫秒；</p> <p>hbase.regionserver.regionSplitLimit：regionsplit的最大限额，默认是MAX_INT=2147483647，设置这个限制后，在到达限制时region split就不会再进行；</p> <p>hbase.regionserver.logroll.period：不管有多少版本，直接roll掉commit log的周期，也就是说一个固定的时间周期，到期就roll，默认是3600000毫秒；</p> <p>hbase.regionserver.logroll.errors.tolerated：可接受的WAL关闭错误个数，到达后将触发服务器终止；设置为0那么在WAL writer做log rolling失败时就停止region server，默认是2；</p> <p>hbase.regionserver.hlog.reader.impl：HLog 文件reader的实现类，默认是org.apache.hadoop.hbase.regionserver.wal.ProtobufLogReader；</p> <p>hbase.regionserver.hlog.writer.impl：HLog 文件writer的实现类，默认是org.apache.hadoop.hbase.regionserver.wal.ProtobufLogWriter；</p> <p>hbase.regionserver.global.memstore.upperLimit：memstore在regionserver内存中的上限，届时新的update被阻塞并且flush被强制写，默认是0.4就是堆内存的40%；阻塞状态持续到regionserver的所有memstore的容量到达hbase.regionserver.global.memstore.lowerLimit；</p> <p>hbase.regionserver.global.memstore.lowerLimit：memstore在regionserver内存中的最大上限，到达时flush就被强制写，默认是0.38等价于38%的内存容量；</p> <p>hbase.regionserver.optionalcacheflushinterval：一个edit版本在内存中的cache时长，默认3600000毫秒，设置为0的话则禁止自动flush；</p> <p>hbase.regionserver.catalog.timeout：regionserver的Catalog Janitor访问META的超时时间，默认是600000；</p> <p>hbase.regionserver.dns.interface：同master类似~~不讲</p> <p>hbase.regionserver.dns.nameserver：同master类似</p> <p>zookeeper.session.timeout：这是个值得说道一下的配置，首先ZK客户端要用，Hbase使用zk的客户端联系总体，同时也被用来启动一个zk server，作为zk的maxSessionTimeout，总的来说就是regionserver与zk的关键参数，如果连接超时，master会重新的balance，regionserver也会被从集群名单中清除，默认是90000；一个问题是如果zk 由hbase自己维护，那么该参数作为regionserver连接是一个值，如果zk在另外的集群，那么zk自己的maxSessionTimeout参数将优先于Hbase的该参数，届时可能会发生超时时间不同的问题；</p> <p>zookeeper.znode.parent：znode存放root region的地址，默认是root-region-server；</p> <p>zookeeper.znode.acl.parent：root znode的acl，默认acl；</p> <p>hbase.zookeeper.dns.interface：zk的dns接口，默认default；</p> <p>hbase.zookeeper.dns.nameserver：zk的dns服务地址，默认default；</p> <p>hbase.zookeeper.peerport：zk的peer之间的通讯端口，默认是2888；</p> <p>hbase.zookeeper.leaderport：zk选leader的通讯端口，默认是3888；</p> <p>hbase.zookeeper.useMulti：zk支持多重update，要求zk在3.4版本以上，默认是false；</p> <p>hbase.config.read.zookeeper.config：让hbaseconfig去读zk的config，默认false，也不支持开启，这个功能很搞笑~~个人观点；</p> <p>hbase.zookeeper.property.initLimit：zk的配置，同步的属性个数限制，默认10个~~没用；</p> <p>hbase.zookeeper.property.syncLimit：zk的配置，同步时的每次请求的条数，默认5个；</p> <p>hbase.zookeeper.property.dataDir：zk的配置，snapshot存放的目录，默认是${hbase.tmp.dir}/zookeeper；</p> <p>hbase.zookeeper.property.clientPort：zk的配置，client连zk的端口，默认2181；</p> <p>hbase.zookeeper.property.maxClientCnxns：zk的配置，允许接入zk的最大并发连接数的限制，按ip分配，默认300；</p><img src ="http://www.blogjava.net/changedi/aggbug/407372.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-12-09 20:07 <a href="http://www.blogjava.net/changedi/archive/2013/12/09/407372.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（7）</title><link>http://www.blogjava.net/changedi/archive/2013/11/13/406295.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 13 Nov 2013 06:41:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/11/13/406295.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/406295.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/11/13/406295.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/406295.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/406295.html</trackback:ping><description><![CDATA[<p>hive配置的最终章</p> <p>fs.har.impl：访问Hadoop Archives的实现类，低于hadoop 0.20版本的都不兼容，默认是org.apache.hadoop.hive.shims.HiveHarFileSystem；</p> <p>hive.archive.enabled：是否允许归档操作，默认是false；</p> <p>hive.archive.har.parentdir.settable：在创建HAR文件时必须要有父目录，需要手动设置，在新的hadoop版本会支持，默认是false；</p> <p>hive.support.concurrency：hive是否支持并发，默认是false，支持读写锁的话，必须要起zookeeper；</p> <p>hive.lock.mapred.only.operation：控制是否在查询时加锁，默认是false；</p> <p>hive.lock.numretries：获取锁时尝试的重试次数，默认是100；</p> <p>hive.lock.sleep.between.retries：在重试间隔的睡眠时间，默认60秒；</p> <p>hive.zookeeper.quorum：zk地址列表，默认是空；</p> <p>hive.zookeeper.client.port：zk服务器的连接端口，默认是2181；</p> <p>hive.zookeeper.session.timeout：zk客户端的session超时时间，默认是600000；</p> <p>hive.zookeeper.namespace：在所有zk节点创建后的父节点，默认是hive_zookeeper_namespace；</p> <p>hive.zookeeper.clean.extra.nodes：在session结束时清除所有额外node；</p> <p>hive.cluster.delegation.token.store.class：代理token的存储实现类，默认是org.apache.hadoop.hive.thrift.MemoryTokenStore，可以设置为org.apache.hadoop.hive.thrift.ZooKeeperTokenStore来做负载均衡集群；</p> <p>hive.cluster.delegation.token.store.zookeeper.connectString：zk的token存储连接串，默认是localhost:2181；</p> <p>hive.cluster.delegation.token.store.zookeeper.znode：token存储的节点跟路径，默认是/hive/cluster/delegation；</p> <p>hive.cluster.delegation.token.store.zookeeper.acl：token存储的ACL，默认是sasl:hive/host1@example.com:cdrwa,sasl:hive/host2@example.com:cdrwa；</p> <p>hive.use.input.primary.region：从一张input表创建表时，创建这个表到input表的主region，默认是true；</p> <p>hive.default.region.name：默认region的名字，默认是default；</p> <p>hive.region.properties：region的默认的文件系统和jobtracker，默认是空；</p> <p>hive.cli.print.header：查询输出时是否打印名字和列，默认是false；</p> <p>hive.cli.print.current.db：hive的提示里是否包含当前的db，默认是false；</p> <p>hive.hbase.wal.enabled：写入hbase时是否强制写wal日志，默认是true；</p> <p>hive.hwi.war.file：hive在web接口是的war文件的路径，默认是lib/hive-hwi-xxxx(version).war；</p> <p>hive.hwi.listen.host：hwi监听的host地址，默认是0.0.0.0；</p> <p>hive.hwi.listen.port：hwi监听的端口，默认是9999；</p> <p>hive.test.mode：hive是否运行在测试模式，默认是false；</p> <p>hive.test.mode.prefix：在测试模式运行时，表的前缀字符串，默认是test_；</p> <p>hive.test.mode.samplefreq：如果hive在测试模式运行，并且表未分桶，抽样频率是多少，默认是32；</p> <p>hive.test.mode.nosamplelist：在测试模式运行时不进行抽样的表列表，默认是空；</p><img src ="http://www.blogjava.net/changedi/aggbug/406295.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-11-13 14:41 <a href="http://www.blogjava.net/changedi/archive/2013/11/13/406295.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基数估计</title><link>http://www.blogjava.net/changedi/archive/2013/11/12/406235.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 12 Nov 2013 02:10:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/11/12/406235.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/406235.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/11/12/406235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/406235.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/406235.html</trackback:ping><description><![CDATA[<p>问题的背景是在大数据冲击下，很多数据指标（尤其是涉及到去重的）的计算无法在合理的空间和时间内完成，比如uv的计算，数学原型问题等价于持续的向一个集合中写数，重复的不记，要求最终给出集合中不重复的元素的个数（集合的势）。而比较暴力的做法是随着数字增多不断的扩展集合的大小，让它放下所有的数，最终数出这个个数就OK。显然这样的空间复杂度在单机下是做不到的，所以多数做法是利用分布式原理将uv数据隔离到不同的计算节点，每个计算节点自行维护一个类似这样的集合（wdm实时里的布隆过滤器），然后分而治之，最后merge为一份结果数据。 </p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 基数估计的初衷就是为了解决在大数据的前提下，如何以低成本的空间复杂度去计算超大集合的势的问题，换句话说，通过基数估计，单机做到计算亿级别uv，误差在4%以内。解决思路主要是概率估计，具体原理和做法参看 blog和论文原文。 </p><p>&nbsp;&nbsp;&nbsp;&nbsp; 出于实验的目的，我简单实现了暴力做法bruteforce-bf，布隆过滤器-bbf，loglog-llc和hyperloglog-hllc四个算法，比较一下基数估计这个计算去重指标的逻辑是否可行（llc非常离谱，可能是我分桶数没有调整好，就不贴出结果了）。 </p><p>预处理方法：1-N生成随机uid，模拟N次（均匀分布），jvm启动-Xmx1024m。 </p><p>实验结果： </p><p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_thumb.png" width="244" height="77" /></a>&nbsp;&nbsp; <a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_thumb_1.png" width="244" height="77" /></a>  </p><p> 附加说明一下，期望值如何计算：其实这个实验的数学原型就是一个长度为k的均匀分布的（1-N)的随机数列，求不重复的元素个数的期望。我实验里k=n，这是一种极端情况（实验设计纯为方便计算，如果k较大会导致计算超慢，uv5000w时根本无法计算出来，增大k理论上会提高精度，我实验过的一组数据是100w uv 500wpv时 hllc的值是991234，误差&lt;1%），理论上k相当于pv，在递推公式中k趋于无穷时期望等于n。 </p><p>这个递推的计算可以通过组合分析推导，推导方法不详说了（当然我有可能推导错了~~数学功底 实在 不行了），通项公式见matlab代码。 </p><p> syms e n;<br /> e = n-(1/n)*((1-2*n+n*n)*((n-1)/n)^(n-2)+(1-n)*n+n*(n-1)); </p><p> vpa(subs(e,'n',1000000),10) </p><p>另外，我个人认为分布式布隆过滤器的方案是非常好的，因为空间和时间都比较均衡，且精确度高，基数估计的方法本质上空间复杂度O(1)，时间复杂度代码高效一点也可以非常快，但是缺点是精确度稍微欠缺，且不易分布式计算（因为它天生适合单进程，llc分桶均衡也是单进程做比较好，分布式完全是牛刀杀鸡）。 </p><p>ref blog: <a href="http://blog.codinglabs.org/articles/cardinality-estimate-exper.html#ref4">http://blog.codinglabs.org/articles/cardinality-estimate-exper.html#ref4</a> </p><p>算法实现的java代码可见github： <a href="https://github.com/changedi/card-estimate">https://github.com/changedi/card-estimate</a> </p><img src ="http://www.blogjava.net/changedi/aggbug/406235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-11-12 10:10 <a href="http://www.blogjava.net/changedi/archive/2013/11/12/406235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（6）</title><link>http://www.blogjava.net/changedi/archive/2013/10/26/405682.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Sat, 26 Oct 2013 11:35:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/10/26/405682.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/405682.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/10/26/405682.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/405682.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/405682.html</trackback:ping><description><![CDATA[<p>hive的index索引相关、统计相关和认证授权相关的配置。</p> <p>hive.index.compact.file.ignore.hdfs：在索引文件中存储的hdfs地址将在运行时被忽略，如果开启的话；如果数据被迁移，那么索引文件依然可用，默认是false；</p> <p>hive.optimize.index.filter.compact.minsize：压缩索引自动应用的最小输入大小，默认是5368709120；</p> <p>hive.optimize.index.filter.compact.maxsize：同上，相反含义，如果是负值代表正无穷，默认是-1；</p> <p>hive.index.compact.query.max.size：一个使用压缩索引做的查询能取到的最大数据量，默认是10737418240 个byte；负值代表无穷大；</p> <p>hive.index.compact.query.max.entries：使用压缩索引查询时能读到的最大索引项数，默认是10000000；负值代表无穷大；</p> <p>hive.index.compact.binary.search：在索引表中是否开启二分搜索进行索引项查询，默认是true；</p> <p>hive.exec.concatenate.check.index：如果设置为true，那么在做ALTER TABLE tbl_name CONCATENATE on a table/partition（有索引） 操作时，抛出错误；可以帮助用户避免index的删除和重建；</p> <p>hive.stats.dbclass：存储hive临时统计信息的数据库，默认是jdbc:derby；</p> <p>hive.stats.autogather：在insert overwrite命令时自动收集统计信息，默认开启true；</p> <p>hive.stats.jdbcdriver：数据库临时存储hive统计信息的jdbc驱动；</p> <p>hive.stats.dbconnectionstring：临时统计信息数据库连接串，默认jdbc:derby:databaseName=TempStatsStore;create=true；</p> <p>hive.stats.defaults.publisher：如果dbclass不是jdbc或者hbase，那么使用这个作为默认发布，必须实现StatsPublisher接口，默认是空；</p> <p>hive.stats.defaults.aggregator：如果dbclass不是jdbc或者hbase，那么使用该类做聚集，要求实现StatsAggregator接口，默认是空；</p> <p>hive.stats.jdbc.timeout：jdbc连接超时配置，默认30秒；</p> <p>hive.stats.retries.max：当统计发布合聚集在更新数据库时出现异常时最大的重试次数，默认是0，不重试；</p> <p>hive.stats.retries.wait：重试次数之间的等待窗口，默认是3000毫秒；</p> <p>hive.client.stats.publishers：做count的job的统计发布类列表，由逗号隔开，默认是空；必须实现org.apache.hadoop.hive.ql.stats.ClientStatsPublisher接口；</p> <p>hive.client.stats.counters：没什么用~~~</p> <p>hive.security.authorization.enabled：hive客户端是否认证，默认是false；</p> <p>hive.security.authorization.manager：hive客户端认证的管理类，默认是org.apache.hadoop.hive.ql.security.authorization.DefaultHiveAuthorizationProvider；用户定义的要实现org.apache.hadoop.hive.ql.security.authorization.HiveAuthorizationProvider；</p> <p>hive.security.authenticator.manager：hive客户端授权的管理类，默认是org.apache.hadoop.hive.ql.security.HadoopDefaultAuthenticator；用户定义的需要实现org.apache.hadoop.hive.ql.security.HiveAuthenticatorProvider；</p> <p>hive.security.authorization.createtable.user.grants：当表创建时自动授权给用户，默认是空；</p> <p>hive.security.authorization.createtable.group.grants：同上，自动授权给组，默认是空；</p> <p>hive.security.authorization.createtable.role.grants：同上，自动授权给角色，默认是空；</p> <p>hive.security.authorization.createtable.owner.grants：同上，自动授权给owner，默认是空；</p> <p>hive.security.metastore.authorization.manager：metastore的认证管理类，默认是org.apache.hadoop.hive.ql.security.authorization.DefaultHiveMetastoreAuthorizationProvider；用户定义的必须实现org.apache.hadoop.hive.ql.security.authorization.HiveMetastoreAuthorizationProvider接口；接口参数要包含org.apache.hadoop.hive.ql.security.authorization.StorageBasedAuthorizationProvider接口；使用HDFS的权限控制认证而不是hive的基于grant的方式；</p> <p>hive.security.metastore.authenticator.manager：metastore端的授权管理类，默认是org.apache.hadoop.hive.ql.security.HadoopDefaultMetastoreAuthenticator，自定义的必须实现org.apache.hadoop.hive.ql.security.HiveAuthenticatorProvider接口；</p> <p>hive.metastore.pre.event.listeners：在metastore做数据库任何操作前执行的事件监听类列表；</p><img src ="http://www.blogjava.net/changedi/aggbug/405682.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-10-26 19:35 <a href="http://www.blogjava.net/changedi/archive/2013/10/26/405682.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（5）</title><link>http://www.blogjava.net/changedi/archive/2013/10/14/404981.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 14 Oct 2013 09:39:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/10/14/404981.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/404981.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/10/14/404981.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/404981.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/404981.html</trackback:ping><description><![CDATA[<p>关于MetaStore：metastore是个独立的关系数据库，用来持久化schema和系统元数据。</p> <p>hive.metastore.local：控制hive是否连接一个远程metastore服务器还是开启一个本地客户端jvm，默认是true，Hive0.10已经取消了该配置项；</p> <p>javax.jdo.option.ConnectionURL：JDBC连接字符串，默认jdbc:derby:;databaseName=metastore_db;create=true；</p> <p>javax.jdo.option.ConnectionDriverName：JDBC的driver，默认<tt>org.apache.derby.jdbc.EmbeddedDriver</tt>；</p> <p>javax.jdo.PersisteneManagerFactoryClass：实现JDO PersistenceManagerFactory的类名，默认org.datanucleus.jdo.JDOPersistenceManagerFactory；</p> <p>javax.jdo.option.DetachAllOnCommit：事务提交后detach所有提交的对象，默认是true；</p> <p>javax.jdo.option.NonTransactionalRead：是否允许非事务的读，默认是true；</p> <p>javax.jdo.option.ConnectionUserName：username，默认APP；</p> <p>javax.jdo.option.ConnectionPassword：password，默认mine；</p> <p>javax.jdo.option.Multithreaded：是否支持并发访问metastore，默认是true；</p> <p>datanucleus.connectionPoolingType：使用连接池来访问JDBC metastore，默认是DBCP；</p> <p>datanucleus.validateTables：检查是否存在表的schema，默认是false；</p> <p>datanucleus.validateColumns：检查是否存在列的schema，默认false；</p> <p>datanucleus.validateConstraints：检查是否存在constraint的schema，默认false；</p> <p>datanucleus.stroeManagerType：元数据存储类型，默认rdbms；</p> <p>datanucleus.autoCreateSchema：在不存在时是否自动创建必要的schema，默认是true；</p> <p>datanucleus.aotuStartMechanismMode：如果元数据表不正确，抛出异常，默认是checked；</p> <p>datanucleus.transactionIsolation：默认的事务隔离级别，默认是read-committed；</p> <p>datanucleus.cache.level2：使用二级缓存，默认是false；</p> <p>datanucleus.cache.level2.type：二级缓存的类型，有两种，SOFT:软引用，WEAK:弱引用，默认是SOFT；</p> <p>datanucleus.identifierFactory：id工厂生产表和列名的名字，默认是datanucleus；</p> <p>datanucleus.plugin.pluginRegistryBundleCheck：当plugin被发现并且重复时的行为，默认是LOG；</p> <p>hive.metastroe.warehouse.dir：数据仓库的位置，默认是/user/hive/warehouse；</p> <p>hive.metastore.execute.setugi：非安全模式，设置为true会令metastore以客户端的用户和组权限执行DFS操作，默认是false，这个属性需要服务端和客户端同时设置；</p> <p>hive.metastore.event.listeners：metastore的事件监听器列表，逗号隔开，默认是空；</p> <p>hive.metastore.partition.inherit.table.properties：当新建分区时自动继承的key列表，默认是空；</p> <p>hive.metastore.end.function.listeners：metastore函数执行结束时的监听器列表，默认是空；</p> <p>hive.metastore.event.expiry.duration：事件表中事件的过期时间，默认是0；</p> <p>hive.metastore.event.clean.freq：metastore中清理过期事件的定时器的运行周期，默认是0；</p> <p>hive.metastore.connect.retries：创建metastore连接时的重试次数，默认是5；</p> <p>hive.metastore.client.connect.retry.delay：客户端在连续的重试连接等待的时间，默认1；</p> <p>hive.metastore.client.socket.timeout：客户端socket超时时间，默认20秒；</p> <p>hive.metastore.rawstore.impl：原始metastore的存储实现类，默认是org.apache.hadoop.hive.metastore.ObjectStore；</p> <p>hive.metastore.batch.retrieve.max：在一个batch获取中，能从metastore里取出的最大记录数，默认是300；</p> <p>hive.metastore.ds.connection.url.hook：查找JDO连接url时hook的名字，默认是javax.jdo.option.ConnectionURL；</p> <p>hive.metastore.ds.retry.attempts：当出现连接错误时重试连接的次数，默认是1次；</p> <p>hive.metastore.ds.retry.interval：metastore重试连接的间隔时间，默认1000毫秒；</p> <p>hive.metastore.server.min.threads：在thrift服务池中最小的工作线程数，默认是200；</p> <p>hive.metastore.server.max.threads：最大线程数，默认是100000；</p> <p>hive.metastore.server.tcp.keepalive：metastore的server是否开启长连接，长连可以预防半连接的积累，默认是true；</p> <p>hive.metastore.sasl.enabled：metastore thrift接口的安全策略，开启则用SASL加密接口，客户端必须要用Kerberos机制鉴权，默认是不开启false；</p> <p>hive.metastore.kerberos.keytab.file：在开启sasl后kerberos的keytab文件存放路径，默认是空；</p> <p>hive.metastore.kerberos.principal：kerberos的principal，_HOST部分会动态替换，默认是<a href="mailto:hive-metastore/_HOST@EXAMPLE.COM">hive-metastore/_HOST@EXAMPLE.COM</a>；</p> <p>hive.metastore.cache.pinobjtypes：在cache中支持的metastore的对象类型，由逗号分隔，默认是Table,StorageDescriptor,SerDeInfo,Partition,Database,Type,FieldSchema,Order；</p> <p>hive.metastore.authorization.storage.checks：在做类似drop partition操作时，metastore是否要认证权限，默认是false；</p> <p>hive.metastore.schema.verification：强制metastore的schema一致性，开启的话会校验在metastore中存储的信息的版本和hive的jar包中的版本一致性，并且关闭自动schema迁移，用户必须手动的升级hive并且迁移schema，关闭的话只会在版本不一致时给出警告，默认是false不开启；</p><img src ="http://www.blogjava.net/changedi/aggbug/404981.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-10-14 17:39 <a href="http://www.blogjava.net/changedi/archive/2013/10/14/404981.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（4）</title><link>http://www.blogjava.net/changedi/archive/2013/09/23/404346.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 23 Sep 2013 10:12:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/09/23/404346.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/404346.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/09/23/404346.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/404346.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/404346.html</trackback:ping><description><![CDATA[<p>hive.exec.drop.ignorenoneexistent：在drop表或者视图时如果发现表或视图不存在，是否报错，默认是true；</p> <p>hive.exec.show.job.failure.debug.info：在作业失败时是否提供一个任务debug信息，默认true；</p> <p>hive.auto.progress.timeout：运行自动progressor的时间间隔，默认是0等价于forever；</p> <p>hive.table.parameters.default：新建表的属性字段默认值，默认是empty空；</p> <p>hive.variable.substitute：是否支持变量替换，如果开启的话，支持语法如${var} ${system:var}和${env.var}，默认是true；</p> <p>hive.error.on.empty.partition：在遇到结果为空的动态分区时是否报错，默认是false；</p> <p>hive.exim.uri.scheme.whitelist：在导入导出数据时提供的一个白名单列表，列表项之间由逗号分隔，默认hdfs,pfile；</p> <p>hive.limit.row.max.size：字面意思理解就是在使用limit做数据的子集查询时保证的最小行数据量，默认是100000；</p> <p>hive.limit.optimize.limit.file：使用简单limit查询数据子集时，可抽样的最大文件数，默认是10；</p> <p>hive.limit.optimize.enable：使用简单limit抽样数据时是否开启优化选项，默认是false，关于limit的优化问题，在hive programming书中解释的是这个feature有drawback，对于抽样的不确定性给出了风险提示；</p> <p>hive.limit.optimize.fetch.max：使用简单limit抽样数据允许的最大行数，默认50000，查询query受限，insert不受影响；</p> <p>hive.rework.mapredwork：是否重做mapreduce，默认是false；</p> <p>hive.sample.seednumber：用来区分抽样的数字，默认是0；</p> <p>hive.io.exception.handlers：io异常处理handler类列表，默认是空，当record reader发生io异常时，由这些handler来处理异常；</p> <p>hive.autogen.columnalias.prefix.label：当在执行中自动产生列别名的前缀，当类似count这样的聚合函数起作用时，如果不明确指出count(a) as xxx的话，那么默认会从列的位置的数字开始算起添加，比如第一个count的结果会冠以列名_c0，接下来依次类推，默认值是_c，数据开发过程中应该很多人都看到过这个别名；</p> <p>hive.autogen.columnalias.prefix.includefuncname：在自动生成列别名时是否带函数的名字，默认是false；</p> <p>hive.exec.perf.logger：负责记录客户端性能指标的日志类名，必须是org.apache.hadoop.hive.ql.log.PerfLogger的子类，默认是org.apache.hadoop.hive.ql.log.PerfLogger；</p> <p>hive.start.cleanup.scratchdir：当启动hive服务时是否清空hive的scratch目录，默认是false；</p> <p>hive.output.file.extension：输出文件扩展名，默认是空；</p> <p>hive.insert.into.multilevel.dirs：是否插入到多级目录，默认是false；</p> <p>hive.files.umask.value：hive创建文件夹时的dfs.umask值，默认是0002；</p><img src ="http://www.blogjava.net/changedi/aggbug/404346.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-09-23 18:12 <a href="http://www.blogjava.net/changedi/archive/2013/09/23/404346.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（3）</title><link>http://www.blogjava.net/changedi/archive/2013/09/10/403890.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 10 Sep 2013 05:45:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/09/10/403890.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/403890.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/09/10/403890.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/403890.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/403890.html</trackback:ping><description><![CDATA[<p>hive.exec.script.maxerrsize：一个map/reduce任务允许打印到标准错误里的最大字节数，为了防止脚本把分区日志填满，默认是100000；</p> <p>hive.exec.script.allow.partial.consumption：hive是否允许脚本不从标准输入中读取任何内容就成功退出，默认关闭false；</p> <p>hive.script.operator.id.env.var：在用户使用transform函数做自定义map/reduce时，存储唯一的脚本标识的环境变量的名字，默认HIVE_SCRIPT_OPERATOR_ID；</p> <p>hive.exec.compress.output：控制hive的查询结果输出是否进行压缩，压缩方式在hadoop的mapred.output.compress中配置，默认不压缩false；</p> <p>hive.exec.compress.intermediate：控制hive的查询中间结果是否进行压缩，同上条配置，默认不压缩false；</p> <p>hive.exec.parallel：hive的执行job是否并行执行，默认不开启false，在很多操作如join时，子查询之间并无关联可独立运行，这种情况下开启并行运算可以大大加速；</p> <p>hvie.exec.parallel.thread.number：并行运算开启时，允许多少作业同时计算，默认是8；</p> <p>hive.exec.rowoffset：是否提供行偏移量的虚拟列，默认是false不提供，Hive有两个虚拟列:一个是INPUT__FILE__NAME,表示输入文件的路径，另外一个是BLOCK__OFFSET__INSIDE__FILE，表示记录在文件中的块偏移量，这对排查出现不符合预期或者null结果的查询是很有帮助的（来自<a href="http://open.taobao.org/hive%E8%B0%83%E4%BC%98%E4%B8%89/">这篇文章</a>）；</p> <p>hive.task.progress：控制hive是否在执行过程中周期性的更新任务进度计数器，开启这个配置可以帮助job tracker更好的监控任务的执行情况，但是会带来一定的性能损耗，当动态分区标志hive.exec.dynamic.partition开启时，本配置自动开启；</p> <p>hive.exec.pre.hooks：执行前置条件，一个用逗号分隔开的实现了org.apache.hadoop.hive.ql.hooks.ExecuteWithHookContext接口的java class列表，配置了该配置后，每个hive任务执行前都要执行这个执行前钩子，默认是空；</p> <p>hive.exec.post.hooks：同上，执行后钩子，默认是空；</p> <p>hive.exec.failure.hooks：同上，异常时钩子，在程序发生异常时执行，默认是空；</p> <p>hive.mergejob.maponly：试图生成一个只有map的任务去做merge，前提是支持CombineHiveInputFormat，默认开启true；</p> <p>hive.mapjoin.smalltable.filesize：输入表文件的mapjoin阈值，如果输入文件的大小小于该值，则试图将普通join转化为mapjoin，默认25MB；</p> <p>hive.mapjoin.localtask.max.memory.usage：mapjoin本地任务执行时hash表容纳key/value的最大量，超过这个值的话本地任务会自动退出，默认是0.9；</p> <p>hive.mapjoin.followby.gby.localtask.max.memory.usage：类似上面，只不过是如果mapjoin后有一个group by的话，该配置控制类似这样的query的本地内存容量上限，默认是0.55；</p> <p>hive.mapjoin.check.memory.rows：在运算了多少行后执行内存使用量检查，默认100000；</p> <p>hive.heartbeat.interval：发送心跳的时间间隔，在mapjoin和filter操作中使用，默认1000；</p> <p>hive.auto.convert.join：根据输入文件的大小决定是否将普通join转换为mapjoin的一种优化，默认不开启false；</p> <p>hive.script.auto.progress：hive的transform/map/reduce脚本执行时是否自动的将进度信息发送给TaskTracker来避免任务没有响应被误杀，本来是当脚本输出到标准错误时，发送进度信息，但是开启该项后，输出到标准错误也不会导致信息发送，因此有可能会造成脚本有死循环产生，但是TaskTracker却没有检查到从而一直循环下去；</p> <p>hive.script.serde：用户脚本转换输入到输出时的SerDe约束，默认是org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe；</p> <p>hive.script.recordreader：从脚本读数据的时候的默认reader，默认是org.apache.hadoop.hive.ql.exec.TextRecordReader；</p> <p>hive.script.recordwriter：写数据到脚本时的默认writer，默认org.apache.hadoop.hive.ql.exec.TextRecordWriter；</p> <p>hive.input.format：输入格式，默认是org.apache.hadoop.hive.ql.io.CombineHiveInputFormat，如果出现问题，可以改用org.apache.hadoop.hive.ql.io.HiveInputFormat；</p> <p>hive.udtf.auto.progress：UDTF执行时hive是否发送进度信息到TaskTracker，默认是false；</p> <p>hive.mapred.reduce.tasks.speculative.execution：reduce任务推测执行是否开启，默认是true；</p> <p>hive.exec.counters.pull.interval：运行中job轮询JobTracker的时间间隔，设置小会影响JobTracker的load，设置大可能看不出运行任务的信息，要去平衡，默认是1000；</p> <p>hive.enforce.bucketing：数据分桶是否被强制执行，默认false，如果开启，则写入table数据时会启动分桶，个人对分桶的理解可以参考<a href="http://kickstarthadoop.blogspot.com/2011/10/enable-sorted-bucketing-in-hive.html">这篇文章</a>，写的较清楚，有示例，分桶在做全表查询和带有分区字段查询时感觉影响不大，主要作用在sampling；</p> <p>hive.enforce.sorting：开启强制排序时，插数据到表中会进行强制排序，默认false；</p> <p>hive.optimize.reducededuplication：如果数据已经根据相同的key做好聚合，那么去除掉多余的map/reduce作业，此配置是文档的推荐配置，建议打开，默认是true；</p> <p>hive.exec.dynamic.partition：在DML/DDL中是否支持动态分区，默认false；</p> <p>hive.exec.dynamic.partition.mode：默认strict，在strict模式下，动态分区的使用必须在一个静态分区确认的情况下，其他分区可以是动态；</p> <p>hive.exec.max.dynamic.partitions：动态分区的上限，默认1000；</p> <p>hive.exec.max.dynamic.partitions.pernode：每个mapper/reducer节点可以创建的最大动态分区数，默认100；</p> <p>hive.exec.max.created.files：一个mapreduce作业能创建的HDFS文件最大数，默认是100000；</p> <p>hive.exec.default.partition.name：当动态分区启用时，如果数据列里包含null或者空字符串的话，数据会被插入到这个分区，默认名字是__HIVE_DEFAULT_PARTITION__；</p> <p>hive.fetch.output.serde：FetchTask序列化fetch输出时需要的SerDe，默认是org.apache.hadoop.hive.serde2.DelimitedJSONSerDe;</p> <p>hive.exec.mode.local.auto：是否由hive决定自动在local模式下运行，默认是false，关于满足什么条件开启localmode，可以参考<a href="http://hugh-wangp.iteye.com/blog/1602551">这篇文章</a>；</p><img src ="http://www.blogjava.net/changedi/aggbug/403890.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-09-10 13:45 <a href="http://www.blogjava.net/changedi/archive/2013/09/10/403890.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（2）</title><link>http://www.blogjava.net/changedi/archive/2013/08/15/402857.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Thu, 15 Aug 2013 06:47:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/08/15/402857.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/402857.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/08/15/402857.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/402857.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/402857.html</trackback:ping><description><![CDATA[<p>标记粗体的我个人认为在运行hive sql时可以根据数据情况进行设置，当然还有一些join的优化的配置需要单独研究。</p> <p>&nbsp;</p> <p><strong>mapred.reduce.tasks</strong>：每个作业的reduce任务数，默认是hadoop client的配置1个；</p> <p><strong>hive.exec.reducers.bytes.per.reducer</strong>：每个reducer的大小，默认是1G，输入文件如果是10G，那么就会起10个reducer；</p> <p>hive.exec.reducers.max：reducer的最大个数，如果在mapred.reduce.tasks设置为负值，那么hive将取该值作为reducers的最大可能值。当然还要依赖（输入文件大小/hive.exec.reducers.bytes.per.reducer）所得出的大小，取其小值作为reducer的个数，hive默认是999；</p> <p>hive.fileformat.check：加载数据文件时是否校验文件格式，默认是true；</p> <p>hive.groupby.skewindata：group by操作是否允许数据倾斜，默认是false，当设置为true时，执行计划会生成两个map/reduce作业，第一个MR中会将map的结果随机分布到reduce中，达到负载均衡的目的来解决数据倾斜，可以参看阿里巴巴数据平台的<a href="http://www.alidata.org/archives/595">这篇文章</a>了解hive对于数据倾斜时group by的处理；</p> <p>hive.groupby.mapaggr.checkinterval：map端做聚合时，group by 的key所允许的数据行数，超过该值则进行分拆，默认是100000；</p> <p>hive.mapred.local.mem：本地模式时，map/reduce的内存使用量，默认是0，就是无限制；</p> <p>hive.mapjoin.followby.map.aggr.hash.percentmemory：map端聚合时hash表的内存占比，该设置约束group by在map join后进行，否则使用hive.map.aggr.hash.percentmemory来确认内存占比，默认值0.3；</p> <p>hive.map.aggr.hash.force.flush.memeory.threshold：map端聚合时hash表的最大可用内存，如果超过该值则进行flush数据，默认是0.9；</p> <p>hive.map.aggr.hash.min.reduction：如果hash表的容量与输入行数之比超过这个数，那么map端的hash聚合将被关闭，默认是0.5，设置为1可以保证hash聚合永不被关闭；</p> <p>hive.optimize.groupby：在做分区和表查询时是否做分桶group by，默认开启true；</p> <p>hive.multigroupby.singlemr：将多个group by产出为一个单一map/reduce任务计划，当然约束前提是group by有相同的key，默认是false；</p> <p>hive.optimize.cp：列裁剪，默认开启true，在做查询时只读取用到的列，这个是个有用的优化；</p> <p>hive.optimize.index.filter：自动使用索引，默认不开启false；</p> <p>hive.optimize.index.groupby：是否使用聚集索引优化group-by查询，默认关闭false；</p> <p>hive.optimize.ppd：是否支持谓词下推，默认开启；所谓谓词下推，将外层查询块的 WHERE 子句中的谓词移入所包含的较低层查询块（例如视图），从而能够提早进行数据过滤以及有可能更好地利用索引。<a href="http://doudouclever.blog.163.com/blog/static/175112310201261355984/">这篇中文文章</a>简单的说明了在关系数据库里的应用；</p> <p>hive.optimize.ppd.storage：谓词下推开启时，谓词是否下推到存储handler，默认开启，在谓词下推关闭时不起作用；</p> <p>hive.ppd.recognizetransivity：在等值join条件下是否产地重复的谓词过滤器，默认开启；</p> <p>hive.join.cache.size：在做表join时缓存在内存中的行数，默认25000；</p> <p>hive.mapjoin.bucket.cache.size：mapjoin时内存cache的每个key要存储多少个value，默认100；</p> <p>hive.optimize.skewjoin：是否开启数据倾斜的join优化，默认不开启false；</p> <p>hive.skewjoin.key：判断数据倾斜的阈值，如果在join中发现同样的key超过该值则认为是该key是倾斜的join key，默认是100000；</p> <p>hive.skewjoin.mapjoin.map.tasks：在数据倾斜join时map join的map数控制，默认是10000；</p> <p>hive.skewjoin.mapjoin.min.split：数据倾斜join时map join的map任务的最小split大小，默认是33554432，该参数要结合上面的参数共同使用来进行细粒度的控制；</p> <p>hive.mapred.mode：hive操作执行时的模式，默认是nonstrict非严格模式，如果是strict模式，很多有风险的查询会被禁止运行，比如笛卡尔积的join和动态分区；</p><img src ="http://www.blogjava.net/changedi/aggbug/402857.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-08-15 14:47 <a href="http://www.blogjava.net/changedi/archive/2013/08/15/402857.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（1）</title><link>http://www.blogjava.net/changedi/archive/2013/08/13/402741.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 13 Aug 2013 07:24:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/08/13/402741.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/402741.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/08/13/402741.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/402741.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/402741.html</trackback:ping><description><![CDATA[<p>一个hive任务，如何才算是优化的任务，hadoop job config里哪些配置能影响hive的效率。看看hive的详细配置我们可以略知一二。</p> <p>hive的配置：</p> <p>hive.ddl.output.format：hive的ddl语句的输出格式，默认是text，纯文本，还有json格式，这个是0.90以后才出的新配置；</p> <p>hive.exec.script.wrapper：hive调用脚本时的包装器，默认是null，如果设置为python的话，那么在做脚本调用操作时语句会变为python &lt;script command&gt;，null的话就是直接执行&lt;script command&gt;；</p> <p>hive.exec.plan：hive执行计划的文件路径，默认是null，会在运行时自动设置，形如hdfs://xxxx/xxx/xx；</p> <p>hive.exec.scratchdir：hive用来存储不同阶段的map/reduce的执行计划的目录，同时也存储中间输出结果，默认是/tmp/&lt;user.name&gt;/hive，我们实际一般会按组区分，然后组内自建一个tmp目录存储；</p> <p>hive.exec.submitviachild：在非local模式下，决定hive是否要在独立的jvm中执行map/reduce；默认是false，也就是说默认map/reduce的作业是在hive的jvm上去提交的；</p> <p>hive.exec.script.maxerrsize：当用户调用transform或者map或者reduce执行脚本时，最大的序列化错误数，默认100000，一般也不用修改；</p> <p>hive.exec.compress.output：一个查询的最后一个map/reduce任务输出是否被压缩的标志，默认为false，但是一般会开启为true，好处的话，根据<a href="http://www.oratea.net/?p=748">这篇文章</a>，节省空间不说，在不考虑cpu压力的时候会提高io；</p> <p>hive.exec.compress.intermediate：类似上个，在一个查询的中间的map/reduce任务输出是否要被压缩，默认false，但一般也会手动开启，<a href="https://groups.google.com/a/cloudera.org/forum/#!topic/cdh-user/plDwzWCZdaw">这篇文章</a>对比了这两个配置，其中回复讲到，The former affects compression between MapReduce stages in a Hive query. The latter affects compression between map and reduce phases during the shuffle. 如此而已；</p> <p>hive.jar.path：当使用独立的jvm提交作业时，hive_cli.jar所在的位置，无默认值；</p> <p>hive.aux.jars.path：当用户自定义了UDF或者SerDe，这些插件的jar都要放到这个目录下，无默认值；</p> <p>hive.partition.pruning：在编译器发现一个query语句中使用分区表然而未提供任何分区谓词做查询时，抛出一个错误从而保护分区表，默认是nonstrict；（待读源码后细化，网上资料极少）</p> <p>hive.map.aggr：map端聚合是否开启，默认开启；<a href="http://dev.bizo.com/2013/02/map-side-aggregations-in-apache-hive.html">这篇文章</a>给出了map端聚合的很详细的描述；</p> <p>hive.join.emit.interval：在发出join结果之前对join最右操作缓存多少行的设定，默认1000；<a href="https://issues.apache.org/jira/browse/HIVE-4952">hive jira</a>里有个对该值设置太小的bugfix；</p> <p>hive.map.aggr.hash.percentmemory：map端聚合时hash表所占用的内存比例，默认0.5，这个在map端聚合开启后使用，参看hive.map.aggr里提到的文章； <p>hive.default.fileformat：CREATE TABLE语句的默认文件格式，默认TextFile，其他可选的有SequenceFile、RCFile还有Orc；对于文件格式的说明对比，<a href="http://www.adaltas.com/blog/2012/03/13/hdfs-hive-storage-format-compression/">这里</a>有篇对比可以参考，<a href="http://www.infoq.com/cn/articles/hadoop-file-format">infoq上有篇文章</a>讲了hadoop的文件格式； <p>hive.merge.mapfiles：在只有map的作业结束时合并小文件，默认开启true； <p>hive.merge.mapredfiles：在一个map/reduce作业结束后合并小文件，默认不开启false；</p> <p>hive.merge.size.per.task：作业结束时合并文件的大小，默认256MB；</p> <p>hive.merge.smallfiles.avgsize：在作业输出文件小于该值时，起一个额外的map/reduce作业将小文件合并为大文件，小文件的基本阈值，设置大点可以减少小文件个数，需要mapfiles和mapredfiles为true，默认值是16MB；</p><img src ="http://www.blogjava.net/changedi/aggbug/402741.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-08-13 15:24 <a href="http://www.blogjava.net/changedi/archive/2013/08/13/402741.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hadoop fs命令</title><link>http://www.blogjava.net/changedi/archive/2013/08/12/402696.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 12 Aug 2013 07:30:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/08/12/402696.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/402696.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/08/12/402696.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/402696.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/402696.html</trackback:ping><description><![CDATA[<p>最近使用hive做一些etl工作，除了日常sql的编写，了解hadoop及hive的一些底层原理性质的东西包括调优非常有必要，一次hive调优就把原来的零散文件做了合并。首先记下hadoop常用的命令：（hadoop fs -help列出全部）</p> <p>1，hadoop fs &#8211;fs [local | &lt;file system URI&gt;]：声明hadoop使用的文件系统，如果不声明的话，使用当前配置文件配置的，按如下顺序查找：hadoop jar里的hadoop-default.xml-&gt;$HADOOP_CONF_DIR下的hadoop-default.xml-&gt;$HADOOP_CONF_DIR下的hadoop-site.xml。使用local代表将本地文件系统作为hadoop的DFS。如果传递uri做参数，那么就是特定的文件系统作为DFS。</p> <p>2，hadoop fs &#8211;ls &lt;path&gt;：等同于本地系统的ls，列出在指定目录下的文件内容，支持pattern匹配。输出格式如filename(full path)&nbsp;&nbsp; &lt;r n&gt;&nbsp; size.其中n代表replica的个数，size代表大小（单位bytes）。</p> <p>3，hadoop fs &#8211;lsr &lt;path&gt;：递归列出匹配pattern的文件信息，类似ls，只不过递归列出所有子目录信息。</p> <p>4，hadoop fs &#8211;du &lt;path&gt;：列出匹配pattern的指定的文件系统空间总量（单位bytes），等价于unix下的针对目录的du &#8211;sb &lt;path&gt;/*和针对文件的du &#8211;b &lt;path&gt; ，输出格式如name(full path)&nbsp; size(in bytes)。</p> <p>5，hadoop fs &#8211;dus &lt;path&gt;：等价于-du，输出格式也相同，只不过等价于unix的du -sb。</p> <p>6，hadoop fs &#8211;mv &lt;src&gt; &lt;dst&gt;：将制定格式的文件 move到指定的目标位置。当src为多个文件时，dst必须是个目录。</p> <p>7，hadoop fs &#8211;cp &lt;src&gt; &lt;dst&gt;：拷贝文件到目标位置，当src为多个文件时，dst必须是个目录。</p> <p>8，hadoop fs &#8211;rm [-skipTrash] &lt;src&gt;：删除匹配pattern的指定文件，等价于unix下的rm &lt;src&gt;。</p> <p>9，hadoop fs &#8211;rmr [skipTrash] &lt;src&gt;：递归删掉所有的文件和目录，等价于unix下的rm &#8211;rf &lt;src&gt;。</p> <p>10，hadoop fs &#8211;rmi [skipTrash] &lt;src&gt;：等价于unix的rm &#8211;rfi &lt;src&gt;。</p> <p>11，hadoop fs &#8211;put &lt;localsrc&gt; &#8230; &lt;dst&gt;：从本地系统拷贝文件到DFS。</p> <p>12，hadoop fs &#8211;copyFromLocal &lt;localsrc&gt; &#8230; &lt;dst&gt;：等价于-put。</p> <p>13，hadoop fs &#8211;moveFromLocal &lt;localsrc&gt; &#8230; &lt;dst&gt;：等同于-put，只不过源文件在拷贝后被删除。</p> <p>14，hadoop fs &#8211;get [-ignoreCrc] [-crc] &lt;src&gt; &lt;localdst&gt;：从DFS拷贝文件到本地文件系统，文件匹配pattern，若是多个文件，则dst必须是目录。</p> <p>15，hadoop fs &#8211;getmerge &lt;src&gt; &lt;localdst&gt;：顾名思义，从DFS拷贝多个文件、合并排序为一个文件到本地文件系统。</p> <p>16，hadoop fs &#8211;cat &lt;src&gt;：展示文件内容。</p> <p>17，hadoop fs &#8211;copyToLocal [-ignoreCrc] [-crc] &lt;src&gt; &lt;localdst&gt;：等价于-get。</p> <p>18，hadoop fs &#8211;mkdir &lt;path&gt;：在指定位置创建目录。</p> <p>19，hadoop fs &#8211;setrep [-R] [-w] &lt;rep&gt; &lt;path/file&gt;：设置文件的备份级别，-R标志控制是否递归设置子目录及文件。</p> <p>20，hadoop fs &#8211;chmod [-R] &lt;MODE[,MODE]&#8230;|OCTALMODE&gt; PATH&#8230;：修改文件的权限，-R标记递归修改。MODE为a+r,g-w,+rwx等，OCTALMODE为755这样。</p> <p>21，hadoop fs -chown [-R] [OWNER][:[GROUP]] PATH&#8230;：修改文件的所有者和组。-R表示递归。</p> <p>22，hadoop fs -chgrp [-R] GROUP PATH&#8230;：等价于-chown &#8230; :GROUP &#8230;。</p> <p>23，hadoop fs &#8211;count[-q] &lt;path&gt;：计数文件个数及所占空间的详情，输出表格的列的含义依次为：DIR_COUNT,FILE_COUNT,CONTENT_SIZE,FILE_NAME或者如果加了-q的话，还会列出QUOTA,REMAINING_QUOTA,SPACE_QUOTA,REMAINING_SPACE_QUOTA。</p> <p>最后就是万能的hadoop fs &#8211;help [cmd]啦~~</p><img src ="http://www.blogjava.net/changedi/aggbug/402696.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-08-12 15:30 <a href="http://www.blogjava.net/changedi/archive/2013/08/12/402696.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>让数据鲜活起来</title><link>http://www.blogjava.net/changedi/archive/2013/05/11/399143.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Sat, 11 May 2013 05:24:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/05/11/399143.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/399143.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/05/11/399143.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/399143.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/399143.html</trackback:ping><description><![CDATA[<p>&#8212;&#8212;数据是什么，不是冰冷的数字，不是躺在数据库里，躺在硬盘上，躺在图书馆的那些尘埃，而是鲜活的东西。</p> <p>outline：</p> <p><strong>数据可视化背景：数据+可视化 what &amp; why</strong>，</p> <p>数据，不用多说，最强大的积累，世界的来源，没有数据的积累，人类什么都不是。数据有几大类：</p> <p>俗话说：一图胜千言，各种感觉器官接受信息的比例是：视觉87％，听觉7％，嗅觉3．4％，触觉1．5％，味觉1％</p> <p>【展示各种比例数据】</p> <p><strong>数据可视化：如何图表展示 how</strong></p> <p>1，时间趋势的可视化</p> <p>时间是个天然的数据维度，我们时刻关注时间，不论数据是哪种类型，我们必然会期望看到它在这段时间内是上升还是下降，发现其中的趋势，回顾过去，超越现在，预测未来。</p> <p>趋势数据分离散型和连续型，离散的数据基于事件的，一段时间一个事件就会发生若干次，比如一个赛季就会发生38场足球比赛，WDM中基于天的数据都是离散事件，比如我们统计crash的次数；而连续型的数据是基于实时变化的，可以认为是随着时间不断变化的，比如温度，会一直持续变化。总结，我们人类定义的最小时间单位基本上在秒这个概念上，我们做数据的可以这样去区分离散和连续：如果事件在很小的时间粒度发生，比如UV这样的，基本可以定位到秒级别，那么这是连续型数据；如刚才的crash信息，因为不可能是每秒都在发生，甚至都不是每分钟都发生，那么可以定义为离散型数据。</p> <p>--离散型：柱形图和散点图，当然对于非时序的分类数据，离散型的数据图展示也满足。</p> <p>基本柱形图：横轴时间轴，纵轴数据轴，要注意柱形宽度，柱形间隔，柱形高度，柱形图的视觉线索是：高度，数值越小，柱形越矮，数值越大，柱形越高。tip1：处理的数据都是正数时，请永远让柱形图的数值轴从0开始，否则会让人难以从视觉上比较各柱形的高度。如果在时间趋势柱形图上想表达多一个维度的概念，可以利用柱形颜色加以区分。tip2：柱形间隔选择时要小心，如果间隔宽度和柱形宽度相近，视觉上会产生振动效应，给人感觉就是柱形和间隔的角色互换。</p> <p>堆叠柱形图：在基本柱形图上，堆叠多个矩形。如果数据存在子分类，并且各个子分类之和是有意义的，就可以使用堆叠柱形图。注意一点，这种情况子分类不宜过多。</p> <p>散点图：用圆点代替矩形，可能意义更明确，小巧而集中，这种表达有一种&#8220;流&#8221;的感觉。散点图的视觉线索是：位置，通过xy坐标来观察，通过与其他点的位置来比较。散点图不强制要求从0开始。注意一点，数据不永远是真实的，笔误等各种原因都会导致数据偏离现实。</p> <p>--连续型：其实数据等价于离散型，区别只在表达的现实意义。</p> <p>点点连接：散点图连接起来，基本折线图，更有跃动感，默认了线性变化，也就是说从a点到b点的稳定变化。（表示人口变化没问题）</p> <p>台阶形：适合表达会长时间停留在某个数值上，然后突然增长或衰退的事务变化。（银行利率）</p> <p>平滑形：数据点杂乱且趋势不是简单的线性稳定趋势，那么可以估算出一条平滑曲线，一般我们会拟合出一条样条曲线或者贝塞尔曲线。</p> <p>2，比例的可视化（分类的可视化）</p> <p>在比例中寻求什么？最大，最小，总体分布？</p> <p>--整体中包含各个部分：占比数据非常重要。</p> <p>饼图：像切蛋糕一样切成若干个楔形，每个楔形代表整体的一个部分，楔形的角度代表占比（视觉线索也可以认为是面积），总和一定是100%。饼图在表示数据时有着一些争议，因为它不像柱形图或者基于位置的图形那样精确，衡量角度或比例要比衡量长度复杂。但是如果数据块不是特别多，饼图是个很不错的选择（良好组织数据，不要将一个饼图分成太多块）。对饼图的一般设计是选择好颜色，同时按照顺序从12点方向开始顺时针排列楔形块。颜色的深浅代表了重点的强弱，需要强调的部分颜色要深。</p> <p>面包圈图：中间有洞的饼图，视觉线索不再是角度，但是跨过的弧度仍然和面积一样可以作为视觉线索。面包圈图中间部分适合放置标签或者其他内容。</p> <p>堆叠柱形图：不仅仅用于时间趋势数据类型，对于比例的展示，堆叠图可以通过改变横轴为类别轴，将柱形高度定义为单个类别的总和，内部柱形高度代表了子分类的数值。堆叠柱形图的表达能力是N个饼图的和。</p> <p>板块层级图：treemap，一种基于面积的可视化方式，通过每一个板块（矩形）的尺寸大小来度量。外部矩形代表父类别，而内部矩形代表子类别。最适合显示层级结构和树状结构的数据。</p> <p>--带时间属性的比例：比例加时间属性，很自然的组合</p> <p>堆叠面积图：水平轴时间，垂直轴是比例（100%），可以理解为按时间将一系列的堆叠柱形图连起来。</p> <p>3，关系的可视化</p> <p>关系中寻求的就是变量之间的关联，比如一个量增加了，另一个怎么变？它们是因果关系还是关联关系，更深入可以探求到数据的分布。</p> <p>--关联性：联系（correlation），发现事物之间的关联，如果确定关联性，那么可以根据一项已知指标来预测另一指标。</p> <p>散点图：表示变量之间的关系，这时的散点图横轴代表一个变量的数值，纵轴也代表一个变量的数值，每个点的坐标xy代表了关系。这时的散点图读图方法是：从左往右，如果是上升的趋势，那么是正相关，否则是负相关，如果杂乱无章，则不相关。</p> <p>散点图矩阵：多个x轴，多个y轴，多个变量之间进行xy比较。</p> <p>气泡图：同散点图一样，只不过气泡的大小表示第三个变量（比散点图多一个维度）。tip：用圆形表示数据时，要用面积来定义尺寸，而不是半径、直径或周长。气泡图中的圆形可以被正方形等其他图形代替。</p> <p>--分布：利用平均数、中位数、众数、重心、线型等来判断数据的分布。</p> <p>直方图：等价于柱形图，横轴表示某个延续性变量，纵轴表示频率或可能性，柱形高度表示柱形所在取值域出现的频率或可能性，柱形宽度表示数值轴上的某个取值域，取值域应该彼此一致。一般直方图的柱形间隔非常小或者没有，并且英文叫做histogram，而柱形图我们叫bar或者column。直方图用来主要观察分布，而不是看到每个具体的数值。比如图像分析里有个典型的图像颜色特征叫做颜色直方图，就是统计一幅图像的各个灰度级别的像素个数绘制的一个图形。</p> <p>密度图：直方图的数值轴是延续性的，但是整个分布依然被分成了多个柱形。每个柱形代表的都是一些条目的集合。对于柱形内部的变化，柱形图无法表达，因此可以利用密度图来对分布的细节变化进行可视化。基本架构是：横轴代表数值轴，纵轴代表可能性大小或者比例，曲线高度代表相应值发生的可能性，曲线下的面积代表整体1.</p> <p>最后还可以考虑直方图和密度图的结合，等价于柱状图和折线图结合。</p> <p>4，空间关系的可视化</p> <p>我们一般把空间信息的寻求量化到地图层面，基于地域维度。利用地图做空间关系的可视化，要比用常规图表更直接，结合地域维度使地区数据更显而易见。同时对于个别感兴趣的区域的数据可以更方便的观察，可以让人们专享数据。</p> <p>--具体位置：给出地点的经纬度信息</p> <p>单纯点图：直接将经纬度点信息标注到地图对应位置。类似现实中在地图上按图钉。这样的可视化只能标注出发生特定事件的地点，也就是指明了类别，维度的话只包含地域维度和事件类别维度。可以看做一维。在这个点图上的扩展就是加入跟踪信息，将有序的点用直线连接起来。线的长短将会成为明显的视觉线索表明点与点之间的距离关系。</p> <p>气泡地图：在单纯点图的基础上，加一个维度，表明了在发生特定类事件的地区的数值大小。</p> <p>--地区：以宏观更大范围的汇总数据</p> <p>地区着色：以地区区域为单位，通过着色表示指标的高低。</p> <p>--跨越时间和空间：将时间维度加入空间地图里，查看随时间变化在地区维度上的数据指标变化。</p> <p>系列组图：将一组地区的图表（前面说过的点图或者着色区域图）按时间顺序排放，随着视线的移动（从左到右或从上到下），可以看出变化趋势。</p> <p>刻画变化：不是将状态按顺序展示出来，而是进行差额计算，直接将变化以着色图的形式展现。使观察更聚焦，直接看变化。</p> <p>动画：让数据动起来，是最直观和吸引人的设计。</p> <p><strong>未来：想象力</strong></p> <p>不管怎么讲，数据可视化是辅助我们拿数据讲故事的最有力的武器。</p> <p><strong>与我们结合：数据可视化是最终的展现，数据产品的终极形态有两种，一种是利用数据做强大的分析和挖掘，得到有意义的可以汇总成知识的内容；另一种就是通过丰富的想象力用可视化形式将其展示。</strong></p><img src ="http://www.blogjava.net/changedi/aggbug/399143.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-05-11 13:24 <a href="http://www.blogjava.net/changedi/archive/2013/05/11/399143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>