public static Admin admin;
public static Configuration configuration;
public static Connection connection;
初始化
/**
* 建立链接
*/
public static void init() {
configuration = HBaseConfiguration.create();
// 设置hbase的存储路径
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
// 获取连接
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
创建表
/**
* 创建表,如果表明已经存在,先删除原来的表,再创建新的表
* @param tableName 表名
* @param fields 列族
*/
public static void createTable(String tableName, String[] fields) throws IOException {
TableName tableName1 = TableName.valueOf(tableName);
// 首先判断表是否存在,如果存在,先将其删除
if (admin.tableExists(tableName1)) {
admin.disableTable(tableName1);
admin.deleteTable(tableName1);
System.out.println(tableName + "表已经存在,现已删除,重新建立ing");
}
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName1);
for (String col : fields) {
ColumnFamilyDescriptor columnFamily = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(col)).build();
tableDescriptorBuilder.setColumnFamily(columnFamily);
}
admin.createTable(tableDescriptorBuilder.build());
}
}
插入值
/**
* 向表中插入值
* @param tableName 表名
* @param row 行健
* @param fields 列族列表(如果列族下面还有列限定符的,列表中的元素会以“columnFamily:column”表示)
* @param values 单元值
* @throws IOException
*/
public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(row.getBytes());
int length = fields.length;
for (int i = 0; i < length; i++){
String col = fields[i];
// 判断列族是否有列限定符
String[] split = col.split(":");
if (split.length == 1) {
put.addColumn(split[0].getBytes(), "".getBytes(), values[i].getBytes());
} else {
put.addColumn(split[0].getBytes(), split[1].getBytes(), values[i].getBytes());
}
table.put(put);
}
// 最后要关闭表的链接
table.close();
}
浏览表中某一列的数据
/**
* 浏览表中某一列的数据。
* 如果该列族有若干列限定符,就列出每个列限定符代表的列的数据;
* 如果列名以“columnFamily:column”形式给出,只需列出该列的数据。
* @param tableName 表名
* @param column 列名
*/
public static void scanColumn(String tableName, String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
String[] split = column.split(":");
if (split.length == 1) {
ResultScanner scanner = table.getScanner(column.getBytes());
for (Result result : scanner) {
// 获取所有的列限定符
Map<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes(column));
ArrayList<String> cols = new ArrayList<String>();
for(Map.Entry<byte[], byte[]> entry:familyMap.entrySet()){
cols.add(Bytes.toString(entry.getKey()));
}
for (String str : cols) {
System.out.print(str + ":" +new String(result.getValue(column.getBytes(), str.getBytes())) + " | ");
}
System.out.println();
}
// 释放扫描器
scanner.close();
} else {
ResultScanner scanner = table.getScanner(split[0].getBytes(), split[1].getBytes());
for (Result result : scanner) {
System.out.println(new String(result.getValue(split[0].getBytes(), split[1].getBytes())));
}
// 释放扫描器
scanner.close();
}
table.close();
}
修改表中的某一单元格中的数据
/**
* 修改制定单元格中的数据
* @param tableName 表名
* @param row 行健
* @param column 列名
*/
public static void modifyData(String tableName, String row, String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scanner scanner = new Scanner(System.in);
String values = null;
System.out.print("请输入要更改的值:");
values = scanner.next();
Put put = new Put(row.getBytes());
// 判断一下是否是某列族下的某列限定符
String[] split = column.split(":");
if (split.length == 1) {
put.addColumn(column.getBytes(), "".getBytes(), values.getBytes());
} else {
put.addColumn(split[0].getBytes(), split[1].getBytes(), values.getBytes());
}
table.put(put);
table.close();
}
删除某一行
/**
* 删除某一行
* @param tableName 表名
* @param row 行健
* @throws IOException
*/
public static void deleteRow(String tableName, String row) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(row.getBytes());
table.delete(delete);
table.close();
}