HBase API
(1)新建项目后在pom.xml中添加依赖:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
(2)获取Configuration对象
private static Connection connection = null;
static{
//创建配置文件
Configuration conf = HBaseConfiguration.create();
//设置zookeeper参数和主机名
conf.set("hbase.zookeeper.quorum","spark01,spark02,spark03");
//设置端口号
conf.set("hbase.zookeeper.property.clientPort","2181");
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
(2)创建表
public static void createTable(String tableName,String... families) throws Exception {
if(!isTableExist(tableName)){
Admin admin = connection.getAdmin();
//创建表的描述器
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
for (String family : families) {
// 创建列族描述
HColumnDescriptor familyDesc = new HColumnDescriptor(family);
desc.addFamily(familyDesc);
}
admin.createTable(desc);
admin.close();
}
}
(3)判断表是否存在
public static boolean isTableExist(String tableName) throws Exception {
HBaseAdmin admin = (HBaseAdmin)connection.getAdmin();
return admin.tableExists(tableName);
}
(4)删除表
public static boolean deleteTable(String tableName) throws Exception {
HBaseAdmin admin = (HBaseAdmin)connection.getAdmin();
try {
if(isTableExist(tableName)){
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
return true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
admin.close();
}
return false;
}
(5)添加数据
public static void put(String tableName,String rowKey,String family,String column,String value) throws Exception {
if(!isTableExist(tableName)){
return ;
}
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(family.getBytes(),column.getBytes(),value.getBytes());
table.put(put);
} catch (IOException e) {
e.printStackTrace();
} finally {
table.close();
}
}
(6)删除一列
public static void deleteRow(String tableName,String rowKey) throws Exception {
if(!isTableExist(tableName)){
return ;
}
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
} catch (Exception e) {
e.printStackTrace();
}finally {
table.close();
}
}
(7)删除指定列族
public static void deleteFamily(String tableName,String rowKey,String family) throws Exception {
if(!isTableExist(tableName)){
return ;
}
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addFamily(Bytes.toBytes(family));
table.delete(delete);
} catch (Exception e) {
e.printStackTrace();
}finally {
table.close();
}
}
(8)获取指定rowkey的数据
public static void getRow(String tableName,String rowKey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
Result result = table.get(get);
// 获取所有列数据
Cell[] cells = result.rawCells();
// 这里循环的是这个rowkey的所有列
for (Cell cell : cells) {
byte[] columns = CellUtil.cloneQualifier(cell);
String column = Bytes.toString(columns);
byte[] value = CellUtil.cloneValue(cell);
String valuestr = Bytes.toString(value);
System.out.println(column + ":" + valuestr);
}
}
(9)根据rowkey范围获取数据
public static void getRowByRange(String tableName,String startRow,String stopRow) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan(startRow.getBytes(), stopRow.getBytes());
ResultScanner scanner = table.getScanner(scan);
// 这里是循环的键
for (Result result : scanner) {
// 获取所有列数据
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] columns = CellUtil.cloneQualifier(cell);
String column = Bytes.toString(columns);
byte[] value = CellUtil.cloneValue(cell);
String valuestr = Bytes.toString(value);
String rowstr = Bytes.toString(CellUtil.cloneRow(cell));
System.out.println(rowstr + "-" +column + ":" + valuestr);
}
}
}
(10)获取所有表数据,就是把(9)的scan创建为空
(11)删除一个列的所有版本(删除一个键的一个列)
public static void deleteCell(String tableName,String rowKey,String family,String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
try {
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(family.getBytes(),column.getBytes());
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
}finally {
table.close();
}
}
(12):用一列的值决定这一行的数据是否被过滤
public static void getRowByFilter(String tableName,String rowKey,String family,String column,String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
SingleColumnValueFilter filter = new SingleColumnValueFilter(family.getBytes(), column.getBytes(), CompareFilter.CompareOp.EQUAL, value.getBytes());
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
// 获取所有列数据
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
byte[] columns = CellUtil.cloneQualifier(cell);
String columnB = Bytes.toString(columns);
byte[] valueB = CellUtil.cloneValue(cell);
String valuestr = Bytes.toString(valueB);
String rowstr = Bytes.toString(CellUtil.cloneRow(cell));
System.out.println(rowstr + "-" +columnB + ":" + valuestr);
}
}
}
496

被折叠的 条评论
为什么被折叠?



