介绍了单一curd和批量curd
1. 导入maven依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.0.1</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
2. 单一curd
public boolean setValue(String value,String colName,String rowKey){
TableName tableName1 = TableName.valueOf(this.tableName);
try {
Table table = hbaseConnection.getTable(tableName1);
Put row001 = new Put(Bytes.toBytes(rowKey));
row001.addColumn(Bytes.toBytes(familyName),
Bytes.toBytes(colName),Bytes.toBytes(value));
table.put(row001);
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public String getValue(String rowKey){
TableName tableName1 = TableName.valueOf(this.tableName);
try {
Table table = hbaseConnection.getTable(tableName1);
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
Cell[] cells = result.rawCells();
StringBuilder returnData = new StringBuilder();
for (Cell cell:cells){
byte[] bytes = CellUtil.cloneRow(cell);
String s = Bytes.toString(bytes);
logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 行键:"+s);
returnData.append(s).append('\n');
String s1 = Bytes.toString(CellUtil.cloneFamily(cell));
logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 列族:"+s1);
returnData.append(s1).append('\n');
String s2 = Bytes.toString(CellUtil.cloneQualifier(cell));
logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 列名:"+s2);
returnData.append(s2).append('\n');
String s3 = Bytes.toString(CellUtil.cloneValue(cell));
logger.info("查询hbase数据信息:目标表:"+tableName+",目标行键:"+rowKey+",查询出的信息: 值:"+s3);
returnData.append(s3).append('\n');
}
return returnData.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "查询失败";
}
3. 批量curd
public boolean setValues() throws IOException {
TableName tableName1 = TableName.valueOf(this.tableName);
try {
Put put = new Put(Bytes.toBytes("rowKey001"))
.addColumn(Bytes.toBytes(familyName),
Bytes.toBytes("colName01"),
Bytes.toBytes("value01"));
put.addColumn(Bytes.toBytes(familyName),
Bytes.toBytes("colName02"),
Bytes.toBytes("value02"));
Get get1 = new Get(Bytes.toBytes("rowKey002")).addFamily(Bytes.toBytes(familyName));
Get get2 = new Get(Bytes.toBytes("rowKey003")).addFamily(Bytes.toBytes(familyName));
Get get3 = new Get(Bytes.toBytes("rowKey004")).addFamily(Bytes.toBytes(familyName));
Delete delete = new Delete(Bytes.toBytes("rowKey005")).addFamily(Bytes.toBytes(familyName));
{
Table table = hbaseConnection.getTable(tableName1);
ArrayList<Put> puts = new ArrayList<>();
ArrayList<Get> gets = new ArrayList<>();
ArrayList<Delete> deletes = new ArrayList<>();
table.put(puts);
Result[] results = table.get(gets);
for (Result result : results) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] bytes = CellUtil.cloneValue(cell);
System.out.println("值:" + Bytes.toString(bytes));
}
}
table.delete(deletes);
}
{
BufferedMutator bufferedMutator = hbaseConnection.getBufferedMutator(tableName1);
BufferedMutator bufferedMutator2 = hbaseConnection.getBufferedMutator(new BufferedMutatorParams(tableName1).pool(null));
ArrayList<Mutation> mutations = new ArrayList<>();
mutations.add(put);
mutations.add(delete);
bufferedMutator.mutate(mutations);
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
项目源码gitee
https://gitee.com/shenshuxin01/first_-spring-boot_-demo/blob/master/HBase2Demo/src/main/java/ssx/demo/hbase2demo/service/SerDemo.java