国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|

服務器之家 - 編程語言 - JAVA教程 - 詳解spring封裝hbase的代碼實現

詳解spring封裝hbase的代碼實現

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

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

前面我們講了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包引入到你的項目里。

 
?
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配置文件。以下是通用配置,具體每個參數的含義可以百度以下,這里不做詳細講解。

 
?
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的配置文件中把這個文件Import進去。

 
?
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
<!--注意到沒有,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。

 
?
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等的操作方式,如何操作在我們的代碼示例中都有。個人覺得,spring封裝的HbaseTemplate不太好使,比如每次請求都會重新鏈接一下zookeeper集群(其中緣由我也沒去研究,有研究透的同學還望不吝賜教)。建議用原生的方式。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 一级a性色生活片毛片 | 后人极品翘臀美女在线播放 | 欧美一级全黄 | 99久久婷婷国产精品综合 | 黄色毛片看看 | 免费的av网站| 欧美日韩一二区 | 欧美日韩国产一区二区三区在线观看 | 国产精品久久久久久久久免费桃花 | 久久国产精品久久久久久电车 | 日韩欧美在线综合 | 97伦理在线 | 免费在线a| 免费看一级电影 | 一区二区成人网 | 久久国产精品免费一区二区三区 | 国产精品丝袜视频 | 亚洲国产精品福利 | www久久久久| 免费av电影网站 | 99成人在线 | 久久久久亚洲精品 | 亚洲综合自拍 | 成人精品视频 | 一区二区日韩精品 | 日韩高清国产一区在线 | 久久韩国| 亚洲精品久久久一区二区三区 | 日韩三级电影网 | www.久久久 | 国内自拍视频在线观看 | 欧美一区二区三区不卡 | 国产精品国色综合久久 | 日韩视频在线观看 | 精品在线视频播放 | 国产一级黄色av | 无码日韩精品一区二区免费 | 色综合888 | 国产97色在线 | 亚洲 | 国产精品高清在线 | 国产精品综合在线 |