【问题标题】:Use Hbase with spring boot [duplicate]将 Hbase 与 Spring Boot 一起使用 [重复]
【发布时间】:2015-07-13 11:01:03
【问题描述】:

是否有示例项目展示如何在 spring boot 中使用 hbase? 我读了这个链接How to use hbase with Spring Boot using Java instead of XML?。但是说明不够详细。

【问题讨论】:

    标签: spring-boot spring-data-hadoop


    【解决方案1】:

    我编写了一个简单的演示项目,用于在没有 xml 的 Spring Boot 应用程序中使用 hbase。并回答了您提到的其他相关问题。

    这个demo主要依赖spring-data-hadoop和hbase-client。

    gradle 依赖:

    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile 'org.springframework.data:spring-data-hadoop:2.5.0.RELEASE'
    compile('org.apache.hbase:hbase-client:1.3.1'){
        exclude group :'log4j',module:'log4j'
        exclude group :'org.slf4j',module:'slf4j-log4j12'
        exclude group: 'javax.servlet', module: 'servlet-api'
    }
    compile('org.springframework.boot:spring-boot-configuration-processor')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    

    在spring boot的application.properties中配置hbase连接参数(无XML!):

    spring.data.hbase.zkQuorum=192.168.0.109:2181
    spring.data.hbase.zkBasePath=/hbase
    spring.data.hbase.rootDir=file:///home/hbase-1.2.2
    

    类 HbaseProperties.java:

    @ConfigurationProperties(prefix = "spring.data.hbase")
    public class HbaseProperties {
        // Addresses of all registered ZK servers.
        private String zkQuorum;
    
        // Location of HBase home directory
        private String rootDir;
    
        // Root node of this cluster in ZK.
        private String zkBasePath;
    
        // getters and setters...
    
    }
    

    HbaseConfig.java,将配置注入HbaseTemplate:

    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.hadoop.hbase.HbaseTemplate;
    
    @Configuration
    @EnableConfigurationProperties(HbaseProperties.class)
    public class HbaseConfig {
    
        @Autowired
        private HbaseProperties hbaseProperties;
    
        @Bean
        public HbaseTemplate hbaseTemplate() {
            org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", this.hbaseProperties.getZkQuorum());
            configuration.set("hbase.rootdir", this.hbaseProperties.getRootDir());
            configuration.set("zookeeper.znode.parent", this.hbaseProperties.getZkBasePath());
            return new HbaseTemplate(configuration);
        }
    
    }
    

    Service类,我们现在可以使用配置的HbaseTemplate了:

    import javax.annotation.PostConstruct;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.hadoop.hbase.HbaseTemplate;
    import org.springframework.stereotype.Service;
    
    import com.zql.hbasedemo.vo.Quote;
    
    @Service
    public class FeedService {
        @Autowired
        private HbaseTemplate hbaseTemplate;
    
        @PostConstruct
        public void test(){
            Quote quote = new Quote();
            quote.setEventType("ft");
            quote.setHandicap("4");
            quote.setMarket("OU");
            quote.setMatchId("27350208");
            quote.setSelection("OVER");
            quote.setPrice("1.93");
            saveQuote(quote);
        }
    
        public void saveQuote(Quote quote) {
            hbaseTemplate.put("quotes", quote.getMatchId(), "data", quote.getMarket() + ":" + quote.getSelection(),
                quote.getPrice().getBytes());
        }
    }
    

    【讨论】:

      【解决方案2】:

      目前没有执行此操作的示例项目。我认为很高兴拥有并且非常感谢对示例项目的贡献 - https://github.com/spring-projects/spring-hadoop-samples/

      我们最近添加了一个带有 XML 示例的普通 HBase,但它没有使用 Spring Boot。也许重写以使用 Boot 和 @Config 是可行的。

      【讨论】:

        猜你喜欢
        • 2021-01-02
        • 2020-03-09
        • 2022-01-24
        • 2016-01-29
        • 2018-04-25
        • 2015-11-03
        • 2016-12-27
        • 2017-03-21
        • 2018-08-07
        相关资源
        最近更新 更多