如何使用Java而不是XML使用hbase和Spring Boot?

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

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

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

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

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

我对它的理解是,Spring for Apache Hadoop目前只支持命名空间配置。 当然可以查看支持命名空间的类并破解工作配置以找到如何使用java配置获得相同的结果,但老实说,我认为成本/增益比率并不合理。 由于它目前没有记录,你永远不会确定你没有忘记以后会破坏的东西。

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

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

或者,您可以在Spring管理的xml配置周围使用@Configuration包装器:

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

尽管HBase的基于Java的配置尚未正式提供(最近如Spring Hadoop 2.2.1),但仍有一个简洁的解决方法。 在基于Java的Spring Hadoop配置中包含相关的ZooKeeper详细信息作为属性。 然后, hbase-configuration XML标记不需要包含任何配置详细信息。 按照惯例,它将取决于基于Java的hadoopConfiguration定义的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配置与部署环境无关:

    

我写了一个简单的演示项目,用于在没有xml的spring boot应用程序中使用hbase。

这个演示主要依赖于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 

class 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); } } 

服务类,我们现在可以使用配置的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()); } } 

请享用! 🙂

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

希望这对你有所帮助。