通过Java Api对Hbase进行操作

HBase提供了Java Api的访问接口,所以可以通过java实现HBase中的各种操作
表结构见上一个博客这里写链接内容

package GID.AID;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.hadoop.conf.Configuration;
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.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.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class HBase {
    private static Configuration conf = null;
    private static HBaseAdmin admin = null;
    private static Logger log = Logger.getLogger(App.class);
    private static String hTableName = "hbase_1102";
    private static String[] families = {"cf1", "cf2"};

    @SuppressWarnings("deprecation")
    public static void main( String[] args ) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
        // 初始化log模块,这是hbase-client的依赖带来的额外工作
        initLog();
        // 设定conf,指定ZooKeeper的位置
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "vm10-0-0-2.ksc.com");
        // 建立一个角色,登陆到ZooKeeper上
        admin = new HBaseAdmin(conf);
        log.info("Log in");

        // Ex2 使用表存在、删除
        doExercise2();
        // Ex3 createTable
        doExercise3();
        // Ex4读写数据
        doExercise4();
        //删除一条数据
        doExercise5();
        //查询所有数据
        doExercise6();
        // 关闭连接——很重要,不然服务会挂
        admin.close();
    }

    //查找所有数据
    private static void doExercise6() throws IOException {
    //插入一条数据
    if (tableExist(hTableName)){
        getAllRow(hTableName);
    }
    }
    //删除一条数据
    private static void doExercise5() throws IOException {
        //插入一条数据
        if (tableExist(hTableName)){
            deleteRow(hTableName, "003");
        }
    }

    //添加一条数据
    private static void doExercise4() throws IOException {
        //插入一条数据
        if (tableExist(hTableName)){
            createData(hTableName, "003", "cf1", "name", "Curry");
        }
    }

    private static void doExercise2() throws IOException {
        Boolean bExist = admin.tableExists(hTableName);
        log.info("Table Exists? " + bExist);
        if (true == bExist){
            admin.disableTable(hTableName);
            admin.deleteTable(hTableName);
        }
    }

    private static void doExercise3() throws IOException{
        if (tableExist(hTableName)){
            return;
        }
        createTable(hTableName, families);
    }

    private static boolean tableExist(String tableName) throws IOException{
        return admin.tableExists(hTableName);   
    }

    private static void createTable(String tableName, String[] families) throws IOException {
        @SuppressWarnings("deprecation")
        HTableDescriptor desc = new HTableDescriptor("hbase_1102_chenweican");
        for (int iFamily = 0; iFamily < families.length; iFamily++){
            HColumnDescriptor columnFamilyDesc = new HColumnDescriptor(families[iFamily]);
            desc.addFamily(columnFamilyDesc);
        }
        admin.createTable(desc);
    }

    //添加一条数据
    private static void createData(String tableName,String rowKey,String columnFamily, 
        String column, String value) throws IOException{
        Put put = new Put(Bytes.toBytes(rowKey));
        HTable table = new HTable(conf, tableName);
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        table.put(put);
    }

    //删除一条数据
    private static void deleteRow(String tableName,String rowKey) throws IOException{
        HTable table = new HTable(conf, tableName);
        Delete del = new Delete(Bytes.toBytes(rowKey));
        table.delete(del);
    }

    private static void getAllRow(String tableName) throws IOException {
        HTable table = new HTable(conf, tableName);
        Scan scan = new Scan();
        ResultScanner results = table.getScanner(scan);
        for(Result result:results){
            for(KeyValue rowKV : result.raw()){
                 System.out.print("行名:" + new String(rowKV.getRow()) + " ");
                 System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
                 System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
                 System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
                 System.out.println("值:" + new String(rowKV.getValue()));
            }
        }
    }

    private static void initLog(){
        BasicConfigurator.configure();
        log.setLevel(Level.INFO);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值