文章目录
- FusionInsight平台——HBase客户端工具封装EveHBase
FusionInsight平台——HBase客户端工具封装EveHBase
简介
- 针对华为HBase封装的客户端EveHBase
- 支持普通客户端、安全模式客户端,
- 功能包括:建表、预分区、建索引、异步请求、Put、Get、Scan、结果自动解析为JavaBean等
项目地址
- GitHub: https://github.com/AlionSSS/EveHBase
- 下载: EveHBase.jar
使用示例
Configuration hbaseConf = HBaseConfiguration.create();
hbaseConf.addResource(new Path("./conf/core-site.xml"));
hbaseConf.addResource(new Path("./conf/hdfs-site.xml"));
hbaseConf.addResource(new Path("./conf/hbase-site.xml"));
HBaseClient client = new EveHBase.Builder()
.config(hbaseConf)
.build();
SecurityConf securityConf = new SecurityConf(
"test",
"./kerberos/user.keytab",
"./kerberos/krb5.conf");
HBaseClient client = new EveHBase.Builder()
.config(hbaseConf)
.enableSafeSupport(securityConf)
.build();
ExecutorServiceAdapter adapter = new ExecutorServiceAdapter() {
@Override
public ExecutorService generateExecutorService() {
return Executors.newFixedThreadPool(4);
}
};
HBaseClient client = new EveHBase.Builder()
.config(hbaseConf)
.pool(adapter)
.build();
EveTable eveTable = new EveTable.Builder()
.table("tb_test")
.familys("family01")
.encoding(DataBlockEncoding.FAST_DIFF)
.compression(Compression.Algorithm.SNAPPY)
.build();
client.create(eveTable);
client.disableAndDelete("tableName");
String[] splitKeys = {"aaa", "ddd", "hhh", "vvv"};
client.multiSplit("tb_test", splitKeys);
client.createIndex(
"tb_test",
"family01",
"phone",
"tb_test_phone_idx");
public class Person {
@RowKey
public String rowkey;
@ColumnFamily
public Info f;
public class Info {
public String address;
public String phone;
public String name;
public String age;
}
}
String[] qualifiers = {"address", "phone", "name", "age", "lac"};
EveGet eveGet = new EveGet.Builder()
.table("tb_test")
.addRowkey("ccc_21312312")
.addRowkey("hhh_24242348")
.select("f", qualifiers)
.build();
try {
List<Person> personList = client.get(eveGet, Person.class);
System.out.println("person = " + personList);
} catch (IOException e) {
e.printStackTrace();
}
client.getAsync(eveGet, new ResultCallback<Person>() {
@Override
public void onSuccessful(List<Person> results) {
for (Person result : results) {
System.out.println("result = " + result);
}
}
@Override
public void onFailed(Exception e) {
System.out.println("e = " + e);
e.printStackTrace();
}
});
- 如果不需要自动解析JavaBean, 那么可以这样做
List<Result> personList = client.get(eveGet, Result.class);
String[] qualifiers = {"address", "phone", "name", "age", "lac"};
EveScan eveScan = new EveScan.Builder()
.table("tb_test")
.startRow("aaa")
.endRow("hhh")
.select("f", qualifiers)
.build();
try {
List<Person> personList = client.scan(eveScan, Person.class);
System.out.println("personList = " + personList);
} catch (IOException e) {
e.printStackTrace();
}
client.scanAsync(eveScan, new ResultCallback<Person>() {
@Override
public void onSuccessful(List<Person> results) {
for (Person result : results) {
System.out.println("result = " + result);
}
}
@Override
public void onFailed(Exception e) {
}
});
List<Put> putList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Put put = new Put(Bytes.toBytes("ggg_132131" + i));
put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("phone"), Bytes.toBytes("1891234567"+ i));
putList.add(put);
}
try {
client.put("tb_test", putList);
} catch (IOException e) {
e.printStackTrace();
}
PutBuffer buffer = client.createPutBuffer("test_table", 1000, 5000);
for (int i = 0; i < 100_000; i++) {
Put put = new Put(Bytes.toBytes("ggg_132131" + i));
put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("phone"), Bytes.toBytes("1891234567" + i));
buffer.put(put);
}
buffer.flush();
client.putAsync("tb_test", putList, new PutCallback() {
@Override
public void onSuccessful() {
System.out.println("GetTest.onSuccessful 提交Put成功!");
}
@Override
public void onFailed(Exception e, List<Put> puts) {
System.out.println("GetTest.onFailed 提交Put失败!");
System.out.println("失败的 puts = " + puts);
}
});
client.putAsync("tb_test", putList, null);
LICENSE
Copyright 2019 ALion
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.