Java Spring HBasetTemplate使连接保持活动状态
我使用Java Spring HBasetTemplate使连接保持活动状态,java,spring,hbase,spring-data-hadoop,Java,Spring,Hbase,Spring Data Hadoop,我使用HbaseTemplate将Hbase集成到Spring应用程序中: import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.hadoop.hbase.HbaseTemplate; import org.springframework.stereotype.Component; import java.util.List; @Component publ
HbaseTemplate
将Hbase
集成到Spring
应用程序中:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class ItemRepositoryImpl implements ItemRepository {
@Autowired
private HbaseTemplate hbaseTemplate;
@Override
public List<Item> findAll() {
Scan scan = new Scan();
scan.addColumn(CF, CQ);
hbaseTemplate.find("TABLE_NAME", scan, (result, rowNum) -> {
return new Item(...)
});
}
}
我找到了两种解决这个问题的方法: 自定义HbaseTemplate,它扩展了
HbaseAccessor
并实现了HbaseOperations
最好的方法似乎是创建一个自定义类,该类扩展HbaseAccessor
,并以与原始HbaseTemplate
类似的方式实现HbaseOperations
,但使用较新的API(即Table
而不是HTableInterface
等)
如何实现它的一个示例可以在项目中找到
注入连接
而不是HbaseTemplate
另一种解决方案是向存储库注入连接
,并在那里执行所有繁重的工作:
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.stream.Collectors;
import java.stream.StreamSupport;
@Component
public class ItemRepositoryImpl implements ItemRepository {
@Autowired
private Connection connection;
@Override
public List<Item> findAll() throws IOException {
Scan scan = new Scan();
scan.addColumn(CF, CQ);
try (Table table = connection.getTable(TableName.valueOf(TABLE_NAME))) {
return StreamSupport
.stream(table.getScanner(scan).spliterator, false)
.map(...)
.collect(Collectors.toList());
}
}
}
我找到了两种解决这个问题的方法: 自定义HbaseTemplate,它扩展了
HbaseAccessor
并实现了HbaseOperations
最好的方法似乎是创建一个自定义类,该类扩展HbaseAccessor
,并以与原始HbaseTemplate
类似的方式实现HbaseOperations
,但使用较新的API(即Table
而不是HTableInterface
等)
如何实现它的一个示例可以在项目中找到
注入连接
而不是HbaseTemplate
另一种解决方案是向存储库注入连接
,并在那里执行所有繁重的工作:
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.stream.Collectors;
import java.stream.StreamSupport;
@Component
public class ItemRepositoryImpl implements ItemRepository {
@Autowired
private Connection connection;
@Override
public List<Item> findAll() throws IOException {
Scan scan = new Scan();
scan.addColumn(CF, CQ);
try (Table table = connection.getTable(TableName.valueOf(TABLE_NAME))) {
return StreamSupport
.stream(table.getScanner(scan).spliterator, false)
.map(...)
.collect(Collectors.toList());
}
}
}
Hbase没有连接池吗?是的,我可以使用独立的
Hbase客户端
,就像建议的那样。我只是不知道如何使用Spring
数据工具。Hbase没有连接池吗?是的,我可以使用一个独立的Hbase客户机
,就像建议的那样。我只是不知道如何使用Spring
数据工具。
@Configuration
public class HbaseConfiguration {
@Bean
public Connection() throws IOException {
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
// configuration setup
return ConnectionFactory.createConnection(conf);
}
}