Hbase术语介绍
在Hbase中,有一些术语需要提前了解。如下:
Table:
Hbase的table由多个行组成
Row:
一个行在Hbase中由一个或多个有值的列组成。Row按照字母进行排序,因此行健的设计非常重要。
RowKey:
行键,可理解成MySQL中的主键列。
Column:
列,可理解成MySQL列。
Column Family:
列簇在物理上包含了许多的列与列的值。可以理解成MySQL的垂直分区(将一张宽表,切分成几张不那么宽的表)。此机制引入的原因,是因为HBase相信,查询可能并不需要将一整行的所有列数据全部返回。(就像我们往往在写SQL时不太会写select all一样)
Column Qualifier:
列簇的限定词,理解为列的唯一标识。但是列标识是可以改变的,因此每一行可能有不同的列标识。
Cell:
Cell是由row,column family,column qualifier包含时间戳与值组成的,一般表达某个值的版本
Timestamp:
时间戳一般写在value的旁边,代表某个值的版本号,默认的时间戳是你写入数据的那一刻,但是你也可以在写入数据的时候指定不同的时间戳。
我们这里假设有一张这种表,来表示上图的关系
Hbase查改
1、maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
2、hbase config配置文件
@Configuration
@Component
public class HbaseConfig {
@Value("${hbase.zookeeper.quorum}")
private String hbaseZookeeperQuorum;
@Value("${hbase.zookeeper.clientPort}")
private String hbaseZookeeperClientPort;
@Bean(name = "hbaseConnection")
public Connection hbaseConnection() throws IOException {
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorm", hbaseZookeeperQuorum);
conf.set("hbase.zookeeper.property.clientPort", hbaseZookeeperClientPort);
//System.setProperties("HADOOP_USER_NAME", "root");
return ConnectionFactory.createConnection(conf);
}
}
3、hbase查改接口
public interface HbaseDao {
/**
* 根据表名、rowKey、ColumnFamily、Column 获取到值
* @param hbaseVO 包含rowKey、ColumnFamily、Column的数据
* @return 对应获取到值
* @throws IOException io异常
*/
public String getValue(HbaseVO hbaseVO) throws IOException;
/**
* 根据表名、rowKey 获取到值
* @param tableName 表名的数据
* @param rowKeyList rowKeylist对象
* @return 对应获取到值
* @throws IOException io异常
*/
public List<HbaseResultVO> getListValue(String tableName, List<String> rowKeyList) throws IOException;
/**
* 根据表名、rowKey 获取到值
* @param list ColumnFamily、Column的数据
* @return 对应获取到值
* @throws IOException io异常
*/
public void batchPutValue(List<HbaseVO> list) throws IOException;
}
4、具体实现类
@Component
public class HbaseDaoImpl implements HbaseDao {
@Resource
private Connection hbaseConnection;
/**
* 根据表名、rowKey、ColumnFamily、Column 获取到值
* @param hbaseVO 包含rowKey、ColumnFamily、Column的数据
* @return 对应获取到值
* @throws IOException io异常
*/
@Override
public String getValue(HbaseVO hbaseVO) throws IOException {
Table table = hbaseConnection.getTable(TableName.valueOf(hbaseVO.getTableName()));
Get get = new Get(hbaseVO.getRowKey().getBytes());
//查询结果
byte[] value = table.get(get).getValue(hbaseVO.getColumnFamily().getBytes(), hbaseVO.getColumnName().getBytes());
return Bytes.toString(value);
}
/**
* 根据表名、rowKey 获取到值
* @param tableName 表名的数据
* @param rowKeyList rowKeylist对象
* @return 对应获取到值
* @throws IOException io异常
*/
@Override
public List<HbaseResultVO> getListValue(String tableName, List<String> rowKeyList) throws IOException {
Table table = hbaseConnection.getTable(TableName.valueOf(tableName));
List<Get> getList = new ArrayList<>(rowKeyList.size());
rowKeyList.stream().forEach(rowkey->{
getList.add(new Get(rowkey.getBytes()));
});
Result[] results = table.get(getList);
List<HbaseResultVO> resultList = new ArrayList<>();
for (Result result : results) {
if (StringUtils.isEmpty(Bytes.toString(result.getRow()))) {
continue;
}
HbaseResultVO hbaseResultVO = new HbaseResultVO();
hbaseResultVO.setImei(Bytes.toString(result.getValue(HbaseSelectCommon.COLUMN_FAMILY.getBytes(), HbaseSelectCommon.COLUMN_IMEI.getBytes())));
hbaseResultVO.setModel(Bytes.toString(result.getValue(HbaseSelectCommon.COLUMN_FAMILY.getBytes(), HbaseSelectCommon.COLUMN_MODEL.getBytes())));
resultList.add(hbaseResultVO);
}
return resultList;
}
/**
* 根据表名、rowKey 获取到值
* @param list ColumnFamily、Column的数据
* @return 对应获取到值
* @throws IOException io异常
*/
@Override
public void batchPutValue(List<HbaseVO> list) throws IOException {
Table table = hbaseConnection.getTable(TableName.valueOf(list.get(0).getTableName()));
ArrayList<Put> putList = new ArrayList<>(list.size());
for (HbaseVO hbaseVO : list) {
Put put = new Put(hbaseVO.getRowKey().getBytes());
for (Column column : hbaseVO.getColumnList()) {
put.addColumn(hbaseVO.getColumnFamily().getBytes(), column.getColumnName().getBytes(), column.getColumnValue().getBytes());
}
putList.add(put);
}
table.put(putList);
}
}
附上使用对象:
@Data
public class HbaseVO {
private String tableName;
private String rowKey;
private String columnFamily;
private String columnName;
private List<Column> columnList;
}
public final class HbaseSelectCommon {
public final static String COLUMN_FAMILY = "CF1";
public final static String COLUMN_IMEI = "imei";
public final static String COLUMN_MODEL= "model";
}
@Data
public class Column {
private String columnName;
private String columnValue;
}
@Data
public class HbaseResultVO {
private String imei;
private String model;
}