大数据之API操作HBase

2020-12-30 13:31

阅读:745

标签:lis   ado   efi   lte   scanner   ase   class   scan   查询   

package com.sjw.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBaseTest {

/*
4. 查看user_info表结构(若无user_info表先创建user_info表,包含info、data两个列族)。
5. 向user_info表中插入如下信息:
row key为rk0001,info:name值为zhangsan,info:age值为18;data:math值为80,data:computer值为90
row key为rk0002,info:name值为lisi,info:age值为19;data:math值为70,data:computer值为90
6. 查询user_info表中行键为rk0002的记录。
7. 查询user_info表中info:name为zhangsan的记录。
8. 查询user_info表中info:age大于等于18的记录。
9. 删除user_info中行键为rk0001的记录。*/

private static Connection connection = null;

static {

Configuration configuration = HBaseConfiguration.create();

configuration.set("hbase.zookeeper.quorum","sunjunwei1.com,sunjunwei2.com,sunjunwei3.com");
configuration.set("hbase.zookeeper.property.client","2181");

try {
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}

//删除表
private static void deleteTable(String tableName) throws IOException {
Admin admin = connection.getAdmin();

try {
if(!admin.tableExists(TableName.valueOf(tableName))){
return;
}
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
}finally {
admin.close();
}
}

//建表
private static void createTable(String tableName,String... family) throws IOException {
Admin admin = connection.getAdmin();

try {
if(admin.tableExists(TableName.valueOf(tableName))){
return;
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

for (String s : family) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(s);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}finally {
admin.close();
}
}

//插入数据
private static void putCells(String tableName,String rowkey,String family,String column,String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Admin admin = connection.getAdmin();
try {
if(!admin.tableExists(TableName.valueOf(tableName))){
return;
}
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
table.put(put);
}finally {
table.close();
}
}

//查询数据
private static void getCell(String tableName,String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
byte[] columnBytes = CellUtil.cloneQualifier(cell);
String columnStr = Bytes.toString(columnBytes);
//获取值
String valueStr = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println(columnStr + " : " + valueStr);
}

}finally {
table.close();
}
}

//过滤 查询user_info表中info:name为zhangsan的记录。
private static void filterColumnValue(String tableName,String family,String column,String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Scan scan = new Scan(Bytes.toBytes(family));
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(column), CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(value));

scan.setFilter(filter);

ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
String rowStr = Bytes.toString(CellUtil.cloneRow(cell));
String familyStr = Bytes.toString(CellUtil.cloneFamily(cell));
String columnStr = Bytes.toString(CellUtil.cloneQualifier(cell));
String valueStr = Bytes.toString(CellUtil.cloneValue(cell));

System.out.println(rowStr + " : " + familyStr + " : " + columnStr + " : " + valueStr);
}
}
}finally {
table.close();
}
}

//删除某一行记录
private static void deleteRow(String tableName,String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Delete delete = new Delete(Bytes.toBytes(rowkey));

table.delete(delete);
}finally {
table.close();
}
}


public static void main(String[] args) throws IOException {
//删除表
//deleteTable("user_info");
//建表
//createTable("user_info","info","data");
//插入数据
/* row key为rk0001,info:name值为zhangsan,info:age值为18;data:math值为80,data:computer值为90
row key为rk0002,info:name值为lisi,info:age值为19;data:math值为70,data:computer值为90*/
/*putCells("user_info","rk0001","info","name","zhangsan");
putCells("user_info","rk0001","info","age","18");
putCells("user_info","rk0001","data","math","80");
putCells("user_info","rk0001","data","computer","90");
putCells("user_info","rk0002","info","name","lisi");
putCells("user_info","rk0002","info","age","19");
putCells("user_info","rk0002","data","math","70");
putCells("user_info","rk0002","data","computer","90");
*/
//根据rowkey获取数据
//getCell("user_info","rk0002");

//查询user_info表中info:name为zhangsan的记录。
//filterColumnValue("user_info","info","age","18");

//删除user_info中行键为rk0001的记录
deleteRow("user_info","rk0001");


}

/*
4. 查看user_info表结构(若无user_info表先创建user_info表,包含info、data两个列族)。
5. 向user_info表中插入如下信息:
row key为rk0001,info:name值为zhangsan,info:age值为18;data:math值为80,data:computer值为90
row key为rk0002,info:name值为lisi,info:age值为19;data:math值为70,data:computer值为90
6. 查询user_info表中行键为rk0002的记录。
7. 查询user_info表中info:name为zhangsan的记录。
8. 查询user_info表中info:age大于等于18的记录。
9. 删除user_info中行键为rk0001的记录。*/
}

大数据之API操作HBase

标签:lis   ado   efi   lte   scanner   ase   class   scan   查询   

原文地址:https://www.cnblogs.com/whyuan/p/13276826.html


评论


亲,登录后才可以留言!