一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|

服務(wù)器之家 - 編程語言 - JAVA教程 - 詳解spring封裝hbase的代碼實(shí)現(xiàn)

詳解spring封裝hbase的代碼實(shí)現(xiàn)

2020-10-28 15:05周游列國之仕子 JAVA教程

本篇文章主要介紹了詳解spring封裝hbase的代碼實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前面我們講了spring封裝MongoDB的代碼實(shí)現(xiàn),這里我們講一下spring封裝Hbase的代碼實(shí)現(xiàn)。

hbase的簡介:

此處大概說一下,不是我們要討論的重點(diǎn)。

HBase是一個(gè)分布式的、面向列的開源數(shù)據(jù)庫,HBase在Hadoop之上提供了類似于Bigtable的能力。HBase是Apache的Hadoop項(xiàng)目的子項(xiàng)目。HBase不同于一般的關(guān)系數(shù)據(jù)庫,它是一個(gè)適合于非結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)的數(shù)據(jù)庫。另一個(gè)不同的是HBase基于列的而不是基于行的模式。hbase是bigtable的開源山寨版本。是建立的hdfs之上,提供高可靠性、高性能、列存儲(chǔ)、可伸縮、實(shí)時(shí)讀寫的數(shù)據(jù)庫系統(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)目里。

 
?
1
 
2
3
4
5
<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ì)講解。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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)去。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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">
 
?
1
 
2
3
4
5
6
7
8
<!--注意到?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。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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é)還望不吝賜教)。建議用原生的方式。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/antao592/article/details/52788763

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲色图首页 | 亚洲精品色婷婷在线影院麻豆 | 亚洲精品国产精品国自产观看 | 岛国虐乳紧缚媚药调教 | 免费看黄色片网站 | 久久久久久久电影 | bdsm中国精品调教 | 国产自产2023最新麻豆 | 四虎国产精品免费久久久 | ak福利影院 | 我与旗袍老师疯狂床震 | 国产午夜精品一区二区三区不卡 | 亚洲一区二区三区91 | 国产精品久久久久久搜索 | 169pp美女 | 好男人好资源在线观看免费 | 国产欧美视频一区二区三区 | 精品手机在线视频 | 色妞女女女女女bbbb | 九色PORNY蝌蚪视频首页 | 日本性生活大片 | 国产草草 | 免费国产影视观看网站入口 | 99精品国产美女福到在线不卡 | 青青热久免费精品视频精品 | 久久毛片基地 | 欧美成狂野欧美在线观看 | 9 1 视频在线 | 久久视频精品3线视频在线观看 | 久久婷婷五月免费综合色啪 | 久久性综合亚洲精品电影网 | 国产精品久久久天天影视香蕉 | 三级无删减高清在线影院 | 久久精品久久久久 | 羲义嫁密着中出交尾gvg794 | 皇上好大好硬好涨好深好爽 | 四虎永久网址在线观看 | 精品国产自在现线久久 | 97色| 精品视频久久久久 | 古装一级毛片 |