hbase学习笔记-增删改查操作

  • 创建Maven工程,添加依赖
<dependencies>
          <dependency>
              <groupId>org.apache.hbase</groupId>
              <artifactId>hbase-client</artifactId>
              <version>1.2.1</version>
          </dependency>
          <dependency>
              <groupId>org.apache.hbase</groupId>
              <artifactId>hbase-common</artifactId>
              <version>1.2.1</version>
          </dependency>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
          </dependency>
      </dependencies>
  • hbase表的增删改查操作

1、初始化一个init方法

public class TestHbase {

        //声明一个Configuration对象
        private Configuration conf=null;
        //声明一个hbase数据库连接
        private Connection conn = null;
       
        @Before
        public void init() throws Exception{
        conf= HBaseConfiguration.create();
  
       // 对于hbase的客户端来说,只需要知道hbase所使用的zookeeper集群地址就可以了
       conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,node3:2181");

       //获取链接
      conn= ConnectionFactory.createConnection(conf);
        
	}
}  

2、创建一个表

 /**
         * 建表
         * @throws Exception
         *  hbase shell------> create 'tableName','列族1','列族2'
         */
        @Test
        public void createTable() throws Exception{
            //获取一个表的管理器
            Admin admin = conn.getAdmin();
            //构造一个表描述器,并指定表名
            HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("t_user_info".getBytes()));
            //构造一个列族描述器,并指定列族名
            HColumnDescriptor hcd1 = new HColumnDescriptor("base_f1");
            // 构造第二个列族描述器,并指定列族名
            HColumnDescriptor hcd2 = new HColumnDescriptor("base_f2");
            // 为该列族设定一个版本数量
            hcd2.setVersions(1, 3);

            // 将列族描述器添加到表描述器中
            tableDescriptor.addFamily(hcd1).addFamily(hcd2);

            //利用表的管理器创建表
            admin.createTable(tableDescriptor);
            //关闭
            admin.close();
            conn.close();
        }

3、修改表属性

/**
         * 修改表
         * @throws Exception
         */   //hbase shell   alter 't_user_info' ,'base_info',
              //   alter 't1', NAME => 'f1', VERSIONS => 5
        @Test
        public void modifyTable() throws Exception{
            //获取一个表的管理器
            Admin admin = conn.getAdmin();
            //获取表的描述器
            HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf("t_user_info"));
            //修改已有的ColumnFamily---extra_info最小版本数和最大版本数
            HColumnDescriptor hcd1 = tableDescriptor.getFamily("extra_info".getBytes());
            hcd1.setVersions(2,5);

            // 添加新的ColumnFamily
            tableDescriptor.addFamily(new HColumnDescriptor("other_info"));

            //表的管理器admin 修改表
            admin.modifyTable(TableName.valueOf("t_user_info"),tableDescriptor);
            //关闭
            admin.close();
            conn.close();
        }

4、put添加数据

/**
        *  put添加数据
        * @throws Exception     hbase shell  put 't_user_info','rk00001','base_info:name','lisi'
        */
       @Test
       public void testPut() throws Exception {
           //构建一个 table对象,通过table对象来添加数据
           Table table = conn.getTable(TableName.valueOf("t_user_info"));
           //创建一个集合,用于存放Put对象
           ArrayList<Put> puts = new ArrayList<Put>();

           // 构建一个put对象(kv),指定其行键  例如hbase shell:  put '表名','rowkey','列族:列名称','值'
           Put put01 = new Put(Bytes.toBytes("user001"));  // "user001".getBytes()
           put01.addColumn(Bytes.toBytes("base_f1"), Bytes.toBytes("username"), Bytes.toBytes("zhangsan"));

           Put put02 = new Put("user001".getBytes());
           put02.addColumn(Bytes.toBytes("base_f1"), Bytes.toBytes("password"), Bytes.toBytes("123456"));

           Put put03 = new Put("user002".getBytes());
           put03.addColumn(Bytes.toBytes("base_f1"), Bytes.toBytes("username"), Bytes.toBytes("lisi"));
           put03.addColumn(Bytes.toBytes("base_f2"), Bytes.toBytes("married"), Bytes.toBytes("false"));

         
           //把所有的put对象添加到一个集合中
           puts.add(put01);
           puts.add(put02);
           puts.add(put03);
        
           //一起提交所有的记录
           table.put(puts);
           table.close();
           conn.close();

       }

5、scan 查询单条数据

        /**
       *  读取数据  get:一次读一行
       * @throws Exception       
       *   hbase shell  :     get 't_user_info',"rowkey"
       */
      @Test
      public void testGet() throws Exception {
          //获取一个table对象
          Table table = conn.getTable(TableName.valueOf("t_user_info"));

          // 构造一个get查询参数对象,指定要get的是哪一行
          Get get = new Get("user001".getBytes());
          //返回查询结果数据
          Result result = table.get(get);
          //获取结果中的所有cell
          List<Cell> cells = result.listCells();
          //遍历所有的cell
          for(Cell c:cells){
  			//获取行键
              byte[] rowBytes = CellUtil.cloneRow(c);
              //获取列族
              byte[] familyBytes = CellUtil.cloneFamily(c);
              //获取列族下的列名称
              byte[] qualifierBytes = CellUtil.cloneQualifier(c);
              //列字段的值
              byte[] valueBytes = CellUtil.cloneValue(c);
              
              System.out.print(new String(rowBytes)+" ");
              System.out.print(new String(familyBytes)+":");
              System.out.print(new String(qualifierBytes)+" ");
              System.out.println(new String(valueBytes));

          }

          //关闭
          table.close();
          conn.close();

      }

6、scan 批量查询数据

/**
         * scan 批量查询数据
         * @throws Exception   hbase shell  scan 't_user_info'
         */
        @Test
        public void testScan() throws Exception {
            //获取table对象
            Table table = conn.getTable(TableName.valueOf("t_user_info"));
            //获取scan对象
            Scan scan = new Scan();
            //获取查询的数据
            ResultScanner scanner = table.getScanner(scan);
            //获取ResultScanner所有数据,返回迭代器
            Iterator<Result> iter = scanner.iterator();
            //遍历迭代器
            while (iter.hasNext()) {
                //获取当前每一行结果数据
                Result result = iter.next();
                //获取当前每一行中所有的cell对象
                List<Cell> cells = result.listCells();
                //迭代所有的cell
                for(Cell c:cells){
                    //获取行键
                    byte[] rowBytes = CellUtil.cloneRow(c);
                    //获取列族
                    byte[] familyBytes = CellUtil.cloneFamily(c);
                    //获取列族下的列名称
                    byte[] qualifierBytes = CellUtil.cloneQualifier(c);
                    //列字段的值
                    byte[] valueBytes = CellUtil.cloneValue(c);

                    System.out.print(new String(rowBytes)+" ");
                    System.out.print(new String(familyBytes)+":");
                    System.out.print(new String(qualifierBytes)+" ");
                    System.out.println(new String(valueBytes));
                }
                System.out.println("-----------------------");
            }

            //关闭
            table.close();
            conn.close();

        }

7、delete删除表中的列数据

/**
        * 删除表中的列数据
        * @throws Exception     
        * hbase shell  delete 't_user_info','user001','base_info:password'
        */
        @Test
        public void testDel() throws Exception {
            //获取table对象
            Table table = conn.getTable(TableName.valueOf("t_user_info"));
            //获取delete对象,需要一个rowkey
            Delete delete = new Delete("user001".getBytes());
            //在delete对象中指定要删除的列族-列名称
        delete.addColumn("base_info".getBytes(), "password".getBytes());
            //执行删除操作
            table.delete(delete);

            //关闭
            table.close();
            conn.close();
        }

8、删除表

       /**
         * 删除表
         * @throws Exception   
         * hbase shell   先disable 't_user_info'   然后drop 't_user_info'
         */
        @Test
        public void testDrop() throws Exception {
            //获取一个表的管理器
            Admin admin = conn.getAdmin();
            //删除表时先需要disable,将表置为不可用,然后在delete
            admin.disableTable(TableName.valueOf("t_user_info"));
            admin.deleteTable(TableName.valueOf("t_user_info"));
            admin.close();
            conn.close();
    }
发布了40 篇原创文章 · 获赞 59 · 访问量 1414

猜你喜欢

转载自blog.csdn.net/qq_26719997/article/details/104959882