import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lw on 2018/11/29.
*/
public class HBaseUtils {
private static Configuration conf;
static {
conf = HBaseConfiguration.create();
// 设置 zookeeper 集群的位置
// 配置 windows 的 hosts 文件,让 windows 也能识别 master, slave1, slave2
// hosts 文件位置:C:\Windows\System32\drivers\etc
// 拷贝到桌面,修改以后再覆盖回去
conf.set("hbase.zookeeper.quorum", "master:2181,slave1:2181,slave2:2181");
}
// 获取 HBase 的链接对象,接下来使用链接对象对 HBase 进行操作
private static Connection getConnection() throws IOException {
return ConnectionFactory.createConnection(conf);
}
// 创建表
// ... 表示可变参数,可以传入任意个字符串,填充到 familyNames 数组中
public static void createTable(String tableName, String ... familyNames) {
// 从 jdk7 开始,实现了 AutoCloseable 接口的 流 支持使用 try-with-resource 语法自动关闭
// 把创建流的语句移动到 try() 中,那么在 {} 结束后会自动关闭流
try (// 1. 获取 HBase 链接对象
Connection conn = getConnection();
// 2. 获取创建表的 admin 对象
Admin admin = conn.getAdmin();){
// 3. 转换 String 类型表名为 HBase 能够识别的 TableName 类型
TableName tn = TableName.valueOf(tableName);
// 4. 根据表名生成建表语句
HTableDescriptor tableDescriptor = new HTableDescriptor(tn);
// 5. 添加列族
for (String familyName : familyNames) {
// 6. 转换 String 类型列族名为 HBase 能够识别的 HColumnDescriptor
HColumnDescriptor columnDescriptor = new HColumnDescriptor(familyName);
// 7. 添加列族到建表语句
tableDescriptor.addFamily(columnDescriptor);
}
// 8. 通过建表语句创建表
admin.createTable(tableDescriptor);
} catch (IOException e) {
e.printStackTrace();
}
}
// 添加一条数据
public static void put(String tableName, String rowKey, String familyName, String qualifierName, String value) {
try(// 1. 获取 HBase 链接对象
Connection conn = getConnection();) {
// 2. 获取表
Table table = conn.getTable(TableName.valueOf(tableName));
// 3. 根据 rowKey 生成 put 语句
// 因为 HBase 只支持 byte[] 类型,所以提供了 Bytes 类实现各种类型和 byte[] 类型的相互转换
Put put = new Put(Bytes.toBytes(rowKey));
// 4. 添加列族,列名,值
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifierName), Bytes.toBytes(value));
// 5. 向表中添加数据
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取一条数据
public static Result get(String tableName, String rowKey, String familyName, String qualifierName) {
Result result = null;
try(// 1. 获取 HBase 链接对象
Connection conn = getConnection();) {
// 2. 获取表
Table table = conn.getTable(TableName.valueOf(tableName));
// 3. 根据 rowKey 生成 get 语句
Get get = new Get(Bytes.toBytes(rowKey));
// 4. 添加列族或列名
if (familyName != null && qualifierName != null) {
// 'info:name'
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifierName));
} else if (familyName != null) {
// 'info'
get.addFamily(Bytes.toBytes(familyName));
}
// 5. 获取查询结果
result = table.get(get);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
// 获取多条数据
public static List<Result> scan(String tableName, String startRow, String stopRow, Filter filter) {
ArrayList<Result> results = new ArrayList<>();
try(// 1. 获取 HBase 链接对象
Connection conn = getConnection();) {
// 2. 获取表
Table table = conn.getTable(TableName.valueOf(tableName));
// 3. 生成 scan 语句
Scan scan = new Scan();
// 4. 设置范围
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
// 5. 添加过滤器,设置查询条件
scan.setFilter(filter);
// 6. 执行 scan 语句,获取查询结果
try(ResultScanner scanner = table.getScanner(scan);) {
// 7. 遍历结果集获取每一个 result 对象,添加到 results 集合中
Result result = null;
while ((result = scanner.next()) != null) {
results.add(result);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return results;
}
public static void main(String[] args) {
// createTable("user", "info", "data");
// put("user", "rk001", "info", "name", "zhangsan");
// put("user", "rk001", "info", "age", "20");
// Result result = get("user", "rk001", "info", null);
//
// // 从 result 中获取想要的数据
// // 1. 获取所有单元格
// List<Cell> cells = result.listCells();
//
// // 2. 遍历获取每个单元格
// for (Cell cell : cells) {
//
// // HBase 提供了 CellUtils 类获取 cell 中数据(value,qualifier,family)
// String value = Bytes.toString(CellUtil.cloneValue(cell));
// String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
// String family = Bytes.toString(CellUtil.cloneFamily(cell));
//
// System.out.println(family + " " + qualifier + " " + value);
// }
// 前缀过滤器:对 rowKey 进行筛选,是否以指定的字符串开头
Filter prefixFilter = new PrefixFilter(Bytes.toBytes("rk"));
List<Result> results = scan("user", "rk000", "rk100", prefixFilter);
for (Result result : results) {
// 1. 获取所有单元格
List<Cell> cells = result.listCells();
// 2. 遍历获取每个单元格
for (Cell cell : cells) {
// HBase 提供了 CellUtils 类获取 cell 中数据(value,qualifier,family)
String value = Bytes.toString(CellUtil.cloneValue(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.println(rowKey + " " + family + " " + qualifier + " " + value);
}
}
}
}
Java中HBase的使用
最新推荐文章于 2024-03-12 17:08:44 发布