分享

Hbase Java编程实现增删改查

pig2 2014-2-14 11:46:03 发表于 实操演练 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 3 26400
阅读本文可以带着下面问题:
1.那个API提供接口来管理HBase数据库中的表信息,如创建表、删除表等方法?
2.那个API提供了对列族的操作。
3.对单个行执行添加操作,可以通过那个API
4.org.apache.hadoop.hbase.client.Get的作用是什么?
5.能否根据自己的理解写一个hbase的增删改查?


一、HBase Java API:

    1. HbaseConfiguration
     关系:org.apache.hadoop.hbase.HBaseConfiguration
     作用:通过此类可以对HBase进行配置
    2.HBaseAdmin
     关系:org.apache.hadoop.hbase.client.HBaseAdmin
     作用:提供一个接口来管理HBase数据库中的表信息。它提供创建表、删除表等方法。
    3.HTableDescriptor
     关系:org.apache.hadoop.hbase.client.HTableDescriptor
     作用:包含了表的名字及其对应列族。 提供的方法有
        void                addFamily(HColumnDescriptor)              添加一个列族
        HColumnDescriptor   removeFamily(byte[] column)               移除一个列族
        byte[]              getName()                                 获取表的名字
        byte[]              getValue(byte[] key)                      获取属性的值
        void                setValue(String key,String value)         设置属性的值
     4.HColumnDescriptor
      关系:org.apache.hadoop.hbase.client.HColumnDescriptor
      作用:维护关于列的信息。提供的方法有
        byte[]              getName()                                 获取列族的名字
        byte[]              getValue()                                获取对应的属性的值
        void                setValue(String key,String value)         设置对应属性的值
     5.HTable
      关系:org.apache.hadoop.hbase.client.HTable
      作用:用户与HBase表进行通信。此方法对于更新操作来说是非线程安全的,如果启动多个线程尝试与单个HTable实例进行通信,那么写缓冲器可能会崩溃。
    6.Put
      关系:org.apache.hadoop.hbase.client.Put
      作用:用于对单个行执行添加操作
   7.Get
      关系:org.apache.hadoop.hbase.client.Get
      作用:用于获取单个行的相关信息
   8.Result
      关系:org.apache.hadoop.hbase.client.Result
      作用:存储Get或Scan操作后获取的单行值。
   9.ResultScanner
      关系:Interface
      作用:客户端获取值的接口。

二、示例:

 以下是一个完整的代码示例,基于hbase-0.90.3编写
类:HBaseOperation  功能: 创建、删除表,增加、删除、查询记录。
类:HBaseTest       功能: 实例化HBaseOperation,使用其相应方法。

2.1 HBaseOperation源码如下:
  1. package model;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.HColumnDescriptor;
  8. import org.apache.hadoop.hbase.HTableDescriptor;
  9. import org.apache.hadoop.hbase.client.Delete;
  10. import org.apache.hadoop.hbase.client.Get;
  11. import org.apache.hadoop.hbase.client.HBaseAdmin;
  12. import org.apache.hadoop.hbase.client.HTable;
  13. import org.apache.hadoop.hbase.client.Put;
  14. import org.apache.hadoop.hbase.client.Result;
  15. import org.apache.hadoop.hbase.client.ResultScanner;
  16. import org.apache.hadoop.hbase.client.Scan;
  17. public class HBaseOperation {
  18.    //.相关属性
  19.    private  Configuration conf ;
  20.    private  HBaseAdmin admin;
  21.   
  22.    public  HBaseOperation(Configuration conf) throws  IOException{
  23.       this.conf=HBaseConfiguration.create(conf);
  24.       this.admin =new HBaseAdmin(this.conf);
  25.    }
  26.    public HBaseOperation() throws IOException{
  27.       Configuration cnf = new Configuration();
  28.       this.conf=HBaseConfiguration.create(cnf);
  29.       this.admin=new HBaseAdmin(this.conf);
  30.    }
  31.   
  32.    //1.创建表
  33.    public void  createTable(String tableName,String colFamilies[]) throws IOException{
  34.          if(this.admin.tableExists(tableName)){
  35.             System.out.println("Table: "+tableName+" already exists !");
  36.          }else{
  37.             HTableDescriptor dsc = new HTableDescriptor(tableName);
  38.             int len = colFamilies.length;
  39.             for(int i=0;i<len;i++){
  40.                HColumnDescriptor family = new HColumnDescriptor(colFamilies[i]);
  41.                dsc.addFamily(family);
  42.             }
  43.             admin.createTable(dsc);
  44.             System.out.println("创建表成功");
  45.          }
  46.    }
  47.    //2.删除表
  48.    public void deleteTable(String tableName) throws IOException{
  49.          if(this.admin.tableExists(tableName)){
  50.             admin.deleteTable(tableName);
  51.             System.out.println("删除表成功");
  52.          }else{
  53.             System.out.println("Table Not Exists !");
  54.          }
  55.    }
  56.    //3.插入一行记录
  57.    public void insertRecord(String tableName,String rowkey,String family,String qualifier,String value) throws IOException {
  58.   
  59.          HTable table = new HTable(this.conf,tableName);
  60.          Put  put = new Put(rowkey.getBytes());
  61.          put.add(family.getBytes(),qualifier.getBytes(),value.getBytes());
  62.          table.put(put);
  63.         
  64.          System.out.println("插入行成功");
  65.    }
  66.    //4.删除一行记录
  67.    public void deleteRecord(String tableName,String rowkey) throws IOException{
  68.      
  69.          HTable table = new HTable(this.conf,tableName);
  70.          Delete del =new Delete(rowkey.getBytes());
  71.          table.delete(del);
  72.          System.out.println("删除行成功");
  73.    }
  74.    //5.获取一行记录
  75.    public Result getOneRecord(String tableName,String rowkey) throws IOException{
  76.          HTable table =new HTable(this.conf,tableName);
  77.          Get get =new Get(rowkey.getBytes());   
  78.          Result rs = table.get(get);
  79.          return rs;
  80.    }
  81.    //6.获取所有记录
  82.     public List<Result> getAllRecord(String tableName) throws IOException{
  83.                
  84.        HTable table = new HTable(this.conf,tableName);
  85.        Scan scan = new Scan();
  86.        ResultScanner scanner = table.getScanner(scan);
  87.        List<Result> list =new ArrayList<Result>();
  88.         for(Result r:scanner){
  89.           list.add(r);
  90.         }
  91.         scanner.close();
  92.         return list;      
  93.     }
  94. }
复制代码
2.2 HBaseTest源码如下:
  1. package model;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.hbase.KeyValue;
  7. import org.apache.hadoop.hbase.client.Result;
  8. public class HBaseTest {
  9.     public static void main(String[] args) throws IOException {
  10.        // TODO Auto-generated method stub
  11.        System.out.println("hello veagle and serapy  ");
  12. //1.初始化HBaseOperation
  13.       
  14.        Configuration conf = new Configuration();
  15.         //与hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同   
  16.         conf.set("hbase.zookeeper.quorum", "172.21.7.124");
  17.         //与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同  
  18.         conf.set("hbase.zookeeper.property.clientPort", "2181");
  19.        HBaseOperation hbase = new HBaseOperation(conf);
  20. //2.测试相应操作
  21.        //2.1创建表
  22.        String tableName = "blog";
  23.        String colFamilies[]={"article","author"};
  24.        hbase.createTable(tableName, colFamilies);
  25.        //2.2插入一条记录
  26.        hbase.insertRecord(tableName, "row1", "article", "title", "Hadoop");
  27.        hbase.insertRecord(tableName, "row1", "author", "name", "veagle");
  28.       hbase.insertRecord(tableName, "row1", "author", "nickname", "serapy");
  29.        //2.3查询一条记录
  30.        Result rs1 = hbase.getOneRecord(tableName, "row1");
  31.        for(KeyValue kv: rs1.raw()){
  32.            System.out.println(new String(kv.getRow()));
  33.            System.out.println(new String(kv.getFamily()));
  34.            System.out.println(new String(kv.getQualifier()));
  35.            System.out.println(new String(kv.getValue()));
  36.        }
  37.        //2.4查询整个Table
  38.        List<Result> list =null;
  39.        list= hbase.getAllRecord("blog");
  40.        Iterator<Result> it = list.iterator();
  41.       
  42.        while(it.hasNext()){
  43.            Result rs2=it.next();
  44.            for(KeyValue kv : rs2.raw()){
  45.               System.out.print("row key is : " + new String(kv.getRow()));
  46.               System.out.print("family is  : " + new String(kv.getFamily()));
  47.               System.out.print("qualifier is:" + new String(kv.getQualifier()));
  48.               System.out.print("timestamp is:" + kv.getTimestamp());
  49.               System.out.println("Value  is  : " + new String(kv.getValue()));
  50.            }
  51.        }
  52.     }
  53. }
复制代码
2.3 执行结果

插入行成功

插入行成功

插入行成功

row1

article

title

Hadoop

row1

author

name

veagle

row1

author

nickname

serapy

row key is : row1family is  : articlequalifier is:titletimestamp is:1322728761046Value  is  : Hadoop

row key is : row1family is  : authorqualifier is:nametimestamp is:1322728761056Value  is  : veagle

row key is : row1family is  : authorqualifier is:nicknametimestamp is:1322728761060Value  is  : serapy



来自群组: Hadoop技术组

已有(3)人评论

跳转到指定楼层
mavs41 发表于 2014-2-14 23:01:40
果断顶起啊!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条