Home > Java > javaTutorial > How to integrate hbase in springboot

How to integrate hbase in springboot

WBOY
Release: 2023-05-30 16:31:24
forward
1205 people have browsed it

Dependencies:

<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>
Copy after login

Add configuration

The official method is through xml, which is simply rewritten as follows:

@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);
  }
}
Copy after login

application.yml :

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

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());
  }
}
Copy after login

The above is the detailed content of How to integrate hbase in springboot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template