前面我們講了spring封裝MongoDB的代碼實現,這裡我們講一下spring封裝Hbase的代碼實現。
hbase的簡介:
此處大概說一下,不是我們要討論的重點。
HBase是一個分布式的、面向列的開源數據庫,HBase在Hadoop之上提供了類似於Bigtable的能力。HBase是Apache的Hadoop項目的子項目。HBase不同於一般的關系數據庫,它是一個適合於非結構化數據存儲的數據庫。另一個不同的是HBase基於列的而不是基於行的模式。hbase是bigtable的開源山寨版本。是建立的hdfs之上,提供高可靠性、高性能、列存儲、可伸縮、實時讀寫的數據庫系統。它介於nosql和RDBMS之間,僅能通過主鍵(row key)和主鍵的range來檢索數據,僅支持單行事務(可通過Hive支持來實現多表join等復雜操作)。主要用來存儲非結構化和半結構化的松散數據。與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配置文件。以下是通用配置,具體每個參數的含義可以百度以下,這裡不做詳細講解。
<?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">
<!--注意到沒有,spring的一貫風格,正如我們在mongodb篇講到的一樣,xxxTemplate封裝-->
<property name="configuration" ref="hbaseConfiguration">
</property>
</bean>
<bean class="com..HbaseDaoImpl" id="hbaseDao">
<constructor-arg ref="htemplate"/>
</bean>
</beans>
最後:
我們就可以重寫我們的HbaseDaoImple類了。在這裡可以實現我們操作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集群(其中緣由我也沒去研究,有研究透的同學還望不吝賜教)。建議用原生的方式。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。