• HBase-Java-API操作表


    package com.itheima;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.*;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.filter.*;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class HBaseStudy {
        private Connection connection;
        private Table myuser;
    
        @Before
        public void init() throws IOException {
            //构建conf对象
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
            //创建数据库连接
            connection = ConnectionFactory.createConnection(conf);
            //System.out.println(connection);
            //connection.close();
            myuser = connection.getTable(TableName.valueOf("myuser"));
    
    
        }
    
        @Test
        public void createTable() throws IOException {
            Admin admin = connection.getAdmin();
            //定义表名
            TableName tableName = TableName.valueOf("myuser");
            //定义描述符
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            //构建列族
            HColumnDescriptor f1 = new HColumnDescriptor("f1");
            HColumnDescriptor f2 = new HColumnDescriptor("f2");
            hTableDescriptor.addFamily(f1);
            hTableDescriptor.addFamily(f2);
            //创建表
            admin.createTable(hTableDescriptor);
            admin.close();
        }
    
        @Test
        public void putData() throws IOException {
            //获取表操作对象
            //Table myuser = connection.getTable(TableName.valueOf("myuser"));
            //创建put对象
            Put put = new Put("0001".getBytes());
            put.addColumn("f1".getBytes(), "id".getBytes(), "0001".getBytes());
            put.addColumn("f1".getBytes(), "name".getBytes(), "zhangsan".getBytes());
            put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(18));
            put.addColumn("f2".getBytes(), "phone".getBytes(), "13898941437".getBytes());
            put.addColumn("f1".getBytes(), "address".getBytes(), "葫芦岛".getBytes());
            myuser.put(put);
            // myuser.close();
        }
    
        @Test
        public void insertBatchData() throws IOException {
    
            //创建put对象,并指定rowkey
            Put put = new Put("0002".getBytes());
            put.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(1));
            put.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("曹操"));
            put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(30));
            put.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
            put.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("沛国谯县"));
            put.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("16888888888"));
            put.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("helloworld"));
    
            Put put2 = new Put("0003".getBytes());
            put2.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(2));
            put2.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("刘备"));
            put2.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(32));
            put2.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
            put2.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("幽州涿郡涿县"));
            put2.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("17888888888"));
            put2.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("talk is cheap , show me the code"));
    
    
            Put put3 = new Put("0004".getBytes());
            put3.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(3));
            put3.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("孙权"));
            put3.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(35));
            put3.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
            put3.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("下邳"));
            put3.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("12888888888"));
            put3.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("what are you 弄啥嘞!"));
    
            Put put4 = new Put("0005".getBytes());
            put4.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(4));
            put4.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("诸葛亮"));
            put4.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(28));
            put4.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
            put4.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("四川隆中"));
            put4.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("14888888888"));
            put4.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("出师表你背了嘛"));
    
            Put put5 = new Put("0005".getBytes());
            put5.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(5));
            put5.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("司马懿"));
            put5.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(27));
            put5.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
            put5.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("哪里人有待考究"));
            put5.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15888888888"));
            put5.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("跟诸葛亮死掐"));
    
    
            Put put6 = new Put("0006".getBytes());
            put6.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(5));
            put6.addColumn("f1".getBytes(), "name".getBytes(), Bytes.toBytes("xiaobubu—吕布"));
            put6.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(28));
            put6.addColumn("f2".getBytes(), "sex".getBytes(), Bytes.toBytes("1"));
            put6.addColumn("f2".getBytes(), "address".getBytes(), Bytes.toBytes("内蒙人"));
            put6.addColumn("f2".getBytes(), "phone".getBytes(), Bytes.toBytes("15788888888"));
            put6.addColumn("f2".getBytes(), "say".getBytes(), Bytes.toBytes("貂蝉去哪了"));
    
            List<Put> listPut = new ArrayList<Put>();
            listPut.add(put);
            listPut.add(put2);
            listPut.add(put3);
            listPut.add(put4);
            listPut.add(put5);
            listPut.add(put6);
            myuser.put(listPut);
        }
        @Test
        public void  getData() throws IOException {
            //创建一个get对象,用于获取一条数据
            Get get = new Get("0006".getBytes());
            //设置过滤条件
            get.addColumn("f1".getBytes(),"name".getBytes());
            get.addColumn("f2".getBytes(),"address".getBytes());
            //返回数据的结果集
            Result result = myuser.get(get);
            List<Cell> cells = result.listCells();
            //第一种方式
           /* for (Cell cell : cells) {
                byte[] rowKey = cell.getRow();
                byte[] family = cell.getFamily();
                byte[] qualifier = cell.getQualifier();
                byte[] value = cell.getValue();
                if(Bytes.toString(qualifier).equals("id")||Bytes.toString(qualifier).equals("age")){
                    System.out.println("rowkey:"+Bytes.toString(rowKey)+",family:"+Bytes.toString(family)+
                            ",qualifier:"+Bytes.toString(qualifier)+",value:"+Bytes.toInt(value));
                }else {
                    System.out.println("rowkey:"+Bytes.toString(rowKey)+",family:"+Bytes.toString(family)+
                            ",qualifier:"+Bytes.toString(qualifier)+",value:"+Bytes.toString(value));
                }
            }*/
           //第二种方式
            for (Cell cell : cells) {
                String rowkey=Bytes.toString(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength());
                String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                String qualifier=Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                String value=Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
                System.out.println("RowKey:"+rowkey+",family:"+family+
                        ",qualifier:"+qualifier+",value:"+value);
            }
        }
        @Test
        public void scanData() throws IOException {
            //全表扫描
            Scan scan = new Scan();
            scan.setStartRow("0001".getBytes());
            scan.setStopRow("0003".getBytes());
            ResultScanner results = myuser.getScanner(scan);
            for (Result result : results) {
                List<Cell> cells = result.listCells();
                for (Cell cell : cells) {
                    String family=Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
                    String qualifier=Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                    String value=Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
                    System.out.println("family:"+family+
                            ",qualifier:"+qualifier+",value:"+value);
                }
            }
        }
        @Test
        public void getDataByFilter() throws IOException {
            Scan scan = new Scan();
            //1。RowFilter
            RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator("0003".getBytes()));
            //2. FamilyFilter
            FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new SubstringComparator("f2"));
            //3. QualifierFilter
            //QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("name".getBytes()));
            //4. ValueFilter
            //ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));
            //5.SingleColumnValueFilter
            //SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
            //6.PrefixFilter
            //PrefixFilter prefixFilter = new PrefixFilter("0001".getBytes());
            //7.通过filterlist设置多过滤器
            FilterList filterList = new FilterList();
            filterList.addFilter(rowFilter);
            filterList.addFilter(familyFilter);
            scan.setFilter(filterList);
            ResultScanner scanner = myuser.getScanner(scan);
            for (Result result : scanner) {
                List<Cell> cells = result.listCells();
                for (Cell cell : cells) {
                    byte[] rowkey = CellUtil.cloneRow(cell);
                    byte[] family = CellUtil.cloneFamily(cell);
                    byte[] qualifier = CellUtil.cloneQualifier(cell);
                    byte[] value = CellUtil.cloneValue(cell);
                    //判断数据类型是否是age或者id
                    if(Bytes.toString(qualifier).equals("id")||Bytes.toString(qualifier).equals("age")){
                        System.out.println("rowkey:"+Bytes.toString(rowkey)+",family:"+Bytes.toString(family)+
                                ",qualifier:"+Bytes.toString(qualifier)+",value:"+Bytes.toInt(value));
                    }else {
                        System.out.println("rowkey:"+Bytes.toString(rowkey)+",family:"+Bytes.toString(family)+
                                ",qualifier:"+Bytes.toString(qualifier)+",value:"+Bytes.toString(value));
                    }
                }
            }
        }
        @Test
        public void getDataByPageFilter() throws IOException {
            int pagsize = 2;
            int pagnum = 3;
            //如果只有一页的情况下
            if(pagnum==1){
                Scan scan = new Scan();
                scan.setFilter(new PageFilter(pagsize));
                scan.setMaxResultSize(pagsize);
                ResultScanner scanner = myuser.getScanner(scan);
                for (Result result : scanner) {
                    List<Cell> cells = result.listCells();
                    for (Cell cell : cells) {
                        System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
                    }
                }
            }else {
                //首先获取第三页的rowkey
                //可以分一页 ,每页显示5条,直接获取最后一条rowkey
                Scan scan1 = new Scan();
                scan1.setStartRow("".getBytes());//rowkey的默认值
                scan1.setFilter(new PageFilter((pagnum-1)*pagsize + 1));
                scan1.setMaxResultSize((pagnum-1)*pagsize + 1);
                ResultScanner scanner = myuser.getScanner(scan1);
                //定义startRow接收变量
                String startRowkey="";
                for (Result result : scanner) {
                    List<Cell> cells = result.listCells();
                    for (Cell cell : cells) {
                        byte[] rowkey = CellUtil.cloneRow(cell);
                        startRowkey = Bytes.toString(rowkey);
                    }
                }
                //System.out.println(startRowkey);
                //基于startrowkey进行第三页的分页
                Scan scan2 = new Scan();
                scan2.setStartRow(startRowkey.getBytes());
                scan2.setFilter(new PageFilter(pagsize));
                scan2.setMaxResultSize(pagsize);
                ResultScanner scanner1 = myuser.getScanner(scan2);
                for (Result result : scanner1) {
                    List<Cell> cells = result.listCells();
                    for (Cell cell : cells) {
                        System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
                    }
                }
    
            }
        }
    
        @Test
        public void deleteData() throws IOException {
    
            Delete delete = new Delete("0001".getBytes());
            myuser.delete(delete);
        }
    
    
        @Test
        public void dropTable() throws IOException {
    
            Admin admin = connection.getAdmin();
            if(!admin.isTableDisabled(TableName.valueOf("user1"))){
                admin.disableTable(TableName.valueOf("user1"));
            }
            if (admin.tableExists(TableName.valueOf("user1"))){
                admin.deleteTable(TableName.valueOf("user1"));
            }
    
            admin.close();
        }
    
        @After
        public void close() throws IOException {
            myuser.close();
            connection.close();
        }
    }
    View Code
  • 相关阅读:
    多线程编程之线程死锁问题 转载
    线程的挂起和恢复 转载
    redis 内存库设置 教你怎么解决64位Windows版Redis狂占C盘的问题.
    MSSQL数据库分区表
    如何将桌面的路径定义到其它盘符,如d:users桌面
    sqlserver数据库脱机时发生异常:由于无法在数据库 'SMS' 上放置锁,ALTER DATABASE 失败。请稍后再试。 ALTER DATABASE 语句失败。 (.Net SqlClient Data Provider)
    JAVA代码反编译笔记
    SQLServer—系统中的内存配置
    .net remoting 客户端与服务端绑定事件,一部电脑当服务器,另一部当客户端,发布后没法接收远程错误信息。
    Java Socket 模拟HTTP请求
  • 原文地址:https://www.cnblogs.com/qidi/p/11677512.html
Copyright © 2020-2023  润新知