1.引入依赖
<dependency>
<groupId>com.aliyun.hbase</groupId>
<artifactId>alihbase-client</artifactId>
<version>2.8.7</version>
<exclusions>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
2. 配置数据源
@Configuration
public class HbaseConfig {
private static final String HBASE_QUORUM = "hbase.zookeeper.quorum";
private static final String HBASE_USERNAME = "hbase.client.username";
private static final String HBASE_PASSWORD = "hbase.client.password";
@Value("${hbase.a.seedserver}")
private String seedServerDs0;
@Value("${hbase.a.username}")
private String userNameDs0;
@Value("${hbase.a.password}")
private String passwordDs0;
@Value("${hbase.b.seedserver}")
private String seedServerDs1;
@Value("${hbase.b.username}")
private String userNameDs1;
@Value("${hbase.b.password}")
private String passwordDs1;
//数据连接池
private static final ExecutorService executorDs0 = new ThreadPoolExecutor(30, 30, 10L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000));
private static final ExecutorService executorDs1 = new ThreadPoolExecutor(30, 30, 10L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000));
@Bean(name = "configHBaseA")
public org.apache.hadoop.conf.Configuration configHBaseA() {
// 新建一个Configuration
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
// 集群的连接地址(VPC内网地址)在控制台页面的数据库连接界面获得
conf.set(HBASE_QUORUM, seedServerDs0);
conf.set(HBASE_USERNAME, userNameDs0);
conf.set(HBASE_PASSWORD, passwordDs0);
return conf;
}
@Bean(name = "configHBaseB")
public org.apache.hadoop.conf.Configuration configHBaseB() {
// 新建一个Configuration
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
// 集群的连接地址(VPC内网地址)在控制台页面的数据库连接界面获得
conf.set(HBASE_QUORUM, seedServerDs1);
conf.set(HBASE_USERNAME, userNameDs1);
conf.set(HBASE_PASSWORD, passwordDs1);
return conf;
}
@Bean(name = "connection0", destroyMethod = "close")
public Connection connectionHBaseA(@Qualifier("configHBaseA") org.apache.hadoop.conf.Configuration configuration) {
try {
return ConnectionFactory.createConnection(configuration, executorDs0);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean(name = "connection1", destroyMethod = "close")
public Connection connectionHBaseB(@Qualifier("configHBaseB") org.apache.hadoop.conf.Configuration configuration) {
try {
return ConnectionFactory.createConnection(configuration, executorDs1);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
3.环境配置
#在实例列表->数据库连接中选择需要的连接方式
hbase.a.seedserver=xxxxxx:port
hbase.a.username=root
hbase.a.password=root
hbase.b.seedserver=xxxxxx:port
hbase.b.username=root
hbase.b.password=root
4.存取数据
@Service
public class HbaseServiceImpl implements HbaseService{
@Autowired
private Map<String, Connection> connectionMap;
final static String DATA_SOURCE_0 = "connectionHBaseA";
final static String DATA_SOURCE_1 = "connectionHBaseB";
//命名空间+表名
final static String TABLE_NAME = "space_name:table_name";
public void saveHbaseResult(){
String rowKey = "1001";
String md5Str = DigestUtils.md5DigestAsHex(rowKey.getBytes(StandardCharsets.UTF_8));
Development development = new Development();
String jsonString = JSON.toJSONString(development);
saveDataBytes(DATA_SOURCE_1,TABLE_NAME,md5Str,jsonString);
}
public String getHbaseResult(String hbaseKey){
Result result = getDataBytes(DATA_SOURCE_1, TABLE_NAME, hbaseKey);
if (result == null){
return null;
}
//从查询结果中获取列数据 , 其中列字段由data:values两部分组成
byte[] value = result.getValue(Bytes.toBytes("data"), Bytes.toBytes("values"));
if (value == null){
return null;
}
String s = new String(value);
return s;
}
//向Hbase中读数据
@Override
public Result getDataBytes(String datasource, String tableName, String rowKey) {
//根据名称选择数据源
Connection connection = connectionMap.get(datasource);
try (Table table = connection.getTable(TableName.valueOf(tableName))) {
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//向Hbase中写数据
@Override
public void saveDataBytes(String datasource, String tableName,String rowKey,String rowValue){
Connection connection = connectionMap.get(datasource);
try (Table table = connection.getTable(TableName.valueOf(tableName))) {
Put put = new Put(Bytes.toBytes(rowKey));
//列名由两部分组成
put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("values"), Bytes.toBytes(rowValue));
table.put(put);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}