<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>无法插入 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E6%97%A0%E6%B3%95%E6%8F%92%E5%85%A5/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Sun, 03 May 2020 12:31:53 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]无法向HBase表插入数据的一个问题</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e6%97%a0%e6%b3%95%e5%90%91hbase%e8%a1%a8%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e6%97%a0%e6%b3%95%e5%90%91hbase%e8%a1%a8%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 13 Jun 2012 11:48:56 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Hbase]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[无法插入]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=4736</guid>

					<description><![CDATA[<p>
遇到一例无法向HBase插入数据的问题，发现问题所在之后觉得超级雷人，特记录下来。<br />
<span id="more-4736"></span><br />
<span style="background-color:#00ff00;">【1】</span>在写程序之前，先通过 hbase shell 来创建一张数据表：</p>
<pre class="brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
create &#39;test&#39;, {NAME =&#62; &#39;f&#39;, COMPRESSION =&#62; &#39;NONE&#39;, VERSIONS =&#62; &#39;1&#39;, TTL =&#62; &#39;5184000&#39;, BLOCKSIZE =&#62; &#39;65536&#39;, IN_MEMORY =&#62; &#39;false&#39;, BLOCKCACHE =&#62; &#39;true&#39;}
</pre>
<p>这样，就创建了一张名为&#8220;test&#8221;的HBase表，其column family为&#8220;f&#8221;。</p>
<p><span style="background-color:#00ff00;">【2】</span>写Java代码，用于向HBase表插入一条记录。代码如下：</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;</pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e6%97%a0%e6%b3%95%e5%90%91hbase%e8%a1%a8%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
遇到一例无法向HBase插入数据的问题，发现问题所在之后觉得超级雷人，特记录下来。<br />
<span id="more-4736"></span><br />
<span style="background-color:#00ff00;">【1】</span>在写程序之前，先通过 hbase shell 来创建一张数据表：</p>
<pre class="brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
create &#39;test&#39;, {NAME =&gt; &#39;f&#39;, COMPRESSION =&gt; &#39;NONE&#39;, VERSIONS =&gt; &#39;1&#39;, TTL =&gt; &#39;5184000&#39;, BLOCKSIZE =&gt; &#39;65536&#39;, IN_MEMORY =&gt; &#39;false&#39;, BLOCKCACHE =&gt; &#39;true&#39;}
</pre>
<p>这样，就创建了一张名为&ldquo;test&rdquo;的HBase表，其column family为&ldquo;f&rdquo;。</p>
<p><span style="background-color:#00ff00;">【2】</span>写Java代码，用于向HBase表插入一条记录。代码如下：</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HBasePutTest {
  private static Logger LOGGER = Logger.getLogger(HBasePutTest.class);
  private static List&lt;Put&gt; list = new ArrayList&lt;Put&gt;();
  private static HTable tableTest = initHTableTest();

  public static HTable initHTableTest() {
    HTable table = null;
    try {
      table = new HTable(new HBaseConfiguration(), &quot;test&quot;);
    } catch (IOException e) {
      LOGGER.error(e.toString());
    }
    return table;
  }

  public static void main(String[] args) {

    Put put = new Put(Bytes.toBytes(&quot;abc&quot;));
    put.add(Bytes.toBytes(&quot;f&quot;),
            Bytes.toBytes(&quot;q&quot;), 13399L,
            Bytes.toBytes(&quot;123&quot;));
    list.add(put);

    try {
      tableTest.put(list);
      LOGGER.info(&quot;Successfully put 1 record into HBase.&quot;);
    } catch (Exception e) {
      LOGGER.error(e.toString());
    } finally {
      list.clear();
    }
  }
}

</pre>
<p><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
上面的代码，向HBase的&ldquo;test&rdquo;表插入了一条记录，row key为&ldquo;abc&rdquo;，value为&ldquo;123&rdquo;，column family为&ldquo;f&rdquo;，qualifier为&ldquo;q&rdquo;（即，column为&ldquo;f:q&rdquo;)，记录的timestamp为13399（随便写的一个值）。</p>
<p><span style="background-color:#00ff00;">【3】</span>代码看上去没有什么问题，因此，我们执行它，然后回到 hbase shell，查看一下记录是否被成功地插入了名为&ldquo;test&rdquo;的HBase表中：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
get &#39;test&#39;, &#39;abc&#39;
</pre>
<p>这表示从&ldquo;test&rdquo;表中取出row key为&ldquo;abc&rdquo;的所有记录。<br />
结果竟然是：一条也没有。为什么？</p>
<p><span style="background-color: rgb(0, 255, 0); ">【4】</span>分析问题。在经过试验之后，发现将Java代码中，要插入记录的timestamp设置为当前时间，是可以成功向HBase插入记录的，于是终于发现，原来是：创建HBase表的时候，设置的那个TTL值，使得timestamp为13399的这条记录，就算是一插入了HBase表，也会被马上删除，所以用 hbase shell 根本看不到。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
所以，调试时千万要注意测试环境中的陷阱。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushSql.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e6%97%a0%e6%b3%95%e5%90%91hbase%e8%a1%a8%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
