最近用HBase存储数据,记录下方法
- HBase批量写数据(rowkey、列簇、value)
a:连接:
private Connection initHbaseConnection(){
if (StringUtils.isBlank(hbaseZookeeperQuorum) || StringUtils.isBlank(hbaseZookeeperZnodeParent)) {
LOG.warn(“hbaseZookeeperQuorum = {}, hbaseZookeeperZnodeParent = {}, initHbaseConnection return null”,
hbaseZookeeperQuorum, hbaseZookeeperZnodeParent);
throw new IllegalArgumentException(
“initHbaseConnection hbaseZookeeperQuorum/hbaseZookeeperZnodeParent is empty”);
}
Configuration configuration = HBaseConfiguration.create();
configuration.set(“hbase.zookeeper.quorum”, hbaseZookeeperQuorum);
configuration.set(“zookeeper.znode.parent”, hbaseZookeeperZnodeParent);
try {
return ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
LOG.error(“ERROR init Hbase Connection, Hbase Connection is null”);
return null;
}
}
b:单个写
public boolean setProductTag() {
final String family = snTypeEnum == m;
final String rowKey = getRowKey(productType, productId);
Table table = null;
try {
table = hbaseConnection.getTable(TableName.valueOf(HBASE_TABLE_NAME));
Put p = new Put(Bytes.toBytes(rowKey));
p.addColumn(Bytes.toBytes(family), Bytes.toBytes(Sn), expireTime, Bytes.toBytes(Value));
table.put(p);
return true;
} catch (IOException e)
return false;
} finally {
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
LOG.error(“table.close error”, e);
}
}
}
c:写多列rowkey
try {
table = hbaseConnection.getTable(TableName.valueOf(HBASE_TABLE_NAME));
List puts = Lists.newArrayListWithExpectedSize(ptpProductTags.size());
for (PtpProductTag ptpProductTag : ptpProductTags) {
final String family =m;
int productType = 0;
String tId = ;
String Sn = ;
String Value = ;
Long expireTime = ;
final String rowKey =
Put p = new Put(Bytes.toBytes(rowKey));
p.addColumn(Bytes.toBytes(family), Bytes.toBytes(Sn), expireTime, Bytes.toBytes(Value));
puts.add(p);
}
if (!puts.isEmpty()) {
table.put(puts);
}
return true;
} catch (IOException e) {
return false;
} finally {
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
LOG.error(“table.close error”, e);
}
}
2、HBase拉取全量数据
由于HBase没有offSet,拉取数据方法如下:
public interface Comparator {
void compare(Multimap