詳解spring封裝hbase的代碼實現(xiàn)
前面我們講了spring封裝MongoDB的代碼實現(xiàn),這里我們講一下spring封裝Hbase的代碼實現(xiàn)。
hbase的簡介:
此處大概說一下,不是我們要討論的重點。
HBase是一個分布式的、面向列的開源數(shù)據(jù)庫,HBase在Hadoop之上提供了類似于Bigtable的能力。HBase是Apache的Hadoop項目的子項目。HBase不同于一般的關系數(shù)據(jù)庫,它是一個適合于非結構化數(shù)據(jù)存儲的數(shù)據(jù)庫。另一個不同的是HBase基于列的而不是基于行的模式。hbase是bigtable的開源山寨版本。是建立的hdfs之上,提供高可靠性、高性能、列存儲、可伸縮、實時讀寫的數(shù)據(jù)庫系統(tǒng)。它介于nosql和RDBMS之間,僅能通過主鍵(row key)和主鍵的range來檢索數(shù)據(jù),僅支持單行事務(可通過Hive支持來實現(xiàn)多表join等復雜操作)。主要用來存儲非結構化和半結構化的松散數(shù)據(jù)。與hadoop一樣,Hbase目標主要依靠橫向擴展,通過不斷增加廉價的商用服務器,來增加計算和存儲能力。hbase給我的印象就是無限存,按照Key讀取。
那么在我們的Java程序中應該如何使用hbase呢。
首先:
引入hbase的jar包,如果不是Maven項目,可以單獨按照以下格式下載hbase的jar包引入到你的項目里。
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>0.96.2-hadoop2</version> </dependency>
其次:
增加hbase在spring中的配置。
1. 新增hbase-site.xml配置文件。以下是通用配置,具體每個參數(shù)的含義可以百度以下,這里不做詳細講解。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--<property>-->
<!--<name>hbase.rootdir</name>-->
<!--<value>hdfs://ns1/hbase</value>-->
<!--</property>-->
<property>
<name>hbase.client.write.buffer</name>
<value>62914560</value>
</property>
<property>
<name>hbase.client.pause</name>
<value>1000</value>
</property>
<property>
<name>hbase.client.retries.number</name>
<value>10</value>
</property>
<property>
<name>hbase.client.scanner.caching</name>
<value>1</value>
</property>
<property>
<name>hbase.client.keyvalue.maxsize</name>
<value>6291456</value>
</property>
<property>
<name>hbase.rpc.timeout</name>
<value>60000</value>
</property>
<property>
<name>hbase.security.authentication</name>
<value>simple</value>
</property>
<property>
<name>zookeeper.session.timeout</name>
<value>60000</value>
</property>
<property>
<name>zookeeper.znode.parent</name>
<value>ZooKeeper中的HBase的根ZNode</value>
</property>
<property>
<name>zookeeper.znode.rootserver</name>
<value>root-region-server</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>zookeeper集群</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>
2. 新建spring-config-hbase.xml文件,記得在spring的配置文件中把這個文件Import進去。
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
http://www.springframework.org/schema/hadoop
http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
">
<hdp:configuration resources="classpath:spring/hbase-site.xml" />
<hdp:hbase-configuration configuration-ref="hadoopConfiguration" />
<bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<!--注意到?jīng)]有,spring的一貫風格,正如我們在mongodb篇講到的一樣,xxxTemplate封裝-->
<property name="configuration" ref="hbaseConfiguration">
</property>
</bean>
<bean class="com..HbaseDaoImpl" id="hbaseDao">
<constructor-arg ref="htemplate"/>
</bean>
</beans>
最后:
我們就可以重寫我們的HbaseDaoImple類了。在這里可以實現(xiàn)我們操作hbase的代碼邏輯。其中prism:OrderInfo是我們的表名,f是列族名稱,OrderInfo的屬性是列族下的列名。orderInfo是我程序定義的bean,你可以按照自己的需求定義自己的bean。
public class HbaseDaoImpl{
private HbaseTemplate hbaseTemplate;
private HConnection hconnection = null;
public HbaseDaoImpl(HbaseTemplate htemplate) throws Exception {
if (hconnection == null) {
hconnection = HConnectionManager.createConnection(htemplate.getConfiguration());
}
if (this.hbaseTemplate == null) {
this.hbaseTemplate = htemplate;
}
}
public void writeDataOrderinfo(final OrderInfo orderInfo) {
HTableInterface table = null;
try {
table = hconnection.getTable(Bytes.toBytes("prism:orderInfo"));
Put p = new Put(Bytes.toBytes( orderInfo.getHistoryId()));
p.add(Bytes.toBytes("f"), Bytes.toBytes("id"), Bytes.toBytes(orderInfo.getId()));
p.add(Bytes.toBytes("f"), Bytes.toBytes("historyId"), Bytes.toBytes(orderInfo.getHistoryId()));
p.add(Bytes.toBytes("f"), Bytes.toBytes("orderId"), Bytes.toBytes(orderInfo.getOrderId()));
p.add(Bytes.toBytes("f"), Bytes.toBytes("orderDirection"), Bytes.toBytes(orderInfo.getOrderDirection()));
p.add(Bytes.toBytes("f"), Bytes.toBytes("overStatus"), Bytes.toBytes(orderInfo.getOverStatus()));
p.add(Bytes.toBytes("f"), Bytes.toBytes("orgArea"), Bytes.toBytes(orderInfo.getOrgArea()));
table.put(p);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public OrderInfo getOrderInfoByRowkey(String rowKey) {
Get get = new Get(Bytes.toBytes(rowKey));
Scan scan = new Scan(get);
List<OrderInfo> list = hbaseTemplate.find("prism:orderInfo", scan, new RowMapper<OrderInfo>() {
@Override
public OrderInfo mapRow(Result result, int rowNum) throws Exception {
OrderInfo orderInfo = new OrderInfo();
orderInfo.setId(Bytes.toString(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("id"))));
orderInfo.setHistoryId(Bytes.toString(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("historyId"))));
orderInfo.setOrderId(Bytes.toLong(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("orderId"))));
return orderInfo;
}
});
if(list.size() > 0){
return list.get(0);
}else{
return null;
}
}
public List<OrderInfo> getOrderInfoByRange(String start_rowKey,String stop_rowKey) {
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(start_rowKey));
scan.setStopRow(Bytes.toBytes(stop_rowKey));
HTableInterface table = null;
ResultScanner rs = null;
List<OrderInfo> list = new ArrayList<OrderInfo>();
try {
table = hconnection.getTable(Bytes.toBytes("prism:orderInfo"));
rs = table.getScanner(scan);
for(Result result : rs){
OrderInfo orderInfo = new OrderInfo();
orderInfo.setId(Bytes.toString(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("id"))));
orderInfo.setHistoryId(Bytes.toString(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("historyId"))));
orderInfo.setOrderId(Bytes.toLong(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("orderId"))));
orderInfo.setOrderDirection(Bytes.toString(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("orderDirection"))));
list.add(orderInfo);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
rs.close();
}
return list;
}
public HbaseTemplate getHbaseTemplate() {
return hbaseTemplate;
}
public void setHbaseTemplate(HbaseTemplate hbaseTemplate) {
this.hbaseTemplate = hbaseTemplate;
}
}
注:在程序中,你可以使用spring封裝的HbaseTemplate,也可以使用原生的hconnection等的操作方式,如何操作在我們的代碼示例中都有。個人覺得,spring封裝的HbaseTemplate不太好使,比如每次請求都會重新鏈接一下zookeeper集群(其中緣由我也沒去研究,有研究透的同學還望不吝賜教)。建議用原生的方式。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- python利用thrift服務讀取hbase數(shù)據(jù)的方法
- python hbase讀取數(shù)據(jù)發(fā)送kafka的方法
- 通用MapReduce程序復制HBase表數(shù)據(jù)
- 在php的yii2框架中整合hbase庫的方法
- HBASE 常用shell命令,增刪改查方法
- hbase-shell批量命令執(zhí)行腳本的方法
- Hbase、elasticsearch整合中jar包沖突的問題解決
- 詳解VMware12使用三臺虛擬機Ubuntu16.04系統(tǒng)搭建hadoop-2.7.1+hbase-1.2.4(完全分布式)
- python 調(diào)用HBase的簡單實例
- Hbase入門詳解
相關文章
Java注解@Transactional事務類內(nèi)調(diào)用不生效問題及解決辦法
這篇文章主要介紹了Java注解@Transactional事務類內(nèi)調(diào)用不生效問題及解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
mybatis的映射xml中動態(tài)設置orderby方式
這篇文章主要介紹了mybatis的映射xml中動態(tài)設置orderby方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
springboot實現(xiàn)防重復提交和防重復點擊的示例
這篇文章主要介紹了springboot實現(xiàn)防重復提交和防重復點擊的示例,幫助大家更好的理解和學習springboot框架,感興趣的朋友可以了解下2020-09-09
Servlet+MyBatis項目轉Spring Cloud微服務,多數(shù)據(jù)源配置修改建議
今天小編就為大家分享一篇關于Servlet+MyBatis項目轉Spring Cloud微服務,多數(shù)據(jù)源配置修改建議,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
詳解Java編程中final,finalize,finally的區(qū)別
這篇文章主要介紹了詳解Java編程中final,finalize,finally的區(qū)別,這個在Java面試題中簡直是太常見了...需要的朋友可以參考下2015-11-11

