温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

HBase中怎么操作API

发布时间:2021-07-27 16:03:25 来源:亿速云 阅读:143 作者:Leah 栏目:云计算

本篇文章给大家分享的是有关HBase中怎么操作API,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

【常用到的几个类】

1. org.apache.hadoop.hbase.HBaseConfiguration

每一个hbase client都会使用到的对象,它代表的是HBase配置信息。它有两种构造方式:

public HBaseConfiguration()
public HBaseConfiguration(final Configuration c)

默认的构造方式会尝试从hbase-default.xml和hbase-site.xml中读取配置。如果classpath没有这两个文件,就需要你自己设置配置。

Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set(“hbase.zookeeper.quorum”, “zkServer”);
HBASE_CONFIG.set(“hbase.zookeeper.property.clientPort”, “2181″);
HBaseConfiguration cfg = new HBaseConfiguration(HBASE_CONFIG);

2. org.apache.hadoop.hbase.client.HBaseAdmin
提供了一个接口来管理HBase数据库的表信息。它提供的方法包括:创建表,删除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。
HBase中怎么操作API

3. org.apache.hadoop.hbase.HTableDescriptor 
包含了表的名字极其对应表的列族。 
常用方法:void addFamily(HcolumnDescriptor family) 添加一个列族。其详细用法如下所示,向tb_user表中添加了一个content列族。

HTableDescriptor tableDescriptor = new HTableDescriptor("tb_user");  
HColumnDescriptor col = new HColumnDescriptor("content:");  
tableDescriptor.addFamily(col);  

  
4. org.apache.hadoop.hbase.HColumnDescriptor 
作用:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。列族被删除的时候,列族里面的数据也会同时被删除。
  
5. org.apache.hadoop.hbase.client.HTable 
作用:可以用来和HBase表直接通信。此方法对于更新操作来说是非线程安全的。 

HBase中怎么操作API

  
7. org.apache.hadoop.hbase.client.Get 
作用:用来获取单个行的相关信息

【实战】

package com.youku.test;

import java.util.Iterator;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

/**
 * HBase Java API Test Demo.
 */
public class HbaseDemo {

       private Configuration conf = null;

       /**
        * 初始化
        */
       @Before
       public void init() {
              conf = HBaseConfiguration.create();
              conf.set("hbase.zookeeper.quorum", "zk01,zk02,zk03");
       }

       /**
        * 删除表
        * @throws Exception
        */
       @Test
       public void testDrop() throws Exception {
              HBaseAdmin admin = new HBaseAdmin(conf);
              admin.disableTable("yk_test");
              admin.deleteTable("yk_test");
              admin.close();
       }

       /**
        * 插入数据
        * @throws Exception
        */
       @Test
       public void testPut() throws Exception {
              HTable table = new HTable(conf, "person_info");
              Put p = new Put(Bytes.toBytes("person_rk_bj_zhang_000002"));
              p.add("base_info".getBytes(), "name".getBytes(), "zhangwuji".getBytes());
              table.put(p);
              table.close();
       }

       /**
        * 删除某列
        * @throws Exception
        */
       @Test
       public void testDel() throws Exception {
              HTable table = new HTable(conf, "user");
              Delete del = new Delete(Bytes.toBytes("rk0001"));
              del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
              table.delete(del);
              table.close();
       }

       /**
        * 单条查询
        * @throws Exception
        */
       @Test
       public void testGet() throws Exception {
              HTable table = new HTable(conf, "person_info");
              Get get = new Get(Bytes.toBytes("person_rk_bj_zhang_000001"));
              get.setMaxVersions(5);
              Result result = table.get(get);

              List<Cell> cells = result.listCells();

              for (Cell c : cells) {
              }

              // result.getValue(family, qualifier); 可以从result中直接取出一个特定的value
              // 遍历出result中所有的键值对
              List<KeyValue> kvs = result.list();
              // kv ---> f1:title:superise.... f1:author:zhangsan f1:content:asdfasldgkjsldg
              for (KeyValue kv : kvs) {
                     String family = new String(kv.getFamily());
                     System.out.println(family);
                     String qualifier = new String(kv.getQualifier());
                     System.out.println(qualifier);
                     System.out.println(new String(kv.getValue()));

              }
              table.close();
       }

       /***
        * 遍历表
        * @throws Exception
        */
       @Test
       public void testScan() throws Exception {
              HTable table = null;
              try {
                     table = new HTable(conf, "person_info");
                     Scan scan = new Scan();
                     scan.addFamily(Bytes.toBytes("v"));
                     ResultScanner rs = table.getScanner(scan);
                     Iterator<Result> it = rs.iterator();

                     while (it.hasNext()) {
                            Result result = it.next();
                            if (result != null && result.size() > 0) {
                                   byte[] row = result.getRow();
                                   String rowStr = Bytes.toString(row); // rowkey
                                   System.out.println("rowkey:" + rowStr);
                                   byte[] value = result.getValue(Bytes.toBytes("v"), Bytes.toBytes("c"));
                                   if(value != null){
                                          long count = Bytes.toLong(value); // value
                                          System.out.println("colum value:" + count);
                                   }
                                   
                            }
                     }
              } catch (Exception e) {
                     e.printStackTrace();
              } finally {
                     if (table != null) {
                            try {
                                   table.close();
                            } catch (Exception e2) {
                                   e2.printStackTrace();
                            }
                     }
              }
       }

【补充说明】

在使用scan操作时,由于HBase表一般很大,往往需要结合过滤器使用,详细参考《HBase--常用过滤器篇》,另外,若在scan时指定了startRow和stopRow时,结果不包含stopRow,但是包含startRow,且startRow和stopRow支持部分匹配,实际应用中若rowkey设计比较复杂,由多部分组成,可以用这种方式查询符合条件的行。

以上就是HBase中怎么操作API,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI