Hbase 基本操作总结

注意事项:
HBaseAdmin 使用完毕一定要主动关闭
HTable 使用完毕要主动关闭

package hbasebasicoperation;
/*
* @author: wjf
* @version: 2016年4月9日 下午7:52:41
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
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.Get;
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;

public class BasicOperation {
    private static Configuration conf = null;
    private static HBaseAdmin admin = null;
    static {
        Configuration hconf = new Configuration();
        // this set is not necessary,hase-site.xml is included to the classpath
        hconf.set("hbase.zookeeper.quorum", "master,slave01");
        hconf.set("hbase.zookeeper.property.clientPort", "2182");
        conf = HBaseConfiguration.create(hconf);
        try {
             admin = new HBaseAdmin(conf);
        } catch (MasterNotRunningException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void createTable(String tableName, String[] family) throws Exception {
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        } else {
            HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
            for (int i = 0; i < family.length; i++) {
                tableDesc.addFamily(new HColumnDescriptor(family[i]));
            }
            admin.createTable(tableDesc);
            System.out.println("create table " + tableName + "ok");

        }
    }

    public static void deleteTable(String tableName) {
        try {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void addRecord(String tableName,String rowKey,String family,String qualifer,String value){
        try {
            HTable table=new HTable(conf, tableName);
            Put put=new Put(rowKey.getBytes());
            put.add(family.getBytes(),qualifer.getBytes(),value.getBytes());
            table.put(put);
            table.close();
            System.out.println("insert recorded "+ rowKey+" to table "+ tableName+ "ok");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void delRecord(String tableName,String rowKey) throws IOException{
        HTable table=new HTable(conf,tableName);
        List list=new ArrayList();
        Delete del=new Delete(rowKey.getBytes());
        list.add(del);
        table.delete(list);
        System.out.println("del record "+rowKey +"ok");
        table.close();
    }

    public static void findOne(String tableName,String rowKey) throws IOException{
        HTable table=new HTable(conf,tableName);
        Get get=new Get(rowKey.getBytes());
        Result rs=table.get(get);
        for(Cell kv:rs.rawCells()){
            System.out.println(new String(CellUtil.cloneRow(kv)));
            System.out.println(new String(CellUtil.cloneFamily(kv)));
            System.out.println(new String(CellUtil.cloneQualifier(kv)));
            System.out.println(new String(CellUtil.cloneValue(kv)));
        }
        table.close();
    }
    public static void findAllRecord(String tableName){
        HTable table;
        try {
            table = new HTable(conf,tableName);
            Scan s=new Scan();
            ResultScanner ss=table.getScanner(s);
            for(Result result:ss){
                for(Cell cell:result.rawCells()){
                    System.out.println("this is a new record-------------");
                    System.out.println(new String(CellUtil.cloneRow(cell)));
                    System.out.println(new String(CellUtil.cloneFamily(cell)));
                    System.out.println(new String(CellUtil.cloneQualifier(cell)));
                    System.out.println(new String(CellUtil.cloneValue(cell)));
                }
            }
            table.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }

        //
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        String tableName="scores";
        String[] familys={"grade","course"};
        try {
            BasicOperation.createTable(tableName, familys);

            BasicOperation.addRecord(tableName, "zkb", "grade","", "5");
            BasicOperation.addRecord(tableName, "zkb", "course", "math", "90");
            BasicOperation.addRecord(tableName, "zkb", "course", "art", "100");


//          BasicOperation.findOne(tableName, "zkb");
            BasicOperation.findAllRecord(tableName);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("method in main");
        try {
            admin.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      BasicOperation obj=new BasicOperation();


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值