Java API 操作Hbase__Java

來源:互聯網
上載者:User
package com.bi.net;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.CompareFilter;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Random;/** * Created by DengNi on 2016/6/18. */public class HbaseJdbc {    public static Configuration conf = null;    public static TableName tab = TableName.valueOf("HbaseInWord".getBytes());    public static TableName hbaseInWord = TableName.valueOf("HbaseInWord");    public static Random ra = new Random();    /**     * 1 create table 需要 HbaseAdmin 類     * 2 資料的insert (put) ,update ,select by get or scan 都是通過HTable 類操作     */    @Before    public void init() {        conf = HBaseConfiguration.create();        conf.set("hbase.zookeeper.quorum", "bigdataspark:2181,bigdatacloud:2181,bigdatahadoop:2181");    }    @Test    public void createNewTableOnHbase() throws IOException {        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);        if (hBaseAdmin.tableExists(tab)) {            hBaseAdmin.disableTable(tab);            hBaseAdmin.deleteTable(tab);        }        HTableDescriptor tableDesc = new HTableDescriptor(tab);        HColumnDescriptor colDesc = new HColumnDescriptor("cfOne");        colDesc.setBlockCacheEnabled(true);        colDesc.setBlocksize(1280);        colDesc.setMaxVersions(16);        tableDesc.addFamily(colDesc);        hBaseAdmin.createTable(tableDesc);        // base(main):015:0> list  cmd check if the table is been create on HBase    }    @Test    public void insertIntoHbaseInWord() throws IOException {        HTable table = new HTable(conf, hbaseInWord);        List list = new ArrayList<Put>();        for (int i = 999; i >= 0; i--) {            Put putValue = new Put(String.valueOf(i).getBytes());            putValue.add("cfOne".getBytes(), "name".getBytes(), "zl".getBytes());            putValue.add("cfOne".getBytes(), "age".getBytes(), String.valueOf(i % 102).getBytes());            list.add(putValue);        }        table.put(list);    }    @Test    public void searchFromHbaseInWordByGet() throws IOException {        HTable table = new HTable(conf, hbaseInWord);        Get get = new Get("999".getBytes());        Result result = table.get(get);        Cell cell = result.getColumnLatestCell("cfOne".getBytes(), "name".getBytes());        byte[] bytes = CellUtil.cloneValue(cell);        System.out.println(new String(bytes, "UTF-8"));    }    @Test    public void searchByScan() throws IOException {        HTable table = new HTable(conf, hbaseInWord);        Scan scan = new Scan("900".getBytes(), "999".getBytes());        ResultScanner result = table.getScanner(scan);        Iterator<Result> iterator = result.iterator();        while (iterator.hasNext()) {            Result result1 = iterator.next();            Cell cell = result1.getColumnLatestCell("cfOne".getBytes(), "name".getBytes());            byte[] value = CellUtil.cloneValue(cell);            byte[] row = CellUtil.cloneRow(cell);            System.out.println(new String(row, "UTF-8") + "  " + new String(value, "UTF-8"));        }    }    @Test    public void deleteOneRow() throws IOException {        HTable table = new HTable(conf, hbaseInWord);        Delete delete = new Delete("988".getBytes());        table.delete(delete);    }    /**     * A more complicated example     */    @Test    public void insertPut() throws IOException {        HTable table1 = new HTable(conf, hbaseInWord);        List<Put> list = new ArrayList<Put>();        for (int i = 0; i < 10000; i++) {            Put put = new Put(generateRowKey("1531187321").getBytes());            put.add("cfOne".getBytes(),"addr".getBytes(),"Shanghai".getBytes());            put.add("cfOne".getBytes(),"type".getBytes(),String.valueOf(ra.nextInt(2)).getBytes());            put.add("cfOne".getBytes(),"phone".getBytes(),generatePhone("153").getBytes());            put.add("cfOne".getBytes(),"time".getBytes(),String.valueOf(ra.nextInt(500)).getBytes());            list.add(put);        }        table1.put(list);    }    private  String generateRowKey(String s){        StringBuffer sb = new StringBuffer(s);        sb.append("_2016").append(String.format("%02d",ra.nextInt(12)+1))                .append(String.format("%02d",ra.nextInt(30)+1))                .append(String.format("%02d",ra.nextInt(24)))                .append(String.format("%02d",ra.nextInt(60)))                .append(String.format("%02d",ra.nextInt(60)));        return sb.toString();    }    private String generatePhone(String p){        StringBuffer sb = new StringBuffer(p);        sb.append(String.format("%08d",ra.nextInt(999999999)));        return sb.toString();    }    @Test    public void searchByScanInterval() throws IOException {        HTable table = new HTable(conf,hbaseInWord);        Scan scan = new Scan("1531187321_20160200000000".getBytes(),"1531187321_20160301000000".getBytes());        ResultScanner scanner = table.getScanner(scan);        Iterator<Result> iterator = scanner.iterator();        while(iterator.hasNext()){            Result result = iterator.next();            Cell column = result.getColumnLatestCell("cfOne".getBytes(),"phone".getBytes());            byte[] row = CellUtil.cloneRow(column);            byte[] value = CellUtil.cloneValue(column);            System.out.println(new String(row) + "  "+ new String(value));        }    }    @Test    public void findByContition() throws IOException {        HTable table = new HTable(conf,hbaseInWord);        Scan scan = new Scan();        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);        PrefixFilter prefixFilter = new PrefixFilter("1531187321_201610".getBytes());        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("cfOne".getBytes(), "type".getBytes(), CompareFilter.CompareOp.EQUAL, "1".getBytes());        filterList.addFilter(prefixFilter);        filterList.addFilter(singleColumnValueFilter);        scan.setFilter(filterList);        ResultScanner scanner = table.getScanner(scan);        Iterator<Result> iterator = scanner.iterator();        while(iterator.hasNext()){            Result result = iterator.next();            Cell columnLatestCell = result.getColumnLatestCell("cfOne".getBytes(),"phone".getBytes());            byte[] va = CellUtil.cloneValue(columnLatestCell);            byte[] row = CellUtil.cloneRow(columnLatestCell);            System.out.println(new String(row) +"  "+new String(va));        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.