欢迎来到入门教程网!

Java

当前位置:主页 > 软件编程 > Java >

基于springboot集成hbase过程解析

来源:本站原创|时间:2020-01-10|栏目:Java|点击:

这篇文章主要介绍了基于springboot集成hbase过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

springboot-habse:

https://github.com/spring-projects/spring-hadoop-samples/tree/master/hbase

依赖:

<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-hadoop-hbase</artifactId>
      <version>2.5.0.RELEASE</version>
    </dependency>
 
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.1.2</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-hadoop</artifactId>
      <version>2.5.0.RELEASE</version>
    </dependency>

增加配置

官方提供的方式是通过xml方式,简单改写后如下:

@Configuration
public class HBaseConfiguration {
 
  @Value("${hbase.zookeeper.quorum}")
  private String zookeeperQuorum;
 
  @Value("${hbase.zookeeper.property.clientPort}")
  private String clientPort;
 
  @Value("${zookeeper.znode.parent}")
  private String znodeParent;
 
  @Bean
  public HbaseTemplate hbaseTemplate() {
    org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
    conf.set("hbase.zookeeper.quorum", zookeeperQuorum);
    conf.set("hbase.zookeeper.property.clientPort", clientPort);
    conf.set("zookeeper.znode.parent", znodeParent);
    return new HbaseTemplate(conf);
  }
}

application.yml:

hbase:
 zookeeper:
  quorum: hadoop001,hadoop002,hadoop003
  property:
   clientPort: 2181
 
zookeeper:
 znode:
  parent: /hbase

HbaseTemplate test :

@Service
@Slf4j
public class HBaseService {
 
 
  @Autowired
  private HbaseTemplate hbaseTemplate;
 
 
  public List<Result> getRowKeyAndColumn(String tableName, String startRowkey, String stopRowkey, String column, String qualifier) {
    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
    if (StringUtils.isNotBlank(column)) {
      log.debug("{}", column);
      filterList.addFilter(new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(column))));
    }
    if (StringUtils.isNotBlank(qualifier)) {
      log.debug("{}", qualifier);
      filterList.addFilter(new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(qualifier))));
    }
    Scan scan = new Scan();
    if (filterList.getFilters().size() > 0) {
      scan.setFilter(filterList);
    }
    scan.setStartRow(Bytes.toBytes(startRowkey));
    scan.setStopRow(Bytes.toBytes(stopRowkey));
 
    return hbaseTemplate.find(tableName, scan, (rowMapper, rowNum) -> rowMapper);
  }
 
  public List<Result> getListRowkeyData(String tableName, List<String> rowKeys, String familyColumn, String column) {
    return rowKeys.stream().map(rk -> {
      if (StringUtils.isNotBlank(familyColumn)) {
        if (StringUtils.isNotBlank(column)) {
          return hbaseTemplate.get(tableName, rk, familyColumn, column, (rowMapper, rowNum) -> rowMapper);
        } else {
          return hbaseTemplate.get(tableName, rk, familyColumn, (rowMapper, rowNum) -> rowMapper);
        }
      }
      return hbaseTemplate.get(tableName, rk, (rowMapper, rowNum) -> rowMapper);
    }).collect(Collectors.toList());
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:Java方法覆盖重写实现原理解析

栏    目:Java

下一篇:Java如何获取数组和字符串的长度(length还是length())

本文标题:基于springboot集成hbase过程解析

本文地址:https://www.xiuzhanwang.com/a1/Java/8824.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有