詳解spring封裝hbase的代碼實(shí)現(xiàn)
前面我們講了spring封裝MongoDB的代碼實(shí)現(xiàn),這里我們講一下spring封裝Hbase的代碼實(shí)現(xiàn)。
hbase的簡(jiǎn)介:
此處大概說一下,不是我們要討論的重點(diǎn)。
HBase是一個(gè)分布式的、面向列的開源數(shù)據(jù)庫(kù),HBase在Hadoop之上提供了類似于Bigtable的能力。HBase是Apache的Hadoop項(xiàng)目的子項(xiàng)目。HBase不同于一般的關(guān)系數(shù)據(jù)庫(kù),它是一個(gè)適合于非結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)的數(shù)據(jù)庫(kù)。另一個(gè)不同的是HBase基于列的而不是基于行的模式。hbase是bigtable的開源山寨版本。是建立的hdfs之上,提供高可靠性、高性能、列存儲(chǔ)、可伸縮、實(shí)時(shí)讀寫的數(shù)據(jù)庫(kù)系統(tǒng)。它介于nosql和RDBMS之間,僅能通過主鍵(row key)和主鍵的range來檢索數(shù)據(jù),僅支持單行事務(wù)(可通過Hive支持來實(shí)現(xiàn)多表join等復(fù)雜操作)。主要用來存儲(chǔ)非結(jié)構(gòu)化和半結(jié)構(gòu)化的松散數(shù)據(jù)。與hadoop一樣,Hbase目標(biāo)主要依靠橫向擴(kuò)展,通過不斷增加廉價(jià)的商用服務(wù)器,來增加計(jì)算和存儲(chǔ)能力。hbase給我的印象就是無限存,按照Key讀取。
那么在我們的Java程序中應(yīng)該如何使用hbase呢。
首先:
引入hbase的jar包,如果不是Maven項(xiàng)目,可以單獨(dú)按照以下格式下載hbase的jar包引入到你的項(xiàng)目里。
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>0.96.2-hadoop2</version> </dependency>
其次:
增加hbase在spring中的配置。
1. 新增hbase-site.xml配置文件。以下是通用配置,具體每個(gè)參數(shù)的含義可以百度以下,這里不做詳細(xì)講解。
<?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的配置文件中把這個(gè)文件Import進(jìn)去。
<?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的一貫風(fēng)格,正如我們?cè)趍ongodb篇講到的一樣,xxxTemplate封裝--> <property name="configuration" ref="hbaseConfiguration"> </property> </bean> <bean class="com..HbaseDaoImpl" id="hbaseDao"> <constructor-arg ref="htemplate"/> </bean> </beans>
最后:
我們就可以重寫我們的HbaseDaoImple類了。在這里可以實(shí)現(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等的操作方式,如何操作在我們的代碼示例中都有。個(gè)人覺得,spring封裝的HbaseTemplate不太好使,比如每次請(qǐng)求都會(huì)重新鏈接一下zookeeper集群(其中緣由我也沒去研究,有研究透的同學(xué)還望不吝賜教)。建議用原生的方式。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python利用thrift服務(wù)讀取hbase數(shù)據(jù)的方法
- python hbase讀取數(shù)據(jù)發(fā)送kafka的方法
- 通用MapReduce程序復(fù)制HBase表數(shù)據(jù)
- 在php的yii2框架中整合hbase庫(kù)的方法
- HBASE 常用shell命令,增刪改查方法
- hbase-shell批量命令執(zhí)行腳本的方法
- Hbase、elasticsearch整合中jar包沖突的問題解決
- 詳解VMware12使用三臺(tái)虛擬機(jī)Ubuntu16.04系統(tǒng)搭建hadoop-2.7.1+hbase-1.2.4(完全分布式)
- python 調(diào)用HBase的簡(jiǎn)單實(shí)例
- Hbase入門詳解
相關(guān)文章
使用Java的方式模擬Flutter的Widget實(shí)現(xiàn)多層括號(hào)嵌套
這篇文章主要介紹了使用Java的方式模擬Flutter的Widget的實(shí)現(xiàn)多層括號(hào)嵌套問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07springboot yml定義屬性,下文中${} 引用說明
這篇文章主要介紹了springboot yml定義屬性,下文中${} 引用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04使用Jenkins來構(gòu)建GIT+Maven項(xiàng)目的方法步驟
這篇文章主要介紹了使用Jenkins來構(gòu)建GIT+Maven項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01java swing實(shí)現(xiàn)電影購(gòu)票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java swing實(shí)現(xiàn)電影購(gòu)票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Java負(fù)載均衡服務(wù)器實(shí)現(xiàn)上傳文件同步
這篇文章主要介紹了Java負(fù)載均衡服務(wù)器實(shí)現(xiàn)上傳文件同步,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Java設(shè)計(jì)模式之觀察者模式(Observer模式)
這篇文章主要介紹了Java設(shè)計(jì)模式之觀察者模式(Observer模式),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04關(guān)于Spring源碼深度解析(AOP功能源碼解析)
這篇文章主要介紹了關(guān)于Spring源碼深度解析(AOP功能源碼解析),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11