首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保持连接活动的弹簧HbaseTemplate

保持连接活动的弹簧HbaseTemplate
EN

Stack Overflow用户
提问于 2020-05-28 14:58:36
回答 1查看 815关注 0票数 1

我成功地使用HbaseSpring应用程序集成到HbaseTemplate中。

代码语言:javascript
复制
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(...)
        });
    }
}

但是,每次我运行findAll()时都会打开到Hbase的连接(然后就关闭)。我在某个地方读到,保持连接活动的方法是使用ConnectionTable来调用Hbase。问题是HbaseTemplate使用了HConnectionHTableInterface

如何使用HbaseTemplate保持连接正常?启动一个新的连接非常耗时,我只想做一次。或者,还有其他方式从Spring应用程序连接到Hbase吗?

我在用:

代码语言:javascript
复制
org.springframework.data:spring-data-hadoop:2.5.0.RELEASE
org.apache.hbase:hbase-client:1.1.2
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-29 14:53:49

我找到了两种解决这个问题的方法:

扩展HbaseAccessor 并实现 HbaseOperations自定义HbaseTemplate

最好的方法似乎是创建一个自定义类,它扩展HbaseAccessor并以与原始HbaseTemplate类似的方式实现HbaseOperations,但使用较新的HbaseTemplate(即。( Table而不是HTableInterface等)

如何实现它的例子之一可以在平易近项目中找到。

注射而不是 HbaseTemplate

另一种解决方案是将Connection注入存储库,并在那里完成所有繁重的工作:

代码语言:javascript
复制
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());
        }
    }
}

Connection @Bean可以这样配置:

代码语言:javascript
复制
@Configuration
public class HbaseConfiguration {

    @Bean
    public Connection() throws IOException {
        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
        // configuration setup
        return ConnectionFactory.createConnection(conf);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62067869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档