理解好Put、Cell,Result等的概念
package com.atguigu.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HbaseUtils {
public static Connection connection;
static {
//获取配置文件对象
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop202");
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断表是否存在
*/
public static Boolean isTableExist(String tableName) throws IOException {
//获取admin对象
Admin admin = connection.getAdmin();
//调用admin的tableExists的判断方法
boolean result = admin.tableExists(TableName.valueOf(tableName));
admin.close();
return result;
}
/**
* 创建表
* @param tableName
* @param columnFamily
* @param columnFamilies
* @throws IOException
*/
public static void createTable(String tableName, String columnFamily, String... columnFamilies) throws IOException {
if (isTableExist(tableName)) {
System.out.println("表已经存在");
} else {
// 创建admin对象
Admin admin = connection.getAdmin();
// 创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 创建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily);
hTableDescriptor.addFamily(hColumnDescriptor);
for (String family : columnFamilies) {
//循环创建列族描述器
HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor(family);
//循环将列族描述器添加到表描述器
hTableDescriptor.addFamily(hColumnDescriptor1);
}
// 创建表
admin.createTable(hTableDescriptor);
admin.close();
System.out.println("表创建成功");
}
}
/**
* 修改表的某个列族的版本号
*/
public static void modifyVersions(String tableName, String columnFamily, int versions) throws IOException {
if (isTableExist(tableName)) {
//创建admin对象
Admin admin = connection.getAdmin();
// 创建列族描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(tableName);
// 版本个数是以列族为单位设置 ,所以此时可以通过列族描述器来设置版本数
HColumnDescriptor hColumnDescriptor1 = hColumnDescriptor.setMaxVersions(versions);
// 通过列描述器设置最大版本
admin.modifyColumn(TableName.valueOf(tableName), hColumnDescriptor1);
admin.close();
} else {
System.out.println("表不存在");
}
}
/**
* 删除表
*/
public static void deleteTable(String tableName) throws IOException {
if (isTableExist(tableName)) {
Admin admin = connection.getAdmin();
admin.deleteTable(TableName.valueOf(tableName));
} else {
System.out.println(tableName + "不存在");
}
}
/**
* 插入某一行的某一列数据
*
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @param value
*/
public static void putCell(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
if (isTableExist(tableName)) {
//创建表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
// 给put对象添加值
Put add = put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
// 将put对象传给table
table.put(add);
} else {
System.out.println(tableName + "表不存在");
}
}
/**
* @param tableName
* @param rowkey
* @throws IOException
*/
public static void getRow(String tableName, String rowkey) throws IOException {
if (isTableExist(tableName)) {
Table table = connection.getTable(TableName.valueOf(tableName));
//创建get对象来获取一行数据
Get get = new Get(Bytes.toBytes(rowkey));
// 通过table的get方法获取一个结果集、
Result result = table.get(get);
// result里面存放的是这行数据的所有cell,可以通过rawCells方法来获取cells
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] rowKeyByte = CellUtil.cloneRow(cell);
byte[] familyByte = CellUtil.cloneFamily(cell);
byte[] columnByte = CellUtil.cloneQualifier(cell);
byte[] valueByte = CellUtil.cloneValue(cell);
System.out.println(
Bytes.toString(rowKeyByte) + "-" +
Bytes.toString(familyByte) + ":" +
Bytes.toString(columnByte) + "-" +
Bytes.toString(valueByte));
}
table.close();
} else {
System.out.println(tableName + "表不存在");
}
}
//获取所有数据
public static void getRows(String tableName) throws IOException {
if (isTableExist(tableName)){
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建Scan对象,通过Scan对象获取所有数据
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] rowKeyByte = CellUtil.cloneRow(cell);
byte[] familyByte = CellUtil.cloneFamily(cell);
byte[] columnByte = CellUtil.cloneQualifier(cell);
byte[] valueByte = CellUtil.cloneValue(cell);
System.out.println(
Bytes.toString(rowKeyByte) + "_" +
Bytes.toString(familyByte) + ":" +
Bytes.toString(columnByte) + "_" +
Bytes.toString(valueByte));
}
}
}else {
System.out.println(tableName + "表不存在");
}
}
/**
* 通过过滤器获取数据
* 已知某个列的列族名、列名、值,对应的一整行
* 因为通过过滤器可能匹配到多行数据,所以还是使用scan对象
*
* @param tableName
* @param columnFamily
* @param column
* @param value
* @throws IOException
*/
public static void getRowsByFilter(String tableName, String columnFamily, String column, String value) throws IOException {
if (isTableExist(tableName)) {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//创建一个过滤器
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
Bytes.toBytes(columnFamily),
Bytes.toBytes(column),
CompareFilter.CompareOp.EQUAL,
Bytes.toBytes(value)
);
//设置过滤器
scan.setFilter(singleColumnValueFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
//result里存放的是这行数据的所有cell,可以通过rawCells方法获取cells
Cell[] cells = result.rawCells();
//cells是所有cell的集合,可以遍历它获取每一个cell
for (Cell cell : cells){
//cell.getQualifierArray()等getXXX方法获取的是这一行所有的数据而不是方法名对应的数据
// byte[] qualifierArray = cell.getQualifierArray();
// System.out.println(Bytes.toString(qualifierArray));
//通过HBase自带的CellUtil获取cell的所有属性
byte[] rowkeyByte = CellUtil.cloneRow(cell);
byte[] familyByte = CellUtil.cloneFamily(cell);
byte[] columnByte = CellUtil.cloneQualifier(cell);
byte[] valueByte = CellUtil.cloneValue(cell);
//通过Bytes对象提供的toString方法将字节数组转换成字符串进行打印
System.out.println(
Bytes.toString(rowkeyByte) + "-" +
Bytes.toString(familyByte) + ":" +
Bytes.toString(columnByte) + "-" +
Bytes.toString(valueByte)
);
}
}
table.close();
} else {
System.out.println(tableName + "表不存在!");
}
}
/**\
*删除一整行
* @param tableName
* @param rowKey
*/
public static void deleteRow(String tableName, String rowKey) throws IOException {
if (isTableExist(tableName)) {
Table table = connection.getTable(TableName.valueOf(tableName));
// 获取delete对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
table.close();
} else {
System.out.println(tableName + "表不存在");
}
}
/**
*删除一行的某一个列
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
*/
public static void deleteColumn(String tableName,String rowKey,String columnFamily,String column) throws IOException {
if (isTableExist(tableName)) {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
//不加s删除最新版本
delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
//加s表示删除所有版本
//delete.addColumns()
table.delete(delete);
}else{
System.out.println(tableName + "表不存在");
}
}
}