HBase API 操 作

依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atlxl</groupId>
    <artifactId>Hbase01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>


        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.5</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.5</version>
        </dependency>

    </dependencies>

</project>

代码:

package com.atlxl;

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;

import static com.sun.xml.internal.fastinfoset.alphabet.BuiltInRestrictedAlphabets.table;

public class TestHbase {

    private static Admin admin = null;
    private static Connection connection = null;
    private static Configuration configuration =null;

    static {
        //Hbase配置文件
//        HBaseConfiguration configuration = new HBaseConfiguration();

        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.192.102");

        //获取连接对象
        try {
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private static void close(Connection conn,Admin admin) {

        if (conn!=null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (admin!=null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //判断表是否存在
    public static boolean tableExist(String tableName) throws IOException {



        //获取HBase管理员对象
//        HBaseAdmin admin = new HBaseAdmin(configuration);

        //执行
//        boolean tableExists = admin.tableExists(tableName);
        boolean tableExists = admin.tableExists(TableName.valueOf(tableName));

        //关闭资源
        admin.close();

        return tableExists;


    }



    //创建表
    public static void createTable(String tableName, String... cfs) throws IOException {

        if (tableExist(tableName)) {
            System.out.println("表已存在!!");
            return;
        }

        //创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

        //添加列族
        for (String cf : cfs) {

            //创建列描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);

            hTableDescriptor.addFamily(hColumnDescriptor);
        }

        //创建表的操作
        admin.createTable(hTableDescriptor);

        System.out.println("表创建成功!");

    }


    //删除表
    public static void deleteTable(String tableName) throws IOException {

        if (!tableExist(tableName)) {
            return;
        }

        //删除表之前先使表不可用(下线)
        admin.disableTable(TableName.valueOf(tableName));

        //执行删除操作
        admin.deleteTable(TableName.valueOf(tableName));

        System.out.println("表已删除!!");

    }


    ////
    public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {

        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
//        HTable table = new HTable(configuration, TableName.valueOf(tableName));

        //创建Put对象
        Put put = new Put(Bytes.toBytes(rowKey));

        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));

        //执行添加操作
        table.put(put);

    }


    //
    public static void delete(String tableName, String rowKey, String cf, String cn) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建delete对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
//        delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));

        //执行删除操作
        table.delete(delete);

        table.close();

    }


    ////全表扫描
    public static void scanTable(String tableName) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //构建扫描器
        Scan scan = new Scan();
//        scan.setStartRow()
//        scan.setStopRow()

        ResultScanner results = table.getScanner(scan);

        //遍历数据并打印
        for (Result result : results) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("RK:" + Bytes.toString( CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }

        table.close();

    }

    //获取指定列族:列的数据
    public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {

        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建一个Get对象
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));

        //指定版本
//        get.setMaxVersions();

        //指定列族
//        get.addFamily();

        //获取数据的操作
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("RK:" + Bytes.toString( CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
                    + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }

        table.close();


    }



    public static void main(String[] args) throws IOException {


//        System.out.println(tableExist("student"));
//        System.out.println(tableExist("staff"));

        //调用创建表
//        createTable("staff", "info");
        //调用删除表
//        deleteTable("staff");
        //判断表是否存在
//        System.out.println(tableExist("staff"));


        //插入数据
//        putData("student", "1002", "info", "name", "zhangsan");
//        putData("student", "1002", "info", "sex", "female");
//        putData("student", "1002", "info", "age", "18");
//        putData("student", "1003", "info", "name", "lisi");
//        putData("student", "1003", "info", "sex", "male");
//        putData("student", "1004", "info", "age", "18");
//        putData("student", "1004", "info", "name", "wanwu");

//        delete("student", "1001", "info", "sex");


//        scanTable("student");


        getData("student", "1003", "info", "name");

        close(connection, admin);



    }
}
原文地址:https://www.cnblogs.com/LXL616/p/11013545.html