用java操作Hbase api

package com.ygy.demo;

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

import java.io.IOException;

public class test1 {
    public static void main(String[] args) throws IOException {
        //创建
        createTable("company","info","data");
        //添加
        putCell("company","001","info","name","Baidu");
        putCell("company","001","data","city","Beijing");

        putCell("company","002","info","name","Tencent");
        putCell("company","002","data","city","Shenzhen");

        putCell("company","003","info","name","HUAWEI");
        putCell("company","003","data","city","Dongguan");

        //查询所有
        getTableAll("company");
        //使用API查询company表中行键值为“001”的数据
        getRowKey("company","001");
        //使用API删除company表中行键值为“002”的数据。
        deleteRowKey("company","002");
        dropTable("company");


    }
    private static Connection connection =null;
    static {
        HBaseConfiguration conf = new HBaseConfiguration();
        conf.set("hbase.zookeeper.quorum","hdp001,hdp002,hdp003");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建表
     * @param tableName 表名
     * @param familes 列族
     * @throws IOException
     */
    public static void createTable(String tableName,String... familes) throws IOException {
        Admin admin = connection.getAdmin();
        try {
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            for (String famile : familes) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(famile);
                descriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(descriptor);
            System.out.println("创建成功");
        } finally {
            admin.close();
        }
    }

    /**
     * 添加数据
     * @param tableName 表名
     * @param rowKey    rowKey
     * @param family    族
     * @param column    列
     * @param value 值
     * @throws IOException
     */
    public static void putCell(String tableName,String rowKey,String family,String column,String value) throws IOException {
        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);
            System.out.println("添加成功");
        }finally {
            table.close();
        }
    }

    /**
     * 获取所有数据
     * @param tableName
     * @throws IOException
     */
    public static void getTableAll(String tableName) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                //rowKey
                byte[] row = cell.getRow();
                String strRow = Bytes.toString(row);
                //value
                byte[] value = cell.getValue();
                String strValue = Bytes.toString(value);
                //输出
                System.out.println(strRow+" : "+strValue );
            }
        }
        scanner.close();
        table.close();
    }

    /**
     * 辊距rowKey获取数据
     * @param tableName 表名
     * @param rowKey    rowKey
     * @throws IOException
     */
    public static void getRowKey(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);
            for (Cell cell : result.rawCells()) {
                //rowKey
                byte[] row = cell.getRow();
                String strRow = Bytes.toString(row);
                //value
                byte[] value = cell.getValue();
                String strValue = Bytes.toString(value);
                //输出
                System.out.println(strRow+" : "+strValue );
            }
        table.close();
    }

    /**
     * 删除一行
     * @param tableName 表名
     * @param rowKey    RowKey
     * @throws IOException
     */
    public static void deleteRowKey(String tableName,String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        try {
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            table.delete(delete);
            System.out.println("删除一行成功");
        } finally {
            table.close();
        }
    }

    /**删除表
     * @param tableName 表名
     * @throws IOException
     */
    public static void dropTable(String tableName) throws IOException {
        Admin admin = connection.getAdmin();
        try {
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("删除表成功");
        } finally {
            admin.close();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值