云数据库 TableStorage

    TableStorage-HBase-Client开发示例

    配置示例

    下面是hbase-site.xml示例:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
        <property>
            <name>hbase.client.connection.impl</name>
            <value>com.baidubce.services.tablestoragehbaseclient.hbase.TablestorageConnection</value>
        </property>
        <property>
            <name>tablestorage.client.endpoint</name>
            <value>http://bts.bd.baidubce.com</value>
        </property>
        <property>
            <name>tablestorage.client.instancename</name>
            <value>tablestorage_test</value>
        </property>
        <property>
            <name>tablestorage.client.accesskeyid</name>
            <value>your-access-key-id</value>
        </property>
        <property>
            <name>tablestorage.client.secretaccesskey</name>
            <value>your-secret-access-key</value>
        </property>
    </configuration>

    详细示例

    初始化

    Configuration conf = HBaseConfiguration.create();
    Connection connection = ConnectionFactory.createConnection(conf);
    Admin admin = connection.getAdmin();
    Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
    BufferedMutator bufferedMutator = connection.getBufferedMutator(TableName.valueOf(TABLE_NAME));
    RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(TABLE_NAME));

    Admin接口示例

    创建表

    try {
        System.out.println("--------------createTable---------------");
        HTableDescriptor createDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
        HColumnDescriptor createColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
        createColumnDescriptor.setTimeToLive(24 * 3600);
        createColumnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY);
        createDescriptor.addFamily(createColumnDescriptor);
        admin.createTable(createDescriptor);
    } catch (IOException e) {
        e.printStackTrace();
    }

    修改column信息

    try {
        System.out.println("--------------modifyColumn---------------");
        HColumnDescriptor modifyColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
        modifyColumnDescriptor.setTimeToLive(HConstants.FOREVER);
        modifyColumnDescriptor.setCompressionType(Compression.Algorithm.NONE);
        admin.modifyColumn(TableName.valueOf(TABLE_NAME), modifyColumnDescriptor);
    } catch (IOException e) {
        e.printStackTrace();
    }

    删除表

    try {
        System.out.println("--------------deleteTable---------------");
        admin.deleteTable(TableName.valueOf(TABLE_NAME));
    } catch (IOException e) {
        e.printStackTrace();
    }

    获取表信息

    try {
        System.out.println("--------------getTable---------------");
        HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf(TABLE_NAME));
        for (HColumnDescriptor columnDescriptor : descriptor.getFamilies()) {
            System.out.println(columnDescriptor.getCompressionType());
            System.out.println(columnDescriptor.getTimeToLive());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    列举表

    try {
        System.out.println("--------------listTable---------------");
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.out.println(tableName.getNameAsString());
        }
     
        System.out.println("--------------listTable with pattern---------------");
        tableNames = admin.listTableNames("Table.*");
        for (TableName tableName : tableNames) {
            System.out.println(tableName.getNameAsString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Table接口示例

    向Table写入数据

    try {
        System.out.println("--------------put---------------");
        Put put = new Put(ROW_KEY);
        put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
        table.put(put);
     
        System.out.println("--------------batchPut---------------");
        List<Put> putList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Put put1 = new Put(Bytes.toBytes("row_" + i));
            for (int j = 0; j < 10; j++) {
                put1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
                        Bytes.toBytes("value_" + i + "_" + j));
            }
            putList.add(put1);
        }
        table.put(putList);
    } catch (IOException e) {
        e.printStackTrace();
    }

    从Table读取数据

    try {
        System.out.println("--------------get---------------");
        Get get = new Get(ROW_KEY);
        get.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
        Result result = table.get(get);
        System.out.println("result: " + result.toString());
        System.out.println("result: " + Bytes.toString(result.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME)));
     
        System.out.println("--------------batchGet---------------");
        List<Get> getList = new ArrayList<>();
        for (int i = 0; i < 55; i++) {
            Get get1 = new Get(Bytes.toBytes("row_" + i));
            for (int j = 0; j < 10; j++) {
                get1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
            }
            getList.add(get1);
        }
        getList.add(get);
        Result[] results = table.get(getList);
        for (Result result1 : results) {
            System.out.println("result: " + result1.toString());
        }
     
        System.out.println("--------------scan---------------");
        Scan scan = new Scan();
        scan.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
        ResultScanner scanner = table.getScanner(scan);
        try {
            for (Result row : scanner) {
                byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
                System.out.println("\t row: " + Bytes.toString(row.getRow())
                        + ", value: " + Bytes.toString(valueBytes));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    删除Table中的数据

    try {
        System.out.println("--------------delete---------------");
        Delete delete = new Delete(ROW_KEY);
        delete.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
        table.delete(delete);
     
        System.out.println("--------------batchDelete---------------");
        List<Delete> deleteList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Delete delete1 = new Delete(Bytes.toBytes("row_" + i));
            for (int j = 0; j < 10; j++) {
                delete1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
            }
            deleteList.add(delete1);
        }
        table.delete(deleteList);
    } catch (IOException e) {
        e.printStackTrace();
    }

    BufferedMutator接口示例

    mutate操作

    try {
        System.out.println("--------------buffered mutation mutate---------------");
        Put put1 = new Put(ROW_KEY);
        put1.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
        bufferedMutator.mutate(put1);
        System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
        Delete delete2 = new Delete(ROW_KEY);
        delete2.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
        bufferedMutator.mutate(delete2);
        System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
     
        System.out.println("--------------buffered mutation batchMutate---------------");
        List<Mutation> mutationList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Put putMutation = new Put(Bytes.toBytes("row_" + i));
            for (int j = 0; j < 10; j++) {
                putMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
                        Bytes.toBytes("value_" + i + "_" + j));
            }
            mutationList.add(putMutation);
        }
        for (int i = 0; i < 50; i++) {
            Delete deleteMutation = new Delete(Bytes.toBytes("row_" + i));
            for (int j = 0; j < 10; j++) {
                deleteMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
            }
            mutationList.add(deleteMutation);
        }
        bufferedMutator.mutate(mutationList);
        System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
     
     
        System.out.println("--------------buffered mutation flush---------------");
        bufferedMutator.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    RegionLocator接口示例

    获取表中指定Region的信息

    try {
        System.out.println("--------------Region locator getRegionLocation---------------");
        HRegionLocation location = regionLocator.getRegionLocation(ROW_KEY);
        System.out.println(location.getHostnamePort());
        location = regionLocator.getRegionLocation(ROW_KEY, true);
        System.out.println(location.getHostnamePort());
    } catch (IOException e) {
        e.printStackTrace();
    }

    获取表中所有Region的信息

    try {
        System.out.println("--------------Region locator getAllRegionLocations---------------");
        List<HRegionLocation>  hRegionLocationList = regionLocator.getAllRegionLocations();
        for (HRegionLocation location : hRegionLocationList) {
            System.out.println(location.getHostnamePort());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    获取表中所有Region的startKey

    try {
        System.out.println("--------------Region locator getStartKeys---------------");
        byte[][] startKeys = regionLocator.getStartKeys();
        for (byte[] startKey : startKeys) {
            System.out.println(Bytes.toString(startKey));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    获取表中所有Region的endKey

    try {
        System.out.println("--------------Region locator getEndKeys---------------");
        byte[][] stopKeys = regionLocator.getEndKeys();
        for (byte[] stopKey : stopKeys) {
            System.out.println(Bytes.toString(stopKey));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    获取表中所有Region的startKey和endKey

    try {
        System.out.println("--------------Region locator getStartEndKeys---------------");
        Pair<byte[][], byte[][]> startEndKeys = regionLocator.getStartEndKeys();
        for (int i = 0; i < startEndKeys.getFirst().length; i++) {
            StringBuffer buffer = new StringBuffer();
            buffer.append("startKey: ");
            buffer.append(Bytes.toString(startEndKeys.getFirst()[i]));
            buffer.append(", stopKey: ");
            buffer.append(Bytes.toString(startEndKeys.getSecond()[i]));
            System.out.println(buffer.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Hello World

    package com.baidubce.services.tablestoragehbaseclient.hbase;
     
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HConstants;
    import org.apache.hadoop.hbase.HRegionLocation;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.Admin;
    import org.apache.hadoop.hbase.client.BufferedMutator;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    import org.apache.hadoop.hbase.client.Delete;
    import org.apache.hadoop.hbase.client.Get;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.client.RegionLocator;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    import org.apache.hadoop.hbase.client.Table;
    import org.apache.hadoop.hbase.io.compress.Compression;
    import org.apache.hadoop.hbase.util.Bytes;
     
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
     
    public class TablestorageDemo {
        private static final String TABLE_NAME = "HBaseClientTest";
        private static final byte[] ROW_KEY = Bytes.toBytes("www.baidu.com/1");
        private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("cf0");
        private static final byte[] COLUMN_NAME = Bytes.toBytes("col_1");
        private static final byte[] DEFAULT_VALUE = Bytes.toBytes("value_1");
     
        public static void main(String[] args) {
            try {
                runHbaseClientDemo();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        private static void runHbaseClientDemo() throws IOException {
            Configuration conf = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(conf);
            Admin admin = connection.getAdmin();
            Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
            BufferedMutator bufferedMutator = connection.getBufferedMutator(TableName.valueOf(TABLE_NAME));
            RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(TABLE_NAME));
     
            try {
                System.out.println("--------------createTable---------------");
                HTableDescriptor createDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
                HColumnDescriptor createColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
                createColumnDescriptor.setTimeToLive(24 * 3600);
                createColumnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY);
                createDescriptor.addFamily(createColumnDescriptor);
                admin.createTable(createDescriptor);
     
                System.out.println("--------------isTableAvailable---------------");
                while (!admin.isTableAvailable(TableName.valueOf(TABLE_NAME))) {
                    System.out.println("table is not available now, please wait a moment.");
                    try {
                        Thread.sleep(10 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
     
                System.out.println("--------------modifyColumn---------------");
                HColumnDescriptor modifyColumnDescriptor = new HColumnDescriptor(COLUMN_FAMILY_NAME);
                modifyColumnDescriptor.setTimeToLive(HConstants.FOREVER);
                modifyColumnDescriptor.setCompressionType(Compression.Algorithm.NONE);
                admin.modifyColumn(TableName.valueOf(TABLE_NAME), modifyColumnDescriptor);
     
                System.out.println("--------------getTable---------------");
                HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf(TABLE_NAME));
                for (HColumnDescriptor columnDescriptor : descriptor.getFamilies()) {
                    System.out.println(columnDescriptor.getCompressionType());
                    System.out.println(columnDescriptor.getTimeToLive());
                }
     
                System.out.println("--------------listTable---------------");
                TableName[] tableNames = admin.listTableNames();
                for (TableName tableName : tableNames) {
                    System.out.println(tableName.getNameAsString());
                }
     
                System.out.println("--------------listTable with pattern---------------");
                tableNames = admin.listTableNames("Table.*");
                for (TableName tableName : tableNames) {
                    System.out.println(tableName.getNameAsString());
                }
     
                System.out.println("--------------put---------------");
                Put put = new Put(ROW_KEY);
                put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
                table.put(put);
     
                System.out.println("--------------batchPut---------------");
                List<Put> putList = new ArrayList<>();
                for (int i = 0; i < 50; i++) {
                    Put put1 = new Put(Bytes.toBytes("row_" + i));
                    for (int j = 0; j < 10; j++) {
                        put1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
                                Bytes.toBytes("value_" + i + "_" + j));
                    }
                    putList.add(put1);
                }
                table.put(putList);
     
                System.out.println("--------------get---------------");
                Get get = new Get(ROW_KEY);
                get.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
                Result result = table.get(get);
                System.out.println("result: " + result.toString());
                System.out.println("result: " + Bytes.toString(result.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME)));
     
                System.out.println("--------------batchGet---------------");
                List<Get> getList = new ArrayList<>();
                for (int i = 0; i < 55; i++) {
                    Get get1 = new Get(Bytes.toBytes("row_" + i));
                    for (int j = 0; j < 10; j++) {
                        get1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
                    }
                    getList.add(get1);
                }
                getList.add(get);
                Result[] results = table.get(getList);
                System.out.println(getList.size());
                System.out.println(results.length);
                for (Result result1 : results) {
                    System.out.println("result: " + result1.toString());
                }
     
                System.out.println("--------------scan---------------");
                Scan scan = new Scan();
                scan.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
                ResultScanner scanner = table.getScanner(scan);
                try {
                    for (Result row : scanner) {
                        byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
                        System.out.println("\t row: " + Bytes.toString(row.getRow())
                                + ", value: " + Bytes.toString(valueBytes));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    scanner.close();
                }
     
                System.out.println("--------------delete---------------");
                Delete delete = new Delete(ROW_KEY);
                delete.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
                table.delete(delete);
     
                System.out.println("--------------batchDelete---------------");
                List<Delete> deleteList = new ArrayList<>();
                for (int i = 0; i < 50; i++) {
                    Delete delete1 = new Delete(Bytes.toBytes("row_" + i));
                    for (int j = 0; j < 10; j++) {
                        delete1.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
                    }
                    deleteList.add(delete1);
                }
                table.delete(deleteList);
     
                System.out.println("--------------buffered mutation mutate---------------");
                Put put1 = new Put(ROW_KEY);
                put1.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
                bufferedMutator.mutate(put1);
                Put put2 = new Put(ROW_KEY);
                put2.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, DEFAULT_VALUE);
                bufferedMutator.mutate(put2);
                System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
                bufferedMutator.flush();
     
                Delete delete2 = new Delete(ROW_KEY);
                delete2.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME);
                bufferedMutator.mutate(delete2);
                System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
                bufferedMutator.flush();
     
                System.out.println("--------------buffered mutation batchMutate---------------");
                List<Mutation> mutationList = new ArrayList<>();
                for (int i = 0; i < 50; i++) {
                    Put putMutation = new Put(Bytes.toBytes("row_" + i));
                    for (int j = 0; j < 10; j++) {
                        putMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j),
                                Bytes.toBytes("value_" + i + "_" + j));
                    }
                    mutationList.add(putMutation);
                }
                for (int i = 0; i < 50; i++) {
                    Delete deleteMutation = new Delete(Bytes.toBytes("row_" + i));
                    for (int j = 0; j < 10; j++) {
                        deleteMutation.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("col_" + j));
                    }
                    mutationList.add(deleteMutation);
                }
                bufferedMutator.mutate(mutationList);
                System.out.println("buffered size: " + bufferedMutator.getWriteBufferSize());
                bufferedMutator.flush();
     
                System.out.println("--------------Region locator getAllRegionLocations---------------");
                List<HRegionLocation>  hRegionLocationList = regionLocator.getAllRegionLocations();
                for (HRegionLocation location : hRegionLocationList) {
                    System.out.println(location.getHostnamePort());
                }
     
                System.out.println("--------------Region locator getRegionLocation---------------");
                HRegionLocation location = regionLocator.getRegionLocation(ROW_KEY);
                System.out.println(location.getHostnamePort());
                location = regionLocator.getRegionLocation(ROW_KEY, true);
                System.out.println(location.getHostnamePort());
     
                System.out.println("--------------Region locator getStartKeys---------------");
                byte[][] startKeys = regionLocator.getStartKeys();
                for (byte[] startKey : startKeys) {
                    System.out.println(Bytes.toString(startKey));
                }
     
                System.out.println("--------------Region locator getEndKeys---------------");
                byte[][] stopKeys = regionLocator.getEndKeys();
                for (byte[] stopKey : stopKeys) {
                    System.out.println(Bytes.toString(stopKey));
                }
     
                System.out.println("--------------Region locator getStartEndKeys---------------");
                Pair<byte[][], byte[][]> startEndKeys = regionLocator.getStartEndKeys();
                for (int i = 0; i < startEndKeys.getFirst().length; i++) {
                    StringBuffer buffer = new StringBuffer();
                    buffer.append("startKey: ");
                    buffer.append(Bytes.toString(startEndKeys.getFirst()[i]));
                    buffer.append(", stopKey: ");
                    buffer.append(Bytes.toString(startEndKeys.getSecond()[i]));
                    System.out.println(buffer.toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    System.out.println("--------------deleteTable---------------");
                    admin.deleteTable(TableName.valueOf(TABLE_NAME));
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    regionLocator.close();
                    bufferedMutator.close();
                    table.close();
                    admin.close();
                    connection.close();
                }
            }
        }
    }
    上一篇
    从HBase迁移到TableStorage
    下一篇
    版本更新说明