<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
<version>2.1.5</version>
</dependency>
public class HbaseApi {
private static Connection conn;
private static Admin admin;
public static void init(String quorum, String clientPort) throws IOException {
Configuration config = HBaseConfiguration.create();
System.getProperties().setProperty("HADOOP_USER_NAME", "hadoop");
config.set("hbase.zookeeper.quorum", quorum);
config.set("hbase.zookeeper.clientPort", clientPort);
config.set("hbase.client.operation.timeout", "60000");
config.set("hbase.rpc.timeout", "60000");
conn = ConnectionFactory.createConnection(config);
admin = conn.getAdmin();
}
// *********************** 3. 获取一条数据 *******************************
public static void getRow(String tableName, String rowKey) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey)); // 通过rowkey创建一个 get 对象
Result result = table.get(get); // 输出结果
for (Cell cell : result.rawCells()) {
System.out.println(
"\u884c\u952e:" + new String(CellUtil.cloneRow(cell)) + "\t" +
"\u5217\u65cf:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
"\u5217\u540d:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
"\u503c:" + new String(CellUtil.cloneValue(cell)) + "\t" +
"\u65f6\u95f4\u6233:" + cell.getTimestamp());
}
table.close();
conn.close();
}
//**************************** 2. 添加一条数据 *******************************
public static void addRow(String tableName, String rowKey, String columnFamily,
String column, String value) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey)); // 通过rowkey创建一个 put 对象
// 在 put 对象中设置 列族、列、值
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put); // 插入数据,可通过 put(List<Put>) 批量插入
table.close();
conn.close();
}
// *************************** 4. 全表扫描 ****************************
public static void scanTable(String tableName) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Scan scan = new Scan(); // 创建扫描对象
ResultScanner results = table.getScanner(scan); // 全表的输出结果
for (Result result : results) {
for (Cell cell : result.rawCells()) {
System.out.println(
"\u884c\u952e:" + new String(CellUtil.cloneRow(cell)) + "\t" +
"\u5217\u65cf:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
"\u5217\u540d:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
"\u503c:" + new String(CellUtil.cloneValue(cell)) + "\t" +
"\u65f6\u95f4\u6233:" + cell.getTimestamp());
}
}
results.close();
table.close();
conn.close();
}
//**************************** 5. 删除一条数据 *****************************
public static void delRow(String tableName, String rowKey) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
table.close();
conn.close();
}
//************************ 6. 删除多条数据 *****************************
public static void delRows(String tableName, String[] rows) throws IOException {
HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
List<Delete> list = new ArrayList<Delete>();
for (String row : rows) {
Delete delete = new Delete(Bytes.toBytes(row));
list.add(delete);
}
table.delete(list);
table.close();
conn.close();
}
//获取列簇信息
public static List<String> getColumnFamilyList(String tableName) throws Exception {
Table table = conn.getTable(TableName.valueOf(tableName));
List<String> columnFamilyList = new ArrayList<>();
HTableDescriptor hTableDescriptor = table.getTableDescriptor();
for (HColumnDescriptor descriptor : hTableDescriptor.getColumnFamilies()) {
String name = descriptor.getNameAsString();
columnFamilyList.add(descriptor.getNameAsString());
}
System.out.println("--------------" + tableName + ":ColumnFamilyInfo---------------");
columnFamilyList.forEach(s -> System.out.println(s));
return columnFamilyList;
}
//判断列簇是否存在
public static boolean isExistColumnFamily(String tableName, String cf) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
TableDescriptor tableDescriptor = table.getDescriptor();
ColumnFamilyDescriptor descriptor = tableDescriptor.getColumnFamily(Bytes.toBytes(cf));
return descriptor == null ? false : true;
}
//动态新增列簇
public static synchronized void insertColumnFamilyList(String tableName, List<String> columnFamilyList) throws Exception {
if (columnFamilyList == null || columnFamilyList.size() <= 0) {
return;
}
TableName table = TableName.valueOf(tableName);
admin.disableTable(table);
columnFamilyList.forEach(c -> {
try {
admin.addColumn(table, new HColumnDescriptor(c));
} catch (IOException e) {
e.printStackTrace();
}
});
admin.enableTable(table);
}
}