2015-07-13 104 views

回答

0

目前沒有樣本項目可以做到這一點。我認爲這將是非常好的事情,並且會對示例項目非常感謝 - https://github.com/spring-projects/spring-hadoop-samples/

我們最近添加了一個帶有XML示例的普通HBase,但它沒有使用Spring Boot。也許重寫使用引導和@Config將是可行的。

0

我寫了一個簡單的演示項目,在沒有xml的spring引導應用程序中使用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') 

配置在春季啓動的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); 
    } 

} 

服務類,我們現在可以使用配置的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()); 
    } 
}