How to use Hbase
This article mainly introduces how to use Hbase, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
Compare with MySQL:
1 To create a table, you only need to specify the famly column family. You do not need to specify specific columns and types.
@PostConstruct
public boolean createTable() {
log.info("create table start");
TableName tableName = TableName.valueOf(this.getTableName());
try {
Admin admin = connection.getAdmin();
if (! admin.tableExists(tableName)) {
log.info(tableName.toString() + "is not exist,create it");
HTableDescriptor tdesc = new HTableDescriptor(tableName);
HColumnDescriptor colDesc = new HColumnDescriptor(FAMILY);
tdesc.addFamily(colDesc);
admin.createTable(tdesc);
admin.close();
}
log.info("create table end");
return true;
} catch ( IOException e) {
log.error("create table error {} {}", tableName, e.getLocalizedMessage());
return false;
}
}
2 Storage data format HBASE value All byte[] byte data format storage
Advantages: 1 No need to process data types in advance unified use Bytes.toByte() so storage speed is higher
2 Compared with the original data type, serialized byte[] form storage can save bytes
3 can be based on rowKey range search, this need to rowKey design properly.
@Override
public List getByRange(String start, String end) {
try {
Table table = connection.getTable(TableName.valueOf(getTableName()));
Scan scan = new Scan();
scan.withStartRow(start.getBytes(), true)
.withStopRow(end.getBytes(), true);
ResultScanner scanner = table.getScanner(scan);
List list = new ArrayList();
for (Result result : scanner) {
list.add(getObj(result));
}
return list;
} catch (Exception e) {
log.error("HBase failed to get data in bulk", e);
}
return Collections.emptyList();}
Thank you for reading this article carefully. I hope that the article "How to Use Hbase" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!