hbase新版api java程序实现

本文详细介绍HBase数据库的各种操作方法,包括表管理、数据读写及过滤等高级功能。通过具体实例展示了如何使用Java API进行HBase表的创建、数据插入、查询及删除,并介绍了多种扫描过滤器的应用场景。

public class HbaseOperatorI {

    private Connection connection = null;
    private Table table = null;
    private Admin admin = null;
    private List<Put> puts = new ArrayList<Put>();
    private List<Get> gets = new ArrayList<Get>();
    private Result[] getRes = null;
    private List<Delete> deletes = new ArrayList<Delete>();

    public HbaseOperatorI() {

    }

    public void getConnection() throws IOException {
       
        connection = ConnectionFactory.createConnection();

    }

    public void getTable(String tableName) throws IOException {
        table = connection.getTable(TableName.valueOf(tableName));
    }

    public void getAdmin() throws IOException {
        admin = connection.getAdmin();
    }

    // --------------------------------admin start----------------------------
    public void createTable(String tableName, String familyName) throws IOException {
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        HColumnDescriptor columnDes = new HColumnDescriptor(familyName);
        descriptor.addFamily(columnDes);
        columnDes.setCompressionType(Algorithm.SNAPPY);
        if (!admin.tableExists(TableName.valueOf(tableName))) {
            admin.createTable(descriptor);
        } else {
            System.err.println("the table :" + tableName + " is exists!");
        }

    }

    public void compactTable(String tableName) throws IOException {
        admin.compact(TableName.valueOf(tableName));

    }

    public void compactRegion(String region) throws IOException {
        admin.compactRegion(Bytes.toBytes(region));
    }

    public void createNameSpace(String namespace) throws IOException {
        // not pass

        // admin.createNamespace(NamespaceDescriptor.create(namespace));

    }

    // -----------------regions info--------------------------
    public void printTableRegions(String tableName) {
        System.out.println("print regions of table:" + tableName);
        // HTable
        // Pair<byte[][],byte[][]> pair=table. getStartEndKeys()
        byte[][] regions = new byte[][] { Bytes.toBytes("A"), Bytes.toBytes("A"), Bytes.toBytes("D"),
                Bytes.toBytes("G"), Bytes.toBytes("K"), Bytes.toBytes("O"), Bytes.toBytes("I"), };

    }

    public void printTableDescriptor() throws IOException {
        HTableDescriptor[] htds = admin.listTables();
        for (HTableDescriptor htd : htds) {
            System.out.println(htd);
        }
    }

    public void modifyTable(TableName tableName) throws TableNotFoundException, IllegalArgumentException, IOException {
        HTableDescriptor htd1 = admin.getTableDescriptor(tableName);
        HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("family2"));
        htd1.addFamily(hcd);
        htd1.setMaxFileSize(1024 * 1024 * 1024L);
        admin.disableTable(tableName);
        admin.modifyTable(tableName, htd1);
        admin.enableTable(tableName);

    }

    public void deleteColumn(String tableName, String col) throws IOException {
        admin.deleteColumn(TableName.valueOf(tableName), Bytes.toBytes(col));
    }

    public void deleteNameSpace(String namespace) throws IOException {
        admin.deleteNamespace(namespace);
    }

    public void deletesnapShot(String snapshotName) throws IOException {
        admin.deleteSnapshot(Bytes.toBytes(snapshotName));
    }

    public void deleteTable(String tableName) throws IOException {
        if (tableName == null) {
            throw new RuntimeException("no delete tableName!");
        }
        admin.deleteTable(TableName.valueOf(tableName));
    }

    public void deleteTableAsync(String tablename) {
        // Deletes the table but does not block and wait for it be completely
        // removed.
        // admin. deleteTableAsync(tablename);
    }

    public void deleteTables(Pattern pa) throws IOException {
        admin.deleteTables(pa);

    }

    public void disableTables(String regex) throws IOException {
        admin.disableTables(regex);
    }

    public void enableTables(String regex) throws IOException {
        admin.enableTables(regex);
    }

    public void getAlterStatus(String tableName) throws IOException {
        Pair<Integer, Integer> status = admin.getAlterStatus(TableName.valueOf(tableName));
    }

    public void getTableRegions(String tableName) throws IOException {
        List<HRegionInfo> region = admin.getTableRegions(TableName.valueOf(tableName));
        for (HRegionInfo info : region) {
            System.out.println(info.getRegionNameAsString());
        }
    }

    public void listTableName() throws IOException {
        TableName[] tableName = admin.listTableNames();

        for (TableName name : tableName) {
            System.out.println("tableName:" + name.getNameAsString());
        }
    }

    public void listNamespaceDescriptors() throws IOException {
        NamespaceDescriptor[] namespaceDes = admin.listNamespaceDescriptors();
        for (NamespaceDescriptor name : namespaceDes) {
            System.out.println("namespaceDes:" + name.getName());
        }
    }

    public void listTableDescriptorsByNamespace(String name) throws IOException {
        HTableDescriptor[] tableDes = admin.listTableDescriptorsByNamespace(name);
        for (HTableDescriptor des : tableDes) {
            System.out.println("tableDes:" + des.getNameAsString());
        }

    }
    // --------------------------------admin end----------------------------
    // --------------------------------table start---------------------------

    public void getSingleRow(String row) throws IOException {
        Get get = new Get(Bytes.toBytes(row));
        // If no further operations are done, this will get the latest version
        // of all columns in all families of the specified row.
        Result res = table.get(get);
        if (!res.isEmpty()) {
            Cell[] cs = res.rawCells();
            for (Cell cl : cs) {
                System.out.println("rowkey" + Bytes.toString(cl.getRowArray(), cl.getRowOffset(), cl.getRowLength()));
                System.out.println(
                        "family" + Bytes.toString(cl.getFamilyArray(), cl.getFamilyOffset(), cl.getFamilyLength()));
                System.out.println("column"
                        + Bytes.toString(cl.getQualifierArray(), cl.getQualifierOffset(), cl.getQualifierLength()));
                System.out.println(
                        "value" + Bytes.toString(cl.getValueArray(), cl.getValueOffset(), cl.getValueLength()));
            }

        }

    }

    public void getColumnCells(String row, String family, String column) throws IOException {
        Get get = new Get(Bytes.toBytes(row));

        Result res = table.get(get);
        if (!res.isEmpty()) {
            List<Cell> cs = res.getColumnCells(Bytes.toBytes(family), Bytes.toBytes(column));
            for (int i = 0; i < cs.size(); i++) {

                System.out.println(Bytes.toString(cs.get(i).getValueArray(), cs.get(i).getValueOffset(),
                        cs.get(i).getValueLength()));
                // 注意Byte.toString(b);
                // Bytes.toString(b, off, len)

            }
        } else {
            System.out.println("res is 0");
        }
    }

    public void getInBatch(String[] row) throws IOException {
        if (row != null) {
            for (int i = 0; i < row.length; i++) {
                Get get = new Get(Bytes.toBytes(row[i]));
                get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("Datadate"));
                gets.add(get);
                if (gets != null && gets.size() % 3000 == 0) {
                    getRes = table.get(gets);
                }
            }

            for (Result result : getRes) {
                if (!result.isEmpty()) {
                    Cell[] cells = result.rawCells();
                    for (Cell cl : cells) {
                        System.out.println(Bytes.toString(cl.getRowArray(), cl.getRowOffset(), cl.getRowLength()));
                        System.out.println(
                                Bytes.toString(cl.getFamilyArray(), cl.getFamilyOffset(), cl.getFamilyLength()));
                        System.out.println("column" + Bytes.toString(cl.getQualifierArray(), cl.getQualifierOffset(),
                                cl.getQualifierLength()));
                        System.out.println(
                                "value" + Bytes.toString(cl.getValueArray(), cl.getValueOffset(), cl.getValueLength()));
                    }
                } else {
                    System.out.println("result is 000");
                }

            }
        }

    }

    public void getRowByAddFamily(String family, String qualifier, Get get) throws IOException {
        Get get1 = new Get(get);
        get1.addFamily(Bytes.toBytes(family));
        Result result = table.get(get1);
    }

    public void getRowByAddColumn(String family, String qualifier, Get get) throws IOException {
        Get get1 = new Get(get);
        get1.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        // Result result = table.get(get1);

    }

    public void getOtherMethod(Object obj, Row row, Get get) throws IOException {
        Get get1 = new Get(get);
        get1.compareTo(row);
        get1.equals(obj);
        // Method for retrieving the keys in the familyMap
        get1.familySet();
        // Get whether blocks should be cached for this Get.
        get1.getCacheBlocks();
        // Method for retrieving the get's familyMap检索
        get1.getFamilyMap();
        // Compile the table and column family (i.e.
        get1.getFingerprint();
        // Method for retrieving the get's maximum number of values to return
        // per Column Family
        get1.getMaxResultsPerColumnFamily();
        // Method for retrieving the get's maximum number of version
        get1.getMaxVersions();
        // Method for retrieving the get's offset per row per column family
        // (#kvs to be skipped)
        get1.getRowOffsetPerColumnFamily();

    }

    public void useScanField() {
        String name = Scan.SCAN_ATTRIBUTES_TABLE_NAME;
        String id_attribute = Scan.ID_ATRIBUTE;
        name.toString();
        id_attribute.toString();

    }

    public void useScanConstructor(Scan oldscan, Get get, byte[] startRow, byte[] stopRow, Filter filter)
            throws IOException {
        // Create a Scan operation across all rows.
        Scan scan1 = new Scan();
        // Create a Scan operation starting at the specified row.
        Scan scan2 = new Scan(startRow);
        // Create a Scan operation for the range of rows specified.
        Scan scan3 = new Scan(startRow, stopRow);
        // Builds a scan object with the same specs as get
        Scan scan4 = new Scan(get);
        //
        Scan scan5 = new Scan(oldscan);
        String json = scan1.toJSON() + scan1.toJSON() + scan2.toJSON() + scan3.toJSON() + scan4.toJSON()
                + scan5.toJSON();
        System.out.println(json);

    }

    public void useScanMethod(Scan scan, byte[] family, byte[] qualifier) {
        // Get the column from the specified family with the specified
        // qualifier.
        scan.addColumn(family, qualifier);
        // Get all columns from the specified family.
        scan.addFamily(family);
        scan.getBatch();
        scan.setCacheBlocks(scan.getCacheBlocks());
        Map<byte[], NavigableSet<byte[]>> familyMap = new HashMap<byte[], NavigableSet<byte[]>>();
        scan.setFamilyMap(familyMap);
        scan.getFamilies();
        scan.setFilter(scan.getFilter());
        scan.getMaxResultSize();
        scan.getMaxResultsPerColumnFamily();
        scan.getStartRow();
        scan.getStopRow();
        scan.hasFamilies();
        scan.hasFilter();
        scan.isGetScan();
        scan.setReversed(true);
        scan.setScanMetricsEnabled(true);
        // Get whether this scan is a reversed one.
        scan.isReversed();
        scan.isScanMetricsEnabled();
        scan.numFamilies();
        scan.setBatch(30000);
        // Set the number of rows for caching that will be passed to scanners.
        scan.setCaching(3000);
        scan.setConsistency(scan.getConsistency());
        scan.setIsolationLevel(scan.getIsolationLevel());
        scan.setMaxResultSize(scan.getMaxResultSize());
        int maxCols = 100;
        Map<String, Object> maps = scan.toMap(maxCols);
        maps.size();

    }

    public void scanRowWithColumn(String startrow, String stopRow, String family, String qualifier) throws IOException {
        Scan scan = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(stopRow));
        // scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        scan.setCaching(1000);
        scan.setBatch(1);
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        while (iterator.hasNext()) {
            Result result = iterator.next();
            Cell[] cell = result.rawCells();
            for (Cell c : cell) {
                System.out.println(Bytes.toString(c.getValueArray(), c.getValueOffset(), c.getValueLength()));
                System.out.println(Bytes.toString(CellUtil.cloneValue(c)));
            }
        }

    }

    public void scanWithValueFilter(String value) throws IOException {
        Filter filter = new ValueFilter(CompareOp.EQUAL, new SubstringComparator(value));
        Scan scan = new Scan();
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                System.out.println(Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
                System.out.println(Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
            }
        }
        scanner.close();

    }

    public void scanWithRowFilter(String family, String qualifier, String compval) throws IOException {
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        // 精确匹配
        Filter filter = new RowFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes(compval)));
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result res : scanner) {
            System.out.println(res.toString());
        }
        scanner.close();
        // 正则匹配
        Filter filter1 = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("\\d{1,3}"));
        scan.setFilter(filter1);
        ResultScanner scanner1 = table.getScanner(scan);
        for (Result result : scanner1) {
            System.out.println(result.toString());
        }
        scanner1.close();

        // 字符串匹配
        Filter filter2 = new RowFilter(CompareOp.EQUAL, new SubstringComparator("12"));
        scan.setFilter(filter2);
        ResultScanner scanner2 = table.getScanner(scan);
        for (Result result3 : scanner2) {
            System.out.println(result3.toString());

        }
        scanner2.close();
    }

    public void scanWithFamilyFilter(String colfamily) throws IOException {
        Scan scan = new Scan();
        Filter filter1 = new FamilyFilter(CompareOp.LESS, new BinaryComparator(Bytes.toBytes(colfamily)));
        scan.setFilter(filter1);
        ResultScanner result = table.getScanner(scan);
        for (Result re : result) {
            System.out.println(re.toString());
        }

        Get get = new Get(Bytes.toBytes("row-5"));
        get.setFilter(filter1);
        Result resu = table.get(get);
        System.out.println(resu.toString());

        Filter filter2 = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("colfam2")));
        Get get2 = new Get(Bytes.toBytes("row-5"));
        get2.setFilter(filter2);
    }

    public void scanWithQualifierFilter() throws IOException {
        Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("col-2")));
        Scan scan = new Scan();
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.println(result);

        }
        scanner.close();
        Get get = new Get(Bytes.toBytes("row-5"));
        get.setFilter(filter);
        Result result1 = table.get(get);
        System.out.println("result of get()" + result1);
    }

    // 用一列值决定是否一行数据被过滤
    public void scanWithSingleColumnValueFilter(byte[] family, byte[] col) throws IOException {
        SingleColumnValueFilter filter = new SingleColumnValueFilter(family, col, CompareOp.NOT_EQUAL,
                new SubstringComparator("val-5"));
        filter.setFilterIfMissing(true);
        Scan scan = new Scan();
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result re : scanner) {
            System.out.println(re);
        }

    }

    // 前缀过滤器,所有和前缀匹配的行都会被返回到客户端,在扫描过程中非常有用
    public void scanWithPrefixFilter() throws IOException {
        Filter filter = new PrefixFilter(Bytes.toBytes("row-"));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result s : scanner) {
            System.out.println(s);
        }
        scanner.close();

    }
    // 分页过滤器,需要制定pageSize参数

    public void scanWithPageFilter(int pageSize) throws IOException {
        Filter filter = new PageFilter(pageSize);
        final byte[] POSTFIX = new byte[] { 0x00 };
        byte[] lastRow = null;
        while (true) {
            Scan scan = new Scan();
            scan.setFilter(filter);
            if (lastRow != null) {
                byte[] startRow = Bytes.add(lastRow, POSTFIX);
                scan.setStartRow(startRow);
            }

            ResultScanner scanner = table.getScanner(scan);
            int totalRows = 0;
            Result result;
            while ((result = scanner.next()) != null) {
                System.out.println(totalRows++ + ":" + result);
                totalRows++;
                lastRow = result.getRow();
            }
            scanner.close();
            if (totalRows == 0)
                break;
        }
    }

    public void scanWithTimeStampFilter() throws IOException {
        List<Long> ts = new ArrayList<Long>();
        ts.add(new Long(5));
        ts.add(new Long(10));
        ts.add(new Long(15));
        Filter filter = new TimestampsFilter(ts);
        Scan scan = new Scan();
        scan.setFilter(filter);
        scan.setTimeRange(8, 10);
        ResultScanner scanner1 = table.getScanner(scan);
        for (Result result : scanner1) {
            System.out.println(result);
        }
        scanner1.close();

    }

    public void put(String row, String family, String col, String val) throws IOException {

        Put put = new Put(Bytes.toBytes(row));

        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(col), Bytes.toBytes(val));
        table.put(put);

    }

    public void putInBatch(String row, String family, String col, String val) throws IOException {
        Put put = new Put(Bytes.toBytes(row));
        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(col), Bytes.toBytes(val));
        puts.add(put);
        if (puts != null && puts.size() % 3000 == 0) {
            table.put(puts);
        }

    }

    public void deleteSpecifiedRow(String row) throws IOException {
        Delete delete = new Delete(Bytes.toBytes(row));
        table.delete(delete);
    }

    public void deleteSpecifiedRowInBatch(String[] row) throws IOException {
        if (row != null) {
            for (String r : row) {
                Delete delete = new Delete(Bytes.toBytes(r));
                deletes.add(delete);

                if (deletes != null && deletes.size() % 3000 == 0) {
                    table.delete(deletes);
                }

            }
        }

    }

    public void deleteRowByTimeStamp(byte[] row, long timestamp) throws IOException {
        Delete delete = new Delete(row, timestamp);
        table.delete(delete);

    }

    // through get method
    public void deleteRowByOther(byte[] rowArray, int rowOffset, int rowLength) throws IOException {
        Delete delete = new Delete(rowArray, rowOffset, rowLength);
        table.delete(delete);
    }

    // rowArray - We make a local copy of this passed in row.
    public void deleteRowByOtherTs(byte[] rowArray, int rowOffset, int rowLength, long ts) throws IOException {
        Delete delete = new Delete(rowArray, rowOffset, rowLength, ts);
        table.delete(delete);
    }

    public void deletebyDelete(Delete delete) throws IOException {
        Delete delete1 = new Delete(delete);
        table.delete(delete1);
    }

    public Delete getDelete(Cell kv, byte[] family, byte[] qualifier, long timestamp) throws IOException {
        Delete delete = new Delete(kv.getRowArray());
        delete.addColumn(family, qualifier);
        delete.addColumn(family, qualifier, timestamp);
        delete.addColumns(family, qualifier);
        delete.addColumns(family, qualifier, timestamp);
        delete.addDeleteMarker(kv);
        delete.addFamily(family);
        delete.addFamily(family, timestamp);
        return delete;
    }

    public boolean existsGet(Get get, byte[] family, byte[] qualifier) throws IOException {
        get.addColumn(family, qualifier);
        return table.exists(get);
    }

    public boolean[] existsListGets(List<Get> gets) throws IOException {
        // Array of boolean. True if the specified Get matches one or more keys,
        // false if not.
        return table.existsAll(gets);
    }

    public void closeResources() {
        try {
            if (table != null || connection != null || admin != null) {
                table.close();
                admin.close();
                connection.close();
            }

        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    public void getHTablePool() {

    }

}

 

转载于:https://my.oschina.net/iioschina/blog/809203

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值