HbaseAPI编写

2022年6月14日11:59:38 发表评论 1,138 views
package hbaseapi;

import com.google.inject.internal.util.$AsynchronousComputationException;
import com.sun.tools.internal.xjc.reader.xmlschema.BindYellow;
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 sun.rmi.transport.tcp.TCPConnection;

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

public class HbaseTest {
    static Configuration conf;
    static Connection connection = null;
    static HBaseAdmin admin = null;
    static {
        try {
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum","bigdata166");
            conf.set("hbase.zookeeper.property.clientport","2181");
            connection = ConnectionFactory.createConnection(conf);
            admin = (HBaseAdmin) connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断表是否存在
      * @param tableName
     * @return
     * @throws IOException
     */

    public static boolean isTableExist(String tableName) throws IOException {
        return admin.tableExists(tableName);
    }

    /**
     * 列族建议小于3个
     * @param tableName
     * @param columnFamily
     * @throws Exception
     */
    public static void createTable(String tableName,String... columnFamily)throws Exception{
        if (isTableExist(tableName)) {
            System.out.println("表已经存在");
           }else{
                //表描述器
                HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
                for (String cf : columnFamily) {
                    HColumnDescriptor hcd = new HColumnDescriptor(cf);
                    desc.addFamily(hcd);
                }
                admin.createTable(desc);
                System.out.println("表创建成功"+tableName);
            }
    }
    public static void droptable(String tableName) throws Exception{
        if (isTableExist(tableName)){
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println("表已删除");
        }else{
            System.out.println("表不存在");
        }
    }

    public static void addRowData(String tableName,String rowKey,String columnFamily,String column,String value) throws IOException {
        if (isTableExist(tableName)){
//        HTable hTable = new HTable(conf, tableName);
            Table hTable = connection.getTable(TableName.valueOf(tableName)); //  新API,老API代码量多的时候可能出现线程不安全
            // 往put中放入数据
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column),Bytes.toBytes(value));//第一种方法
//        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),Bytes.toBytes(value)); //第二种方法
        hTable.put(put);
        hTable.close();
            System.out.println("数据插入成功");}else{
            System.out.println("表不存在");
        }
    }

    public static void deleteMultiRow(String tableName,String... rows) throws IOException {
//        HTable hTable = new HTable(conf, tableName);
        Table hTable = connection.getTable(TableName.valueOf(tableName)); //  新API,老API代码量多的时候可能出现线程不安全
        List<Delete> deleteList = new ArrayList<>(); // 用于打包删除
        for (String row : rows) {
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
            System.out.println("删除行成功");
        }
        hTable.delete(deleteList);
        hTable.close();


    }

    public static void getAllRows(String tableName) throws IOException {
//        HTable hTable = new HTable(conf, tableName);
        Table hTable = connection.getTable(TableName.valueOf(tableName)); //  新API,老API代码量多的时候可能出现线程不安全
        Scan scan = new Scan();
        ResultScanner resultScanner= hTable.getScanner(scan);
        for (Result result : resultScanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("RK:"+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)));
            }
        }
    }

///////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 获得某一行数据
     *
     * @param tableName
     * @param rowKey
     * @throws IOException
     */
    public 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);
        for (Cell cell : result.rawCells()) {
            System.out.println("行键:" + Bytes.toString(result.getRow()));
            System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:" + cell.getTimestamp());
        }
    }

    /**
     * 获得某一个value的值
     *
     * @param tableName
     * @param rowKey
     * @throws IOException
     */
    public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));

        Result result = table.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println("行键:" + Bytes.toString(result.getRow()));
            System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:" + cell.getTimestamp());
        }
    }

    /**
     * 初始化命名空间
     *
     * @param namespace 命名空间的名字
     * @throws Exception
     */
    public static void initNameSpace(String namespace) throws Exception {

        //命名空间描述器
        NamespaceDescriptor nd = NamespaceDescriptor
                .create(namespace)
                .addConfiguration("AUTHOR", "andy")
                .build();
        //通过admin对象来创建命名空间
        admin.createNamespace(nd);
        System.out.println("已初始化命名空间");
        //关闭两个对象
        close(admin, connection);
    }

    /**
     * 关闭admin对象和connection对象
     *
     * @param admin      关闭admin对象
     * @param connection 关闭connection对象
     * @throws IOException IO异常
     */
    private static void close(Admin admin, Connection connection) throws IOException {
        if (admin != null) {
            admin.close();
        }
        if (connection != null) {
            connection.close();
        }
    }




    public static void main(String[] args) throws Exception {
//        boolean flage = isTableExist("student");
//        System.out.println(flage);

//        createTable("ShallTable2","cf1","cf2","cf3");
//        droptable("Shalltable2");
        addRowData("student","1007","cf1","xinAPIideacolumn2","这是来自idea的数据");
        //插入不存在的列族名,会报错
//        addRowData("student","1006","cf2","ideacolumn","这是来自idea的数据");
//        deleteMultiRow("student","1006");
        getAllRows("student");
    }
}


发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: