【问题标题】:How to use hbase with Spring Boot using Java instead of XML?如何使用 Java 而不是 XML 将 hbase 与 Spring Boot 结合使用?
【发布时间】:2014-09-07 19:50:03
【问题描述】:

我有 Spring Boot Hadoop 并想利用 Spring HbaseTemplate。我的问题是文档只有关于配置和设置的“xml”方式的信息。

我如何以及在哪里将我的配置定义为 java 中的 hbase 配置,而不是官方文档中显示的 xml?

http://docs.spring.io/spring-hadoop/docs/1.0.1.RC1/reference/html/hbase.html

【问题讨论】:

    标签: java spring spring-mvc hadoop spring-boot


    【解决方案1】:

    嗯,这不是一个真正的预期答案,但我想开发它太多评论。

    我仔细阅读了最新发布版本中的Spring for Apache Hadoop - Reference Documentation,如果它确实包含命名空间配置的示例和详细信息,我在 Java 配置中找不到一行。

    我的理解是 Spring for Apache Hadoop 目前只支持命名空间配置。当然应该可以查看支持命名空间的类并破解工作配置以找到如何使用 java config 获得相同的结果,但老实说,我认为成本/收益比并不能证明它是合理的。而且由于它目前没有记录,因此您永远无法确定自己没有忘记以后会损坏的东西。

    由于 Spring 提供在 Java 配置 Spring 应用程序中包含 xml 配置文件,我强烈建议您保留所有现有 Java 配置,使用提供的命名空间在 xml 中编写 Apache Hadoop 部分,并简单地添加一个 @ImportResource注释到您的配置类。假设spring hadoop的配置是hadoopContext.xml在classpath的根目录下,你可以这样写:

    @Configuration
    ...
    @ImportResource("classpath:/hadoopContext.xml")
    public classConfig {
    ...
    }
    

    或者,您可以在您设法被 Spring 扫描的 xml 配置周围使用 @Configuration 包装器:

    @Configuration
    @ImportResource("classpath:/hadoopContext.xml")
    public class HadoopConfig {
    
    }
    

    【讨论】:

    • 我同意。有可能破解它,但不值得花时间。我与Spring Web Services 的情况相同,并使用了完全相同的@ImportResource 技巧,直到他们添加了对Java 配置的支持。
    【解决方案2】:

    尽管 HBase 的基于 Java 的配置尚未正式可用(最近是 Spring Hadoop 2.2.1),但有一个干净的解决方法。将相关的 ZooKeeper 详细信息作为属性包含在基于 Java 的 Spring Hadoop 配置中。然后,hbase-configuration XML 标记不需要包含任何配置细节。按照惯例,它将依赖于基于 Java 的 HadoopConfigConfigurer 定义的 hadoopConfiguration bean。

    @Configuration
    @EnableHadoop
    public class MapReduceConfiguration extends SpringHadoopConfigurerAdapter {
        @Override
        public void configure(HadoopConfigConfigurer config) throws Exception {
            config
                    .fileSystemUri(myConfigManager.getHadoopFsUri())
                    .resourceManagerAddress(myConfigManager.getHadoopResourceManagerAddress())
                    .withProperties()
                    .property("hbase.zookeeper.quorum", myConfigManager.getZookeeperQuorum())
                    .property("hbase.zookeeper.property.clientPort", myConfigManager.getZookeeperPort())
                    .property("hbase.client.scanner.caching", "1");
        }
    }
    

    剩余的 XML 配置与部署环境无关:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:hdp="http://www.springframework.org/schema/hadoop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
        <hdp:hbase-configuration />
    </beans>
    

    【讨论】:

      【解决方案3】:

      我写了一个简单的演示项目,在没有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());
          }
      }
      

      享受吧! :)

      【讨论】:

        【解决方案4】:
        【解决方案5】:

        您可以在 github 中查看 spring-hadoop 示例,特别是 spring-hadoop-samples/original-samples/hbase-crud 中的文件。存储库位于https://github.com/SpringSource/spring-hadoop-samples

        希望对你有所帮助。

        【讨论】:

        • 没有与 hbase 或 hbaseTemplate 相关的任何代码。自述文件中提到了,但仅此而已。
        猜你喜欢
        • 2017-02-01
        • 1970-01-01
        • 2022-11-30
        • 2020-09-17
        • 1970-01-01
        • 2017-03-30
        • 1970-01-01
        • 2021-01-04
        • 1970-01-01
        相关资源
        最近更新 更多