Home  >  Article  >  Database  >  [原创]无法向HBase表插入数据的一个问题

[原创]无法向HBase表插入数据的一个问题

WBOY
WBOYOriginal
2016-06-07 16:28:151339browse

转载必须注明出处: http://www.codelast.com/ 遇到一例无法向HBase插入数据的问题,发现问题所在之后觉得超级雷人,特记录下来。 【1】 在写程序之前,先通过 hbase shell 来创建一张数据表: create 'test', {NAME = 'f', COMPRESSION = 'NONE', VERSIONS

转载必须注明出处:http://www.codelast.com/

遇到一例无法向HBase插入数据的问题,发现问题所在之后觉得超级雷人,特记录下来。

【1】在写程序之前,先通过 hbase shell 来创建一张数据表:

create 'test', {NAME => 'f', COMPRESSION => 'NONE', VERSIONS => '1', TTL => '5184000', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}

这样,就创建了一张名为“test”的HBase表,其column family为“f”。

【2】写Java代码,用于向HBase表插入一条记录。代码如下:

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<put> list = new ArrayList<put>();
  private static HTable tableTest = initHTableTest();
  public static HTable initHTableTest() {
    HTable table = null;
    try {
      table = new HTable(new HBaseConfiguration(), "test");
    } catch (IOException e) {
      LOGGER.error(e.toString());
    }
    return table;
  }
  public static void main(String[] args) {
    Put put = new Put(Bytes.toBytes("abc"));
    put.add(Bytes.toBytes("f"),
            Bytes.toBytes("q"), 13399L,
            Bytes.toBytes("123"));
    list.add(put);
    try {
      tableTest.put(list);
      LOGGER.info("Successfully put 1 record into HBase.");
    } catch (Exception e) {
      LOGGER.error(e.toString());
    } finally {
      list.clear();
    }
  }
}
</put></put>

文章来源:http://www.codelast.com/
上面的代码,向HBase的“test”表插入了一条记录,row key为“abc”,value为“123”,column family为“f”,qualifier为“q”(即,column为“f:q”),记录的timestamp为13399(随便写的一个值)。

【3】代码看上去没有什么问题,因此,我们执行它,然后回到 hbase shell,查看一下记录是否被成功地插入了名为“test”的HBase表中:

get 'test', 'abc'

这表示从“test”表中取出row key为“abc”的所有记录。
结果竟然是:一条也没有。为什么?

【4】分析问题。在经过试验之后,发现将Java代码中,要插入记录的timestamp设置为当前时间,是可以成功向HBase插入记录的,于是终于发现,原来是:创建HBase表的时候,设置的那个TTL值,使得timestamp为13399的这条记录,就算是一插入了HBase表,也会被马上删除,所以用 hbase shell 根本看不到。
文章来源:http://www.codelast.com/
所以,调试时千万要注意测试环境中的陷阱。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn