HBase Java API 例子

來源:互聯網
上載者:User

標籤:use   nbsp   else   log   base   mil   stat   stop   script   

import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.util.Bytes;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.IOException;/** * Created by Administrator on 2017/8/16. */public class TestHBase {    Configuration configuration = null;    @Before    public void setUp() throws IOException {        configuration = HBaseConfiguration.create();    }    @Test    public void testCreateTable() throws IOException {        String tableName = "jasontest";        HBaseAdmin admin = new HBaseAdmin(configuration);        HTableDescriptor desc = new HTableDescriptor(tableName);        desc.addFamily(new HColumnDescriptor("basic"));        if(admin.tableExists(tableName)){            System.out.println("table exist");        }else{            admin.createTable(desc);            System.out.println("create table successfully.");        }        admin.close();    }    @Test    public void testGetByRow() throws IOException {        String tableName = "user";        HTable table = new HTable(configuration,tableName);        byte[] row1 = Bytes.toBytes("1000");        Get get = new Get(row1);        Result result = table.get(get);        Cell[] cells = result.rawCells();        for(Cell cell:cells){            System.out.println(//                    Bytes.toString(CellUtil.cloneFamily(cell))+":" //                            + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" //                            + Bytes.toString(CellUtil.cloneValue(cell)) //            );        }        table.close();    }    @Test    public void testGetColumn() throws IOException {        String tableName = "user";        HTable table = new HTable(configuration,tableName);        Get get = new Get(Bytes.toBytes("1000"));        get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));        get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));        Result result = table.get(get);        Cell[] cells = result.rawCells();        for(Cell cell:cells) {            System.out.println(                    "Column Family is " + Bytes.toString(CellUtil.cloneFamily(cell)) + " | " //                            + "Column is " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " | " //                            + "Value is " + Bytes.toString(CellUtil.cloneValue(cell))            );        }        table.close();    }    @Test    public void testScanTable() throws IOException {        String tableName = "user";        HTable table = new HTable(configuration,tableName);        Scan scan = new Scan();//        scan.setStartRow(Bytes.toBytes("1001"));//        scan.setStopRow(Bytes.toBytes("1001"));//        scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));        Filter filer = new PrefixFilter(Bytes.toBytes("1000"));        scan.setFilter(filer);        ResultScanner resultScanner = table.getScanner(scan);        for(Result result:resultScanner){            Cell[] cells = result.rawCells();            for(Cell cell:cells){                System.out.println( //                                Bytes.toString(CellUtil.cloneRow(cell))+":"//                                + Bytes.toString(CellUtil.cloneFamily(cell))+":" //                                + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" //                                + Bytes.toString(CellUtil.cloneValue(cell)) //                );            }        }        table.close();    }    @Test    public void testPut() throws IOException {        String tableName = "user";        HTable table = new HTable(configuration,tableName);        Put put = new Put(Bytes.toBytes("1004"));        put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("Jason Zheng"));        put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(28));        table.put(put);        table.close();    }    @Test    public void testDelete() throws IOException {        String tableName = "user";        HTable table = new HTable(configuration,tableName);        Delete delete = new Delete(Bytes.toBytes("1004"));        delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("name"));        delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("age"));//        delete.deleteFamily(Bytes.toBytes("info"));        table.delete(delete);        table.close();    }}

 

HBase Java API 例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.