Hbase练习-常用API使用
程序员文章站
2024-03-12 17:30:26
...
package csdn.dreamzuora;
import com.sun.istack.internal.logging.Logger;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Title:
* Description:
*
* @version 1.0
* @author: weijie
* @date: 2020/11/10 18:02
*/
public class HbaseUtils {
public static Configuration conf;
private Logger logger = Logger.getLogger(HbaseUtils.class);
static{
//使用 HBaseConfiguration 的单例方法实例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "39.96.204.209");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
public Connection getConnection() throws IOException {
return ConnectionFactory.createConnection(conf);
}
public Admin getAdmin() throws IOException {
Connection connection = getConnection();
return connection.getAdmin();
}
public Table getTable(TableName tableName) throws IOException {
Connection connection = getConnection();
return connection.getTable(tableName);
}
/**
* 判断表是否存在
* @param tableName
* @return
* @throws IOException
*/
public boolean isTableExist(String tableName) throws IOException {
Admin admin = getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}
/**
* 创建表
* @param tableName
* @param columnFamily
* @throws IOException
*/
public void createTable(String tableName, String... columnFamily) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
//判断表是否存在
if (isTableExist(tableName)){
logger.info("表" + tableName + "已存在");
}else {
//创建表属性对象,表名需要转字节
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//创建多个列族
for (String cf : columnFamily){
hTableDescriptor.addFamily(new HColumnDescriptor(cf));
}
//根据对表的配置,创建表
admin.createTable(hTableDescriptor);
logger.info("表" + tableName + "创建成功!");
}
}
/**
* 删除表
*/
public void dropTable(String tableName) throws IOException {
Admin admin = getAdmin();
if (isTableExist(tableName)){
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
logger.info("表" + tableName + "删除成功!");
}else {
logger.info(tableName + "表不存在");
}
}
/**
* 向表中插入数据
*/
public void addRowData(String tableName, String rowkey, String columnFamily, String column, String value) throws IOException {
//创建表对象
Table table = getTable(TableName.valueOf(tableName));
//向表中插入数据
Put put = new Put(Bytes.toBytes(rowkey));
//向Put对象中组装数据
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
logger.info("数据插入成功");
}
/**
* 删除多行数据
*/
public void deleteMultiRow(String tableName, String... rows) throws IOException {
Table table = getTable(TableName.valueOf(tableName));
List<Delete> deleteList = new ArrayList<Delete>();
for (String row : rows){
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
table.delete(deleteList);
table.close();
}
/**
* 获取所有数据
*/
public void getAllRows(String tableName) throws IOException {
Table table = getTable(TableName.valueOf(tableName));
//得到用于扫描region的对象
Scan scan = new Scan();
//使用Htable得到resultcanner实现类的对象
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner){
Cell[] cells = result.rawCells();
for (Cell cell : cells){
//得到rowkey
logger.info("行健: " + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
//列名
logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
//列值
logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
/**
* 获取某一行数据
*/
public void getRow(String tableName, String rowkey) throws IOException {
Table table = getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
/**
* get.setMaxVersions() 显示所有版本
* get.setTimeStamp(timeStamp) 显示指定时间戳版本
*/
Result result = table.get(get);
for (Cell cell : result.rawCells()){
logger.info("行键: " + Bytes.toString(result.getRow()));
//得到列族
logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
//列名
logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
//列值
logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
/**
* 获取某一行指定"列族:列"的数据
*/
public void getRowQualifier(String tableName, String rowkey, String family, String qualifier) throws IOException {
Table table = getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
for (Cell cell : result.rawCells()){
logger.info("行键: " + Bytes.toString(result.getRow()));
//得到列族
logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
//列名
logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
//列值
logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}