hbase developer API 1.22版

最近用hbase1.22搭了套集群环境,发现之前的测试代码虽然还都能用,但是好多的老的方法,已经是不建议使用了。新的代码在下面给贴出来,我把相对于老版本api,有改动的部分用红色字体标注了出来。

package com.woozoom.hbase.test;

import java.io.IOException;
import java.util.*;

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

public class BaseOperation {

    private static final String TABLE_NAME = "demo_table";

    public static Configuration conf = null;
    public HTable table = null;
    public HBaseAdmin admin = null;

    static {
        conf = HBaseConfiguration.create();
    }

    /**
     * 创建一张表
     */
    public static void creatTable(String strTN, String[] familys)
            throws Exception {
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        TableName tn = TableName.valueOf(strTN);
        if (admin.tableExists(tn)) {
            System.out.println("table already exists!");
        } else {

            HTableDescriptor tableDesc = new HTableDescriptor(tn);
            for (String colFamily : familys) {
                tableDesc.addFamily(new HColumnDescriptor(colFamily));
            }
            admin.createTable(tableDesc);
            System.out.println("create table " + strTN + " ok.");
        }
    }

    /**
     * 删除表
     */
    public static void deleteTable(String strTN) throws Exception {
        try {
            Connection conn = ConnectionFactory.createConnection(conf);
            Admin admin = conn.getAdmin();
            TableName tn = TableName.valueOf(strTN);
            admin.disableTable(tn);
            admin.deleteTable(tn);
            System.out.println("delete table " + strTN + " ok.");
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        }
    }

    /**
     * 插入一行记录
     */
    public static void addRecord(String strTN, String rowKey,
                                 String family, String qualifier, String value) throws Exception {
        try {
            Connection conn = ConnectionFactory.createConnection(conf);
            TableName tn = TableName.valueOf(strTN);
            Table table = conn.getTable(tn);

            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),
                    Bytes.toBytes(value));
            table.put(put);
            System.out.println("insert recored " + rowKey + " to table "
                    + strTN + " ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一行记录
     */
    public static void delRecord(String strTN, String rowKey)
            throws IOException {
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tn = TableName.valueOf(strTN);
        Table table = conn.getTable(tn);
        List list = new ArrayList();
        Delete del = new Delete(rowKey.getBytes());
        list.add(del);
        table.delete(list);
        System.out.println("del recored " + rowKey + " ok.");
    }

    /**
     * 查找一行记录
     */
    public static void getOneRecord(String strTN, String rowKey)
            throws IOException {
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tn = TableName.valueOf(strTN);
        Table table = conn.getTable(tn);
        Get get = new Get(rowKey.getBytes());
        Result rs = table.get(get);
        for (Cell cell : rs.listCells()) {
            KeyValue kv = (KeyValue)cell;
            System.out.println("### " + new String(kv.getKey()) + " ###");
            System.out.println("### " + new String(Arrays.copyOfRange(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyOffset() + cell.getFamilyLength())) + " ###");
            System.out.println("### " + new String(Arrays.copyOfRange(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierOffset() + cell.getQualifierLength())) + " ###");
            System.out.println("### " + new String(Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueOffset() + cell.getValueLength())) + " ###");
            System.out.println("### " + cell.getTimestamp() + " ###");
        }

        NavigableMap<byte[], NavigableMap<byte[], byte[]>> nm = rs.getNoVersionMap();
        for(byte[] family : nm.keySet()){
            System.out.println(new String(family));
            NavigableMap<byte[], byte[]> nmSon = nm.get(family);
            for (byte[]  qualifier : nmSon.keySet()){
                System.out.println(new String(qualifier));
                byte[] value = nmSon.get(qualifier);
                System.out.println(new String(value));
            }
        }

    }

    /**
     * 显示所有数据
     */
    public static void getAllRecord(String strTN) {
        try {
            Connection conn = ConnectionFactory.createConnection(conf);
            TableName tn = TableName.valueOf(strTN);
            Table table = conn.getTable(tn);
            Scan s = new Scan();
            ResultScanner ss = table.getScanner(s);
            for (Result r : ss) {
                for (Cell cell : r.listCells()) {
                    KeyValue kv = (KeyValue)cell;
                    System.out.print(new String(kv.getKey()) + " ");
                    System.out.print(new String(Arrays.copyOfRange(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyOffset() + kv.getFamilyLength())) + ":");
                    System.out.print(new String(Arrays.copyOfRange(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierOffset() + kv.getQualifierLength())) + " ");
                    System.out.print(kv.getTimestamp() + " ");
                    System.out.println(new String(Arrays.copyOfRange(kv.getValueArray(), kv.getValueOffset(), kv.getValueOffset() + kv.getValueLength())));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] agrs) {
        try {
            String tablename = "scores1";
            String[] familys = {"grade", "course"};
            BaseOperation.creatTable(tablename, familys);

            // add record zkb
            BaseOperation.addRecord(tablename, "zkb", "grade", "", "5");
            BaseOperation.addRecord(tablename, "zkb", "course", "", "90");
            BaseOperation.addRecord(tablename, "zkb", "course", "math", "97");
            BaseOperation.addRecord(tablename, "zkb", "course", "art", "87");
            // add record baoniu
            BaseOperation.addRecord(tablename, "baoniu", "grade", "", "4");
            BaseOperation
                    .addRecord(tablename, "baoniu", "course", "math", "89");

            System.out.println("===========get one record========");
            BaseOperation.getOneRecord(tablename, "zkb");

            System.out.println("===========show all record========");
            BaseOperation.getAllRecord(tablename);

            System.out.println("===========del one record========");
            BaseOperation.delRecord(tablename, "baoniu");
            BaseOperation.getAllRecord(tablename);

            System.out.println("===========show all record========");
            BaseOperation.getAllRecord(tablename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

转载于:https://my.oschina.net/dongtianxi/blog/726901

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值