java 获取 hbase数据 springdatahadoop -- hbasetemplate

原博客地址:http://blog.csdn.net/linlinv3/article/details/42737113


配置文件
1、建立 spring-hbase.xml 获取连接池
第一种:直接在配置文件注明 hdfs的端口和zk的地址以及端口进行连接
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <beans xmlns="http://www.springframework.org/schema/beans"  
  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.    xmlns:hdp="http://www.springframework.org/schema/hadoop"  
  5.    xmlns:beans="http://www.springframework.org/schema/beans"  
  6.    xmlns:context="http://www.springframework.org/schema/context"  
  7.    xsi:schemaLocation="  
  8.     http://www.springframework.org/schema/beans   
  9.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.     http://www.springframework.org/schema/hadoop   
  11.     http://www.springframework.org/schema/hadoop/spring-hadoop.xsd  
  12.     http://www.springframework.org/schema/context   
  13.     http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  14.      
  15.    <context:property-placeholder location="hbase.properties" />  
  16.      
  17.       <!-- 配置HbaseTemplate -->  
  18.     <bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">    
  19.           <property name="configuration" ref="hbaseConfiguration">  
  20.           </property>  
  21.      </bean>    
  22.     <!-- 配置hadoop的基本信息 -->   
  23.      <hdp:configuration>  
  24.           fs.default.name="hdfs://192.168.0.173:8082"  
  25.      </hdp:configuration>   
  26.      <!-- 配置zookeeper地址和端口 -->  
  27.      <hdp:hbase-configuration zk-quorum="192.168.0.173" zk-port="2181" />   
  28.  </beans>  
第二种:只需要配置zk的地址和端口,但是需要在classpath另外新增 hbase-site.xml的配置
spring-hbase.xml 配置:
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <beans xmlns="http://www.springframework.org/schema/beans"  
  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.    xmlns:hdp="http://www.springframework.org/schema/hadoop"  
  5.    xmlns:beans="http://www.springframework.org/schema/beans"  
  6.    xsi:schemaLocation="  
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.     http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">  
  9.       <!-- 配置zookeeper的信息,远程连接hbase时使用 -->  
  10.     <hdp:configuration resources="classpath:/hbase-site.xml" />  
  11.     <hdp:hbase-configuration configuration-ref="hadoopConfiguration" />  
  12.     <!-- 配置HbaseTemplate -->  
  13.     <bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">  
  14.         <property name="configuration" ref="hbaseConfiguration">  
  15.         </property>  
  16.     <property name="encoding" value="UTF-8"></property>  
  17.     </bean>  
  18.  </beans>  
hbase-site.xml 配置  放在classpath下(直接扔到src下即可)
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0"?>  
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  
  3. <configuration>  
  4.     <property>  
  5.         <name>hbase.zookeeper.quorum</name>  
  6.         <value>192.168.0.173</value>  
  7.     </property>  
  8.   
  9.     <property>  
  10.         <name>hbase.zookeeper.property.clientPort</name>  
  11.         <value>2181</value>  
  12.     </property>  
  13. </configuration>  
配置完成


程序代码
官网的api地址已经在上面发过了,这里只做了一个简单的demo,列举了简单的 get  find execue 方法
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.fulong.hbase;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.hadoop.hbase.Cell;  
  8. import org.apache.hadoop.hbase.client.HTableInterface;  
  9. import org.apache.hadoop.hbase.client.Put;  
  10. import org.apache.hadoop.hbase.client.Result;  
  11. import org.apache.hadoop.hbase.client.Scan;  
  12. import org.apache.hadoop.hbase.util.Bytes;  
  13. import org.springframework.beans.factory.BeanFactory;  
  14. import org.springframework.context.ApplicationContext;  
  15. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  16. import org.springframework.data.hadoop.hbase.HbaseTemplate;  
  17. import org.springframework.data.hadoop.hbase.RowMapper;  
  18. import org.springframework.data.hadoop.hbase.TableCallback;  
  19.   
  20. import cn.fulong.view.HbaseModel;  
  21. public class HbaseTest {  
  22.     //ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "application_hbase.xml" });    
  23.     ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring_hbase.xml" });    
  24.   
  25.     BeanFactory factory = (BeanFactory) context;   
  26.     HbaseTemplate htemplate = (HbaseTemplate) factory.getBean("htemplate");  
  27.     Map hMap = new HashMap<String, List<HbaseModel>>();  
  28.     public String key;  
  29.     public String familyName ;  
  30.     public String qualifier;  
  31.     public String value;  
  32.           
  33.     public String getValue() {  
  34.         return value;  
  35.     }  
  36.     public void setValue(String value) {  
  37.         this.value = value;  
  38.     }  
  39.     public String getFamilyName() {  
  40.         return familyName;  
  41.     }  
  42.     public void setFamilyName(String familyName) {  
  43.         this.familyName = familyName;  
  44.     }  
  45.     public String getQualifier() {  
  46.         return qualifier;  
  47.     }  
  48.     public void setQualifier(String qualifier) {  
  49.         this.qualifier = qualifier;  
  50.     }  
  51.     public String getKey() {  
  52.         return key;  
  53.     }  
  54.     public void setKey(String key) {  
  55.         this.key = key;  
  56.     }  
  57.       
  58.            
  59.           
  60.     public static void main(String[] args) {  
  61.         //PrefixFilter  
  62.         HbaseTest h = new HbaseTest();  
  63.    
  64.         for(int i=0;i<=10000;i++){  
  65.             h.setKey("linlin"+i);  
  66.             h.setFamilyName("info");   
  67.             h.setQualifier("service");  
  68.             h.setValue(i+"技术创新和质量服务");  
  69.             h.execute("linlintest"null);  
  70.         }  
  71.       
  72.      List<Map<String,Object>>  mapList1 = h.find("linlintest",null,null);  
  73.         System.out.println("2");  
  74.     }  
  75.     /** 
  76.      * 写数据 
  77.      * @param tableName 
  78.      * @param action 
  79.      * @return 
  80.      */  
  81.     public Boolean execute(String tableName, TableCallback<Boolean> action) {    
  82.         return htemplate.execute(tableName, new TableCallback<Boolean>() {  
  83.             public Boolean doInTable(HTableInterface table) throws Throwable {  
  84.                 boolean flag = false;  
  85.                 try{  
  86.                     byte[] rowkey = key.getBytes();  
  87.                     Put put = new Put(rowkey);  
  88.                     put.add(Bytes.toBytes(familyName),Bytes.toBytes(qualifier), Bytes.toBytes(value));  
  89.                     table.put(put);  
  90.                  flag = true;  
  91.                 }catch(Exception e){  
  92.                     e.printStackTrace();  
  93.                 }  
  94.                 return flag;  
  95.             }  
  96.         });  
  97.     }    
  98.      /** 
  99.       * 通过表名和key获取一行数据 
  100.       * @param tableName 
  101.       * @param rowName 
  102.       * @return 
  103.       */  
  104.     public Map<String, Object> get(String tableName, String rowName) {  
  105.          return htemplate.get(tableName, rowName,new RowMapper<Map<String,Object>>(){  
  106.                public Map<String,Object> mapRow(Result result, int rowNum) throws Exception {      
  107.                    List<Cell> ceList =   result.listCells();  
  108.                    Map<String,Object> map = new HashMap<String, Object>();  
  109.                         if(ceList!=null&&ceList.size()>0){  
  110.                             for(Cell cell:ceList){  
  111.                                 map.put(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+  
  112.                                    "_"+Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()),   
  113.                                    Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));  
  114.                        }  
  115.                    }  
  116.                     return  map;  
  117.                }  
  118.              });  
  119.     }  
  120.       
  121.     /** 
  122.      * 通过表名  key 和 列族 和列 获取一个数据 
  123.      * @param tableName 
  124.      * @param rowName 
  125.      * @param familyName 
  126.      * @param qualifier 
  127.      * @return 
  128.      */  
  129.     public String get(String tableName ,String rowName, String familyName, String qualifier) {  
  130.           return htemplate.get(tableName, rowName,familyName,qualifier ,new RowMapper<String>(){  
  131.                  public String mapRow(Result result, int rowNum) throws Exception {     
  132.                      List<Cell> ceList =   result.listCells();  
  133.                      String res = "";  
  134.                      if(ceList!=null&&ceList.size()>0){  
  135.                          for(Cell cell:ceList){  
  136.                              res = Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());  
  137.                          }  
  138.                      }  
  139.                    return res;  
  140.                  }  
  141.           });  
  142.     }  
  143.       
  144.       
  145.      /** 
  146.       * 通过表名,开始行键和结束行键获取数据 
  147.       * @param tableName 
  148.       * @param startRow 
  149.       * @param stopRow 
  150.       * @return 
  151.       */  
  152.     public List<Map<String,Object>> find(String tableName , String startRow,String stopRow) {  
  153.          Scan scan = new Scan();  
  154.          if(startRow==null){  
  155.              startRow="";  
  156.          }  
  157.          if(stopRow==null){  
  158.              stopRow="";      
  159.          }  
  160.          scan.setStartRow(Bytes.toBytes(startRow));  
  161.          scan.setStopRow(Bytes.toBytes(stopRow));  
  162.         /* PageFilter filter = new PageFilter(5); 
  163.          scan.setFilter(filter);*/  
  164.          return     htemplate.find(tableName, scan,new RowMapper<Map<String,Object>>(){  
  165.                public Map<String,Object> mapRow(Result result, int rowNum) throws Exception {   
  166.                     
  167.                      List<Cell> ceList =   result.listCells();  
  168.                      Map<String,Object> map = new HashMap<String,Object>();  
  169.                      Map<String,Map<String,Object>> returnMap = new HashMap<String,Map<String,Object>>();  
  170.                      String  row = "";  
  171.                      if(ceList!=null&&ceList.size()>0){  
  172.                            for(Cell cell:ceList){  
  173.                             row =Bytes.toString( cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());  
  174.                             String value =Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());  
  175.                             String family =  Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());  
  176.                             String quali = Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());  
  177.                             map.put(family+"_"+quali, value);  
  178.                            }  
  179.                            map.put("row",row );  
  180.                        }  
  181.                        return  map;  
  182.                    }  
  183.                  });  
  184.     }  
  185.   
  186.       
  187.     /* public  void scanValueByFilter(String tableName,String row) throws IOException{ 
  188.          HTable table = new HTable(conf, tableName);   
  189.          Scan scan = new Scan(); 
  190.          scan.setFilter(new PrefixFilter(row.getBytes())); 
  191.          ResultScanner resultScanner = table.getScanner(scan); 
  192.          for(Result rs:resultScanner){ 
  193.      
  194.               
  195.          } 
  196.           
  197.      }*/  
  198. }  


下载

具体可运行代码下载地址    http://download.csdn.net/detail/linlinv3/8580185


项目目录结构和所需jar包如下图所示:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值