Hbase Java API程序设计步骤

http://www.it165.net/admin/html/201407/3390.html

步骤1:创建一个Configuration对象
   包含了客户端链接Hbase服务所需的全部信息:
   zookeeper位置(我们只有链接到zookeeper才能与hbase通信,master仅负责负载均衡等) ,
   zookeeper链接超时时间


包含各种配置信息,hbase server zookeeper 访问地址和端口号等。
Configutation conf = HbaseConfiguration.create();
create()函数的内部逻辑:
从classpath中加载hbase-default.xml和hbase-site.xml两个文件
hbase-defaut.xml已经打包到Hbase jar包中
hbase-site.xml需要添加到class path中
hbase-site.xml将覆盖hbase-default.xml中的同名属性。

hbase如何找到并从classpath中获取hbase-site.xml信息 ====》如何检查hbase-site.xml已经在hadoop classpath中了呢? 运行 hadoop classpath | grep hbase
1 修改hadoop脚本,将Hbase classpath加入
2 在<hadoop_install>/conf/hadoop-env.sh中设置:
export HADOOP_CLASSPATH=$HBASE_HOME/*:$HBASE_HOME/conf:$HADOOP_CLASSPATH 两个目录放到hadoop的conf的hadoop.env.sh中

如果已经有了一个Configuration文件,可进行如下操作:
Configuration newconf = Configuration.create(existingConf);
用户自定义的配置文件将在已有配置文件之后加载,将覆盖hbase-default.xml hbase-site.xml中的配置

    create
    public static org.apache.hadoop.conf.Configuration create()
    Creates a Configuration with HBase resources
    Returns:
        a Configuration with HBase resources
create public static org.apache.hadoop.conf.Configuration create(org.apache.hadoop.conf.Configuration that) Parameters: that
- Configuration to clone. Returns: a Configuration created with the hbase-*.xml files plus the given configuration.

没有直接从路径添加的构造函数,但是可以构造一个 org.apache.hadoop.conf.Configuration
  /**
   * Add a configuration resource.
   *
   * The properties of this resource will override properties of previously
   * added resources, unless they were marked <a href="#Final">final</a>.
   *
   * @param file file-path of resource to be added, the local filesystem is
   *             examined directly to find the resource, without referring to
   *             the classpath.
   */
  public void addResource(Path file) {
    addResourceObject(new Resource(file));
  }

   可单独覆盖某一个或多个参数值

   Configuration conf = HbaseConfiguration.create();
   conf.set("hbase.zookeeper.quorum", "node1, node2"); 但通常不推荐这么做。 因为需要重新打包,不方便,不如放到配置文件中。

 


步骤2:创建一个HTable句柄
   提供Configuration对象
提供待访问Table名称
HTable table = new HTable(conf, tabName);

》一个table对应一个Htable句柄 这个句柄在org.apache.hadoop.hbase.client.HTable
》提供了CRUD操作 create read update del
》提供行级事务,
不支持多行事务或者表级别事务
严格的行一致性
并发读,顺序写。

创建HTable句柄代价很大
1 扫描.META.表
2 创建一次,以后尽可能复用
3 如果需要创建多个HTable句柄,使用HTableTool

HTable并非线程安全的,一个线程创建一个即可。
Htable支持CRUD批处理,非线程安全,仅是为了提高性能。





步骤3:执行相应的操作
put,get,delete,scan等
table.getTableName();
步骤4:关闭Htable句柄 【句柄不关,会发生内存泄露】
将内存数据刷新到磁盘上。
释放各种资源。
table.close()
package com.jlc.hadoop.hbase.example;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class PutTest {

    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        HTable hTable = new HTable(conf, "tab1");
        System.out.println(Bytes.toString(hTable.getTableName()));
        
        //步骤一,创建一个put对象
        Put put = new Put(Bytes.toBytes("row1"));
        
        //设置cell值
        //Put.add(family, column, value)
        //Put.add(family, column, timestamp, value)
        //Put.add(KeyValue kv)  KeyValue 事实上就是封装了 上面四个,看起来简单
        
        
        byte[] family = Bytes.toBytes("fam1");
        byte[] qualifier = Bytes.toBytes("col1");
        byte[] value = Bytes.toBytes("val2");;
        put.add(family, qualifier, value);
        
        hTable.put(put);    
        
        hTable.close();
        
//        hbase(main):003:0> scan 'tab1'
//        ROW                   COLUMN+CELL                                               
//         row1                 column=fam1:col1, timestamp=1395796780021, value=val1     
//         row2                 column=fam1:col1, timestamp=1398305517331, value=val2     
//        2 row(s) in 0.0090 seconds
//      若row1 fam1 col1 已经存在,则   scan的时候 显示最新时间戳的那个值,其他的如何获取呢???
//        hbase(main):004:0> scan 'tab1'
//        ROW                   COLUMN+CELL                                               
//         row1                 column=fam1:col1, timestamp=1398305623182, value=val2     
//         row2                 column=fam1:col1, timestamp=1398305517331, value=val2  
        
    }

}
package com.jlc.hadoop.hbase.example;

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.conf.Configuration;

public class ConstructHTable {

    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        HTable hTable = new HTable(conf, "tab1");
        System.out.println(Bytes.toString(hTable.getTableName()));
        hTable.close();
    }

}
package com.jlc.hadoop.hbase.example;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class ReadTest {
    
    public static void print(Result result){
        System.out.println("-----------------------------");
        System.out.println("RowId:" + Bytes.toString(result.getRow()));
        byte[] val1 = result.getValue(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
        System.out.println("fam1:col1=" + Bytes.toString(val1));
        byte[] val2 = result.getValue(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
        System.out.println("fam1:col1=" + Bytes.toString(val2));
    }
    
    
    public static void scan(HTable hTable, String startrow,String stoprow) throws IOException{
        System.out.println("scaning from " + startrow + " to " + stoprow);
        
        Scan scan = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(stoprow));  // 这个限定了row 但怎么限定列呢
        //scan.addColumn(family, qualifier)
        scan.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));   // 这个就指明我们仅仅取某个列就行了
        ResultScanner scanner = hTable.getScanner(scan);    //获取scanner句柄
        for (Result result : scanner) {
//            byte[] value = result.getValue(family, qualifier)
            byte[] value = result.getValue(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
            System.out.println(Bytes.toString(result.getRow()) + "=>" + Bytes.toString(value));
        }
        scanner.close();//  这个不要忘记哦
    }
    

    public static void main(String[] args) throws IOException {

        /*
         * 支持的API类型 通过rowkey获取一行数据 通过rowkey集合获取多条记录 扫描整个表或者表的一部分
         * 
         * 扫描表:可指定扫描的范围,[startkey, endkey] 表中数据是按照rowkey排序的
         * 
         * 读取时的注意事项: 1 只读取需要的数据
         * [比方我只读取某个columnfamliy里某一个column的数据,指定这一列就行了,不要都读取出来,浪费io] 2
         * 尽可能的增加数据约束条件 3 可增加family, column(s) time range 和 max
         * versions【我要返回多少个版本号】等约束条件
         * 
         * 接口实例:get.setTimeRange(minStamp, maxStamp) 这个就是增加时间范围, time range
         * get.setMaxVersions(maxVersions)get.addFamily(family) 只后去众多familys
         * 中的一个familyget.addColumn(family, column)
         * 只获取某个family的众多columen中的一个column的数据
         */
        
        Configuration conf = HBaseConfiguration.create();
        HTable hTable = new HTable(conf, "tab1");
        System.out.println(Bytes.toString(hTable.getTableName()));
        
//        byte[] family = Bytes.toBytes("fam1");
//        byte[] qualifier = Bytes.toBytes("col1");
//        byte[] value = Bytes.toBytes("val2");;
        
        
        
        // get example
//        Get get = new Get(Bytes.toBytes("row1"));
//        Result result = hTable.get(get);
//        print(result);
        
        //有时候我们不需要整个row里的所有数据,我们可以加以限制 [推荐使用这种限制的]
//        get.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("col1"));
//        result = hTable.get(get);
//        print(result);
        
//        keyvalues={row1/fam1:col1/1398305623182/Put/vlen=4/mvcc=0}
//        keyvalues={row1/fam1:col1/1398305623182/Put/vlen=4/mvcc=0}
        
        //delete example
        //Delete delete = new Delete(Bytes.toBytes("rowtodel"));
        //hTable.delete(delete);

        //Delete delete1 = new Delete(Bytes.toBytes("another row"));
        //delete1.deleteColumn(family, qualifier)
        //hTable.delete(delete1);
        
        //scan example
        scan(hTable, "row1", "row3"); //前闭 后开的  要得道前两行,需要指定到第三行
        hTable.close();
        
    }

}

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值