srping boot 连接hbase
时间: 2023-10-06 11:08:28 浏览: 33
可以使用 HBase 的 Java API 进行连接,同时可以借助 Spring Data Hadoop 提供的 HBaseTemplate 简化操作。
具体步骤如下:
1.在 pom.xml 中引入相关依赖:
```
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop-core</artifactId>
<version>2.4.0.RELEASE</version>
</dependency>
```
2.配置 HBase 连接信息:
```
@Configuration
public class HBaseConfiguration {
@Value("${hbase.zookeeper.quorum}")
private String zookeeperQuorum;
@Bean
public org.apache.hadoop.conf.Configuration configuration() {
org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", zookeeperQuorum);
configuration.set("hbase.zookeeper.property.clientPort", "2181");
return configuration;
}
@Bean
public HBaseTemplate hbaseTemplate() {
return new HBaseTemplate(configuration());
}
}
```
3.实现相关操作:
```
@Autowired
private HBaseTemplate hbaseTemplate;
public void addData() {
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("column1"),Bytes.toBytes("value1"));
hbaseTemplate.execute("table1", (table) -> {
table.put(put);
return true;
});
}
public void getData() {
String rowkey = "row1";
hbaseTemplate.execute("table1", (table) -> {
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
return Bytes.toString(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("column1")));
});
}
```
以上代码仅作为示例,实际应用中需要根据具体需求进行修改。