当前位置: 移动技术网 > IT编程>开发语言>Java > 详解spring封装hbase的代码实现

详解spring封装hbase的代码实现

2019年07月22日  | 移动技术网IT编程  | 我要评论

前面我们讲了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集群(其中缘由我也没去研究,有研究透的同学还望不吝赐教)。建议用原生的方式。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网