HBase--部分API

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Random;

public class HBaseShell {

    private Connection connection;
    private Admin admin;

    @Before
    public void before() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","CentOS");
        connection= ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }

    //创建
    @Test
    public void testcreatenamespace() throws IOException {
        NamespaceDescriptor namespace=NamespaceDescriptor.create("dd")
                .addConfiguration("author","zhangsan")
                .build();
        admin.createNamespace(namespace);
    }

    //删除
    @Test
    public void dropnamespace() throws IOException {
        admin.deleteNamespace("dd");
    }

    //创建表
    @Test
    public void createtable() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
        //创建列簇
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
        //为列簇cf1指定保存的最大版本数
        cf1.setMaxVersions(10);
        HColumnDescriptor cf2 = new HColumnDescriptor("cf2");
        //为列簇cf2指定保存的最大版本数
        cf2.setMaxVersions(10);
        //设置最大存活时间
        cf2.setTimeToLive(300);//5分钟
        tableDescriptor.addFamily(cf1);
        tableDescriptor.addFamily(cf2);
        admin.createTable(tableDescriptor);
    }

    //为表t_user插入值
    @Test
    public void testput() throws IOException {
        //获取表
        TableName tableName = TableName.valueOf("dd:t_user");
        Table table = connection.getTable(tableName);
        //插入值
        Put put = new Put("100".getBytes());
        put.addColumn("cf1".getBytes(),"name".getBytes(),"张三丰".getBytes());
        put.addColumn("cf1".getBytes(),"age".getBytes(), Bytes.toBytes(256));
        put.addColumn("cf1".getBytes(),"sex".getBytes(), Bytes.toBytes(true));
        table.put(put);
    }

    //为表t_user批量插入
    @Test
    public void tsetbatchput() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        String[] str ={"www.cc.com","www.bb.com"};
        //创建缓冲区
        BufferedMutator bufferedMutator = connection.getBufferedMutator(tableName);
        for (int i =1;i<=1000;i++) {
            String s=str[new Random().nextInt(2)];
            String rowkey =s;
            if(i<10){
                rowkey+="000"+i;
            }else if(i<100){
                rowkey+="0"+i;
            }else if(i<=1000){
                rowkey+=""+i;
            }
            Put put = new Put(rowkey.getBytes());
            put.addColumn("cf1".getBytes(),"name".getBytes(),("user"+i).getBytes());
            put.addColumn("cf1".getBytes(),"age".getBytes(),Bytes.toBytes(i));
            put.addColumn("cf1".getBytes(),"sex".getBytes(),i%2==0?Bytes.toBytes("man"):Bytes.toBytes("women"));
            put.addColumn("cf1".getBytes(),"company".getBytes(),s.getBytes());
            //放入缓冲区
            bufferedMutator.mutate(put);
        }
        //提交到数据库
        bufferedMutator.flush();
        bufferedMutator.close();

    }

    //修改单条记录
    @Test
    public void testupdate() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        Table table = connection.getTable(tableName);
        Put put = new Put("100".getBytes());
        put.addColumn("cf1".getBytes(),"name".getBytes(),"wanfwu".getBytes());
        put.addColumn("cf1".getBytes(),"age".getBytes(),Bytes.toBytes(25));
        put.addColumn("cf1".getBytes(),"sex".getBytes(),Bytes.toBytes("man"));
        put.addColumn("cf1".getBytes(),"company".getBytes(),"abc".getBytes());
        table.put(put);

    }

    //删除表
    @Test
    public void testdeletetable() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }

    //展示表
    @Test
    public void testshowtables() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for(TableName tableName:tableNames){
            System.out.println(tableName.getNameAsString());
        }
    }

    //截断表
    @Test
    public void testtable() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        admin.disableTable(tableName);
        admin.truncateTable(tableName,true);
        admin.close();
    }

    //判断表是否存在
    @Test
    public void testable1() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        boolean b = admin.tableExists(tableName);
        System.out.println(b);
    }

    //插入数据
    @Test
    public void testinserttable() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        HTable hTable = (HTable) connection.getTable(tableName);
        Put put = new Put(Bytes.toBytes("user:123"));
        put.addColumn(Bytes.toBytes("cf1"),
                Bytes.toBytes("name"),
                Bytes.toBytes("lisi"));
        put.addColumn(Bytes.toBytes("cf1"),
                Bytes.toBytes("salary"),
                Bytes.toBytes(1234));
        hTable.put(put);
        hTable.flushCommits();
        hTable.close();
    }

    //删除数据
    @Test
    public void testdeletetables() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        HTable hTable =(HTable)connection.getTable(tableName);
        Delete delete = new Delete("user:123".getBytes());
        hTable.delete(delete);
        hTable.close();
    }

    //查询数据
    @Test
    public void testshowtable() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        HTable hTable = (HTable)connection.getTable(tableName);
        Get get = new Get("user:123".getBytes());
        get.setMaxVersions(10);//设置要查的版本
        Result result = hTable.get(get);
        CellScanner cellScanner = result.cellScanner();
        while (cellScanner.advance()){
            Cell cell = cellScanner.current();
            String qualifier= Bytes.toString(CellUtil.cloneQualifier(cell));
            String famliy= Bytes.toString(CellUtil.cloneFamily(cell));
            String value=Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(famliy+":"+qualifier+" "+value);
        }
    }

    //查询数据
    @Test
    public void testshowtable1() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        HTable hTable =(HTable)connection.getTable(tableName);
        Get get = new Get("user:123".getBytes());
        get.setMaxVersions();
        Result result = hTable.get(get);
        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
            System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
        }
        hTable.close();
    }

    //查询数据
    @Test
    public void testshowtable2() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        HTable hTable = (HTable)connection.getTable(tableName);
        Get get = new Get("user:123".getBytes());
        Result result = hTable.get(get);
        List<Cell> columnCells = result.getColumnCells(Bytes.toBytes("cf1"), Bytes.toBytes("name"));
        for (Cell columnCell : columnCells) {
            System.out.println(Bytes.toString(CellUtil.cloneQualifier(columnCell)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(columnCell)));
        }
        hTable.close();
    }

    //数据查询
    @Test
    public void testshowtable3() throws IOException {
        TableName tableName = TableName.valueOf("dd:t_user");
        HTable hTable  = (HTable)connection.getTable(tableName);
        Scan scan = new Scan();
        ResultScanner scanner = hTable.getScanner(scan);
        for (Result result : scanner) {
            List<Cell> columnCells = result.getColumnCells(Bytes.toBytes("cf1"), Bytes.toBytes("name"));
            for (Cell columnCell : columnCells) {
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(columnCell)));
                System.out.println(Bytes.toString(CellUtil.cloneValue(columnCell)));
            }
        }
        hTable.close();
    }

    @After
    public void after() throws IOException {
        admin.close();
        connection.close();
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值