HBase Api DML 、DDL 的使用

5,964 阅读3分钟

DDL


public class DDL {

    private static Connection connection;

    static {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2,hadoop3");
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

//        creatTable(null, "api", "info");

//        listTableNames(null);

//        listTableDescriptors(null);

//        createNamespace("test");

//        listNamespaceDescriptors();

//        tableExists("stu", "api");

//        isTableDisabled(null, "api");

//        isTableEnabled(null, "api");

//        disableTable(null, "api");

//        truncateTable(null, "api", true);

//        enableTable(null, "api");

//        deleteTable(null, "api");

    }

    /**
     * 创建 Namespace
     *
     * @param namespace
     * @throws IOException
     */
    public static void createNamespace(String namespace) throws IOException {
        Admin admin = connection.getAdmin();

        NamespaceDescriptor descriptor = NamespaceDescriptor.create(namespace).build();
        admin.createNamespace(descriptor);

        admin.close();
    }

    /**
     * 获取所有 Namespace
     *
     * @throws IOException
     */
    public static void listNamespaceDescriptors() throws IOException {
        Admin admin = connection.getAdmin();

        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();

        for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
            System.out.println(namespaceDescriptor.getName());
        }

        admin.close();
    }

    /**
     * 创建表
     *
     * @param namespace
     * @param tableName
     * @param families
     * @throws IOException
     */
    public static void creatTable(String namespace, String tableName, String... families) throws IOException {
        Admin admin = connection.getAdmin();

        TableDescriptor tableDescriptor = TableDescriptorBuilder
                .newBuilder(TableName.valueOf(namespace, tableName))
                .setColumnFamilies(getColumnFamilies(families))
                .build();
        admin.createTable(tableDescriptor);

        admin.close();
    }

    /**
     * 表名列表
     *
     * @param namespace
     * @throws IOException
     */
    public static void listTableNames(String namespace) throws IOException {
        Admin admin = connection.getAdmin();

        TableName[] tableNames = StringUtils.isBlank(namespace) ? admin.listTableNames() : admin.listTableNamesByNamespace(namespace);
        for (TableName name : tableNames) {
            System.out.println(name);
        }

        admin.close();
    }

    /**
     * 表名列表
     *
     * @param namespace
     * @throws IOException
     */
    public static void listTableDescriptors(String namespace) throws IOException {
        Admin admin = connection.getAdmin();

        List<TableDescriptor> tableDescriptors = StringUtils.isBlank(namespace) ? admin.listTableDescriptors() : admin.listTableDescriptorsByNamespace(Bytes.toBytes(namespace));
        for (TableDescriptor descriptor : tableDescriptors) {
            System.out.println(descriptor.getTableName());
            ColumnFamilyDescriptor[] columnFamilies = descriptor.getColumnFamilies();
            for (ColumnFamilyDescriptor columnFamily : columnFamilies) {
                System.out.println(columnFamily.getNameAsString());
            }
            System.out.println("");
        }

        admin.close();
    }

    /**
     * 表是否存在
     *
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean tableExists(String namespace, String tableName) throws IOException {
        Admin admin = connection.getAdmin();

        boolean tableExists = admin.tableExists(TableName.valueOf(namespace, tableName));

        System.out.println(tableExists);

        admin.close();

        return tableExists;
    }

    /**
     * 删除表
     *
     * @param tableName
     * @throws IOException
     */
    public static void deleteTable(String namespace, String tableName) throws IOException {
        Admin admin = connection.getAdmin();

        admin.deleteTable(TableName.valueOf(namespace, tableName));

        admin.close();
    }

    /**
     * 是否禁用表
     *
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean isTableDisabled(String namespace, String tableName) throws IOException {
        Admin admin = connection.getAdmin();

        boolean tableDisabled = admin.isTableDisabled(TableName.valueOf(namespace, tableName));

        admin.close();

        System.out.println(tableDisabled);

        return tableDisabled;
    }

    /**
     * 是否启用表
     *
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean isTableEnabled(String namespace, String tableName) throws IOException {
        Admin admin = connection.getAdmin();

        boolean tableEnabled = admin.isTableEnabled(TableName.valueOf(namespace, tableName));

        admin.close();

        System.out.println(tableEnabled);

        return tableEnabled;
    }

    /**
     * 禁用表
     *
     * @param tableName
     * @throws IOException
     */
    public static void disableTable(String namespace, String tableName) throws IOException {
        Admin admin = connection.getAdmin();

        admin.disableTable(TableName.valueOf(namespace, tableName));

        admin.close();
    }

    /**
     * 启用表
     *
     * @param tableName
     * @throws IOException
     */
    public static void enableTable(String namespace, String tableName) throws IOException {
        Admin admin = connection.getAdmin();

        admin.enableTable(TableName.valueOf(namespace, tableName));

        admin.close();
    }

    /**
     * 清空表
     *
     * @param tableName
     * @param preserveSplits
     * @throws IOException
     */
    public static void truncateTable(String namespace, String tableName, boolean preserveSplits) throws IOException {
        Admin admin = connection.getAdmin();

        admin.truncateTable(TableName.valueOf(namespace, tableName), preserveSplits);

        admin.close();
    }

    /**
     * 列族名转列族对象
     *
     * @param families
     * @return
     */
    public static List<ColumnFamilyDescriptor> getColumnFamilies(String... families) {
        return Arrays.stream(families).map(ColumnFamilyDescriptorBuilder::of).collect(Collectors.toList());
    }

}

DML

public class DML {

    private static Connection connection;

    static {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2,hadoop3");
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

        String namespace = "test";

        String tableName = "test_dml";

//        putData(namespace, tableName, "1000", "info", "name", "zhangsan");
//        putData(namespace, tableName, "1000", "info", "age", "18");
//        putData(namespace, tableName, "1000", "info", "phone", "17777777777");
//        putData(namespace, tableName, "1001", "info", "name", "lisi");
//        putData(namespace, tableName, "1001", "info", "age", "19");
//        putData(namespace, tableName, "1001", "info", "phone", "17777777888");
//        putData(namespace, tableName, "1002", "info", "name", "wangwu");
//        putData(namespace, tableName, "1002", "info", "age", "13");
//        putData(namespace, tableName, "1002", "info", "phone", "17777666688");
//        putData(namespace, tableName, "1003", "info", "name", "zhaoliu");
//        putData(namespace, tableName, "1003", "info", "age", "16");
//        putData(namespace, tableName, "1003", "info", "phone", "17333777888");

//        getData(namespace, tableName, "1000", null, null);
//        getData(namespace, tableName, "1000", "info", null);
//        getData(namespace, tableName, "1000", "info", "name");

//        deleteData(namespace, tableName, "1001", "info", "name", 1620995476748L);
//        deleteData(namespace, tableName, "1001", "info", "age", null);
//        deleteData(namespace, tableName, "1001", "info", null, null);
//        deleteData(namespace, tableName, "1001", null, null, null);

//        scanData(namespace, tableName, null, null, null, null);
//        scanData(namespace, tableName, "1001", "1002", null, null);
//        scanData(namespace, tableName, null, null, true, 3);
//        scanData(namespace, tableName, "1001", "1002", true, 3);
    }

    /**
     * 获取数据
     *
     * @param namespace
     * @param tableName
     * @throws IOException
     */
    public static void getData(String namespace, String tableName, String row, String family, String qualifier) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        Get get = new Get(row.getBytes());

        if (StringUtils.isNotBlank(family) && StringUtils.isNotBlank(qualifier)) {
            get.addColumn(family.getBytes(), qualifier.getBytes());
        } else if (StringUtils.isNotBlank(family)) {
            get.addFamily(family.getBytes());
        }

        Result result = table.get(get);

        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
            System.out.println(
                    Bytes.toString(CellUtil.cloneFamily(cell)) + "\t" +
                            Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t" +
                            Bytes.toString(CellUtil.cloneRow(cell)) + "\t" +
                            Bytes.toString(CellUtil.cloneValue(cell))
            );
        }

        table.close();
    }

    /**
     * 添加修改数据
     *
     * @param namespace
     * @param tableName
     * @throws IOException
     */
    public static void putData(String namespace, String tableName, String row, String family, String qualifier, String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        Put put = new Put(row.getBytes());

        put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());

        table.put(put);

        table.close();
    }

    /**
     * 扫描表
     *
     * @param namespace
     * @param tableName
     * @throws IOException
     */
    public static void scanData(String namespace, String tableName, String startRow, String stopRow, Boolean raw, Integer versions) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        Scan scan = new Scan();

        if (StringUtils.isNotBlank(startRow) && StringUtils.isNotBlank(startRow)) {
            scan.withStartRow(startRow.getBytes()).withStopRow(stopRow.getBytes());
        }
        if (raw != null && versions != null) {
            scan.setRaw(raw).readVersions(versions);
        }

        ResultScanner resultScanner = table.getScanner(scan);

        for (Result result : resultScanner) {
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                System.out.println(
                        Bytes.toString(CellUtil.cloneFamily(cell)) + "\t" +
                                Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t" +
                                Bytes.toString(CellUtil.cloneRow(cell)) + "\t" +
                                Bytes.toString(CellUtil.cloneValue(cell)) + "\t" +
                                cell.getType().name() + "\t" +
                                cell.getTimestamp()
                );
            }
        }

        table.close();
    }

    /**
     * 删除数据
     *
     * @param namespace
     * @param tableName
     * @throws IOException
     */
    public static void deleteData(String namespace, String tableName, String row, String family, String qualifier, Long timestamp) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));

        Delete delete = new Delete(row.getBytes());

        if (StringUtils.isNotBlank(family) && StringUtils.isNotBlank(qualifier)) {
            delete.addColumn(family.getBytes(), qualifier.getBytes());
        } else if (StringUtils.isNotBlank(family)) {
            if (timestamp != null) {
                delete.addFamilyVersion(family.getBytes(), timestamp);
            } else {
                delete.addFamily(family.getBytes());
            }
        }

        table.delete(delete);

        table.close();
    }

}

持续分享大数据、Java文章,感兴趣可以关注一下