HBases API

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

import java.io.IOException;

public class HBaseAPI2 {
    private static Connection connection = null ;
    static {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","BigData002,BigData003,BigData004");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        try {
            connection= ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void createTable(String tableName, String ... families) throws IOException {
        Admin admin = connection.getAdmin();
        try {
            if (admin.tableExists(TableName.valueOf(tableName))){
                System.out.println("table:"+tableName+"exists!");
                return;
            }
            HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
            for (String family : families) {
                HColumnDescriptor familyDesc = new HColumnDescriptor(family);
                desc.addFamily(familyDesc);
            }
            admin.createTable(desc);
        }finally {
            admin.close();
        }
    }

    public static boolean tableExist(String tableName) throws IOException {
        Admin admin = connection.getAdmin();
        try {
            return admin.tableExists(TableName.valueOf(tableName));
        } finally {
            admin.close();
        }
    }

    private static void putCompany(String tableName, String rowKey, String family, String column, String value) throws IOException {
        if (!tableExist(tableName)) {

            System.out.println("table:" + tableName + " not exists!");
            return;
        }
        Table table = connection.getTable(TableName.valueOf(tableName));
        try {
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));
            table.put(put);
        } finally {
            table.close();
        }
    }

    private static void getRow(String tableName, String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            byte[] columnBytes = CellUtil.cloneQualifier(cell);
            String columnStr = Bytes.toString(columnBytes);
            byte[] valueBytes = CellUtil.cloneValue(cell);
            String valueStr = Bytes.toString(valueBytes);
            System.out.println(columnStr + ":" + valueStr);
        }
        table.close();
    }

    public static void dropTable(String tableName) throws IOException {
        Admin admin = connection.getAdmin();
        try {
            if (!admin.tableExists(TableName.valueOf(tableName))) {
                System.out.println("table:" + tableName + " not exists!");
                return;
            }
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
        } finally {
            admin.close();
        }
    }

    public static void getAllRows(String tableName) throws IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner resultScanner = table.getScanner(scan);
        for(Result result : resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell : cells){
                System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    public static void deleteRow(String tableName, String rowKey) throws IOException {
        if (!tableExist(tableName)) {
            System.out.println("table:" + tableName + " not exists!");
            return;
        }
        Table table = connection.getTable(TableName.valueOf(tableName));
        try {
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            table.delete(delete);
        } finally {
            table.close();
        }
    }

    public static void main(String[] args) throws IOException {
//        createTable("company","info","data");
//        putCompany("company", "001", "info", "name", "Baidu");
//        putCompany("company", "001", "data", "city", "Beijing");
//        putCompany("company", "002", "info", "name", "Tencent");
        putCompany("company", "002", "data", "city", "Shenzhen");
//        putCompany("company", "003", "info", "name", "HUAWEI");
//        putCompany("company", "003", "data", "city", "Dongguan");
//        getAllRows("company");
//        getRow("company", "001");
//        deleteRow("company", "002");
        dropTable("company");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值