遇到一例无法向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();
}
}
}
文章来源: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/
所以,调试时千万要注意测试环境中的陷阱。
文章来源:https://www.codelast.com/
➤➤ 版权声明 ➤➤
转载需注明出处:codelast.com
感谢关注我的微信公众号(微信扫一扫):

做过测试,timestamp=13399也是没有问题的,
create \"test\", \"cf\"
put \"test\", \"t1\", \"cf:c1\", \"s1\", 13399
get \"test\", \"t1\"
COLUMN CELL
cf:c1 timestamp=13399, value=s1
用java程序也实现了一次,同样可以查询,应该没有你说的13399这样的就不能插入.
甚至相通rowkey, cf:c1, value不一样时,也能插入,可以设置timestamp为一个更小的值。