HBase的基本api操作及简要说明

hbase的jar包

HBase-0.98.6源码和安装包下载
这里用的是hbase-0.98.6-hadoop2-bin.tar.gz ,直接下载解压后将对应的lib下的包导入项目即可,这里只是测试多一点,实际项目需要自己剔除一些不需要的jar包

hbase的java api

因为在代码里新增了注释,就不一一说明,下面直接上代码


import java.io.IOException;
import java.util.Map.Entry;
import java.util.NavigableMap;
import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class HBaseDemo {
    Configuration conf =null;
    HBaseAdmin admin =null;

    private long timestamp = HConstants.LATEST_TIMESTAMP;  
    static final long LATEST_TIMESTAMP = Long.MAX_VALUE;  
    @Before
    public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
        //初始化操作打开admin,ddl命令需要一个HBaseAdmin实例,所有的操作都需要HBaseConfiguration实例
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hddata1,hddata2,hddata3");
        admin =new HBaseAdmin(conf);
    }
    @Test
    public void createTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

        HTableDescriptor htb = new HTableDescriptor(TableName.valueOf("useraa"));
        HColumnDescriptor htc_info =new HColumnDescriptor("info");
        HColumnDescriptor htc_data =new HColumnDescriptor("data");
        htc_info.setMaxVersions(5);
        htb.addFamily(htc_info);
        htb.addFamily(htc_data);
        admin.createTable(htb);
        //admin.close();    //放到了单元测试的@after方法中

    }
    @Test
    public void dropTable()throws Exception{
        admin.disableTable("useraa");
        admin.deleteTable("useraa");


    }
    @Test
    public void alterTable() throws IOException{
        HColumnDescriptor htc = new HColumnDescriptor("testColumn");
        //新增一个列簇testColumn,其他属性都为默认值
        admin.addColumn("useraa", htc);

    }

    /**
     * 数据的插入,可以用批量插入,批量插入的时候都放进put里面提交即可
     * */
    @Test
    public void putRow() throws IOException{

        HTable table =new HTable(conf, TableName.valueOf("people"));
        Put put=  new Put(Bytes.toBytes("Java00001"));
        //同时处理两个cell,put会把其他类型转换成byte[]
        put.add(Bytes.toBytes("data"), Bytes.toBytes("size"),this.timestamp, Bytes.toBytes("data2"));
        put.add(Bytes.toBytes("data"), Bytes.toBytes("nation"),this.timestamp, Bytes.toBytes("中国"));

        table.put(put);
        //table.flushCommits(); //一个可选的选项,显式提交将数据刷新到远程服务器端
        table.close();

    }

    @Test   
    public void deleteCol() throws IOException{ //删除数据,其实是删除数据的标记,只有在刷新之后数据才会消失
        HTable ht = new HTable(conf, "people");
        Delete del = new Delete(Bytes.toBytes("rk0001"));
        del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("hello"));// 删除 data:hello列
        del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));//删除info:name列
        ht.delete(del);                                                 //提交删除信息
        ht.close();
        //如果没有成功会有一个delete的List<Delete>,可以通过delete.size查看未成功的,为成功的包括未扫描到的
        System.out.println(del.size());

    }
    /**
     * 数据的查询,按照key查询、全表扫描,这里未实现Filter
     * */

    @SuppressWarnings("deprecation")
    @Test
    public void getRowKey() throws IOException{
        HTable ht=new HTable(conf, "people");
        Get get=new Get(Bytes.toBytes("Java00001"));
        get.setMaxVersions(5);
        Result rs =ht.get(get);
        System.out.println("-----------方法一  keyValue获取数据---------------");
        for(KeyValue kv : rs.list()){           //keyvalue获取数据
            String family = new String(kv.getFamilyArray(),kv.getFamilyOffset(),kv.getFamilyLength());
            System.out.println(family+":"+family);
            System.out.println(kv.getTimestamp()+"  "+new String(kv.getValue(),"utf-8"));  //都转换为utf-8,中文会乱码
        }

        // NavigableMap<列簇,NavigableMap<列名,Navigable<时间戳,列值>>>
        NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map=rs.getMap();
        System.out.println("------------方法二 NavigableMap获取数据---------------");
        for(byte[] family:map.keySet()){    

            NavigableMap<byte[], NavigableMap<Long, byte[]>> familyMap = map.get(family);//列簇作为key获取其中的列相关数据

            for(byte[] column:familyMap.keySet()){                              //根据列名循坏
                System.out.println(new String(family)+":"+new String(column));
                NavigableMap<Long, byte[]> valuesMap = familyMap.get(column);

                for(Entry<Long, byte[]> s:valuesMap.entrySet()){                //获取列对应的不同版本数据,默认最新的一个
                    System.out.println(s.getKey() +"   "+new String(s.getValue(),"utf-8"));
                }
            }
        }
        System.out.println(new String(rs.getValue(Bytes.toBytes("info"), Bytes.toBytes("size"))));
        ht.close();
    }

    @SuppressWarnings("deprecation")
    @Test
    public void scanRow() throws IOException{
        HTable ht=new HTable(conf, "people");
        Scan  scan = new Scan();
        scan.setSmall(true);
        ResultScanner scanner = ht.getScanner(scan);
        System.out.println("-----------已经废弃的方法list和raw-----");
        for(Result r : scanner){
            System.out.println("------"+new String(r.getRow())+"--------");
            for(KeyValue kv : r.list()){
                String family = new String(kv.getFamily());
                System.out.println(family+":"+new String(kv.getQualifier()));
                System.out.println(kv.getTimestamp()+"  "+new String(kv.getValue(),"utf-8"));
            }


        System.out.println("-----------cell[]进行扫描-----");
        for(Result rs : scanner){
            for(Cell cell:rs.rawCells()){
                 String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                 String family= Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
                 String column= Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                 String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());

                 System.out.println(row+"\n---"+family+":"+column+"\n        "+value);
            }
          }
        }
    }

    @After
    public void end() throws IOException{
        admin.close();
    }

}

进阶

以上只是一些简单的增删改查,那么更深入的可以对hbase的过滤器的使用、rowkey的设计、hbase结合mapreduce的实现,比如对hdfs上的文件进行更新、读取等操作

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hbase 基本操作类 static { //此处可以使用hbase的配置文件,也可以通过代码来实例化hbase连接 /* * Configuration HBASE_CONFIG = new Configuration(); * HBASE_CONFIG.set("hbase.zookeeper.quorum", "10.229.171.45"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("hbase.zookeeper.quorum","10.233.92.85,10.233.92.86,10.233.92.88"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("zookeeper.znode.parent", "/hbase-unsecure"); conf = HBaseConfiguration.create(HBASE_CONFIG); */ conf = HBaseConfiguration.create(); if("kerberos".equals(conf.get("hbase.security.authentication"))){ try { //设置hadoop.security.authentication为kerberos conf.set("hadoop.security.authentication", "kerberos"); //获取kerberos配置文件路径(krb为kerberos配置文件) String krbStr=Thread.currentThread().getContextClassLoader().getResource("krb").getFile(); //获取用户票据hezhong路径(hezhong为给合众分配的用户配置文件) String keyStr=Thread.currentThread().getContextClassLoader().getResource("jdzy").getFile(); //初始化配置文件 System.setProperty("java.security.krb5.conf", krbStr); //使用用户hezhong登录 UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab("jdzy/f04345e6-70c1-448a-9bbb-4ac6b4c0109b@POLICE.COM", keyStr); } catch (IOException e) { e.printStackTrace(); } } tablePool = new HTablePool(conf, poolsize); logger.debug("create hbase connection success"); // System.out.println("create hbase connection success"); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值