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;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.apache.hadoop.hbase.TableName.*;
public class HBaseAPITest {
//获取Configuration
public static Configuration conf;
public static Connection connection;
public static String tableName;
public static String[] cfs;
public static Admin admin;
public static String rowkey;
public static String[] rows;
static {
try {
//使用HBaseConfiguration的单例方法实例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.91.132");//设置Zookeeper
conf.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
//判断表是否存在
public static boolean isTeableExists(String tableName) throws IOException {
//在Hbase中创建管理、访问表需要先创建HBaseAdmin对象
//HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);方法过时了,用connect.getAdmin()
admin = connection.getAdmin();
boolean tableExists = admin.tableExists(valueOf(tableName));
return tableExists;
}
//创建表
public static void createTable(String tableName, String... columnFamily) throws IOException {
admin = connection.getAdmin();
//首先判断表是否存在
if (isTeableExists(tableName)) {
System.out.println("table:" + tableName + " is exists");
} else {
//创建表属性对象,表名需要转字节
HTableDescriptor tableDescriptor = new HTableDescriptor(valueOf(tableName));
//创建多个列族
for (String cf : columnFamily) {
tableDescriptor.addFamily(new HColumnDescriptor(cf));
}
//根据对表的配置,创建表
admin.createTable(tableDescriptor);
System.out.println("table:" + tableName + " is created successful");
}
}
//判断表是否可用
public static Boolean isDisableTable(String tableName) throws IOException {
admin = connection.getAdmin();
boolean tableDisabled = admin.isTableDisabled(valueOf(tableName));
return tableDisabled;
}
//删除表
public static void deleteTable(String tableName) throws IOException {
admin = connection.getAdmin();
if (!isTeableExists(tableName)){
System.out.println("table:" + tableName + " is not exists");
return;
}
if (isTeableExists(tableName) && isDisableTable(tableName)) {
admin.deleteTable(TableName.valueOf(tableName));
} else if (isTeableExists(tableName) && !isDisableTable(tableName)) {
System.out.println("table: " + tableName + " is enabled,is not deleted");
admin.disableTable(TableName.valueOf(tableName));
}
}
//向表中插入数据:单行插入数据
public static void addRowData(String tableName, String rowKey,String columnFamily, String column, String value) throws IOException {
//创建HTable对象
Table table = connection.getTable(valueOf(tableName));
//创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
//给对象赋值
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
//插入数据
table.put(put);
//关闭连接
table.close();
System.out.println("插入数据成功");
}
//项表中插入数据:多行插入数据
public static void addRowDatas(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
//创建HTable
Table table = connection.getTable(valueOf(tableName));
//创建Put类型的list集合,用于向表中插入数据
ArrayList<Put> puts = new ArrayList<Put>();
for (int i = 0; i < 10; i++) {
rowKey = "rowkey"+ i;
//创建Put对象
Put put = new Put(Bytes.toBytes(rowKey));
//给对象赋值
put.addColumn(columnFamily.getBytes(),column.getBytes(),value.getBytes());
//向list集合中插入数据
puts.add(put);
}
//向表中插入数据
table.put(puts);
//关闭连接
table.close();
}
//删除多行数据
public static void deleteMutilRows(String tableName,String... rowkey) throws IOException {
//创建HTable
Table table = connection.getTable(valueOf(tableName));
ArrayList<Delete> deletes = new ArrayList<Delete>();
for (String row : rowkey) {
Delete delete = new Delete(Bytes.toBytes(row));
deletes.add(delete);
}
table.delete(deletes);
//关闭连接
table.close();
}
//获取所有行数据
public static void getAllRows(String tableName) throws IOException {
//创建Scan对象
Scan scan = new Scan();
//创建HTable
Table table = connection.getTable(valueOf(tableName));
//使用HTable获取resultScanner的实现类对象
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//获取rowKey
String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.println("RowKey:" + rowKey);
//获取CF
String CF = Bytes.toString(CellUtil.cloneFamily(cell));
System.out.println("ColumnFamily:" + CF);
//获取列
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
System.out.println("Column:" + column);
//获取值
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("Value:" + value);
}
}
}
//获取某一行数据
public static void getRow(String tableName, String rowKey) throws IOException {
//获取表
Table table = connection.getTable(valueOf(tableName));
//创建get对象,用于获取一行对象
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//获取rowKey
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.println("RowKey:" + rowkey);
//获取CF
String CF = Bytes.toString(CellUtil.cloneFamily(cell));
System.out.println("ColumnFamily:" + CF);
//获取列
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
System.out.println("Column:" + column);
//获取值
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("Value:" + value);
}
}
//获取某一指定"列族:列"的数据
public static void getRowQualifier(String tableName, String rowKey, String columnFamily, String qualifier) throws IOException {
//获取表
Table table = connection.getTable(valueOf(tableName));
//获取rowKey
Get get = new Get(Bytes.toBytes(rowKey));
//获取列族
get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualifier));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//获取rowKey
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.println("RowKey:" + rowkey);
//获取CF
String CF = Bytes.toString(CellUtil.cloneFamily(cell));
System.out.println("ColumnFamily:" + CF);
//获取列
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
System.out.println("Column:" + column);
//获取值
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("Value:" + value);
}
}
//关闭资源
public static void close(){
if (admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
tableName = "student";
//判断表是否存在
boolean tableIsExists = isTeableExists(tableName);
if (tableIsExists) {
System.out.println("table:" + tableName + " is exists!!");
} else {
System.out.println("table:" + tableName + " is not exists");
}
//创建表
tableName = "teachers";
cfs = new String[]{"CF", "CF2", "CF3", "CF4"};
createTable(tableName, cfs);
//判断表是否可用
tableName = "life";
Boolean disableTable = isDisableTable(tableName);
if (disableTable) {
System.out.println("table: " + tableName + " is disabled");
} else {
System.out.println("table: " + tableName + " is enabled");
}
//删除表
tableName = "teacher";
deleteTable(tableName);
//向表中插入数据
addRowData("student","1002","info","sex","Male");
//向表中批量插入数据
for (int i = 0; i < 4; i++) {
addRowDatas("teachers",rowkey,"CF","CF","Male"+ i);
}
//批量删除数据
rows = new String[10];
for (int i = 0; i < 10; i++) {
rows[i] = "rowkey" + i;
}
deleteMutilRows("teachers",rows[0],rows[1],rows[2],rows[3],rows[4],rows[5],rows[6],rows[7],rows[8],rows[9]);
//获取表中的所有数据
getAllRows("student");
System.out.println("====================================");
//获取某行数据
getRow("student","1002");
System.out.println("====================================");
//获取某一指定"列族:列"的数据
getRowQualifier("student","1001","info","name");
//关闭客户端和连接
close();
}
}
以上代码工大家参考,有问题欢迎指点,大家一起交流学习!!
|