Hbase操作:添加数据,查询数据,扫描数据,删除行,删除表

本文介绍了Hbase的基本操作,包括如何添加数据、查询数据、扫描数据、删除行以及删除整个表。通过这些操作,可以全面理解Hbase的数据管理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hbase操作:添加数据,查询数据,扫描数据,删除行,删除表

  1. 添加数据

    public static void set() throws IOException {
        System.out.println("---------------------------set-----------------------");
        Configuration config = HBaseConfiguration.create();
        // 创建表简写
        HTable table = new HTable(config, "test3");
        // put对象实现添加数据,并指定row 参数为字节数组
        Put put1 = new Put(Bytes.toBytes("row1"));
        // 添加列族,列,值
        put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes(1), Bytes.toBytes("Hello1"));
        put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes(2), Bytes.toBytes("Hello2"));
        put1.addColumn(Bytes.toBytes("data"), Bytes.toBytes(3), Bytes.toBytes("Hello3"));
        table.put(put1);
    
        Put put2 = new Put(Bytes.toBytes("row2"));
        // 添加列族,列,值
        put2.addColumn(Bytes.toBytes("data"), Bytes.toBytes(1), Bytes.toBytes("Hello1"));
        put2.addColumn(Bytes.toBytes("data"), Bytes.toBytes(2), Bytes.toBytes("Hello2"));
        put2.addColumn(Bytes.toBytes("data"), Bytes.toBytes(3), Bytes.toBytes("Hello3"));
        table.put(put2);
        table.close();
    }
    
  2. 查询数据

    public static void get() throws IOException {
        System.out.println("---------------------------get-----------------------");
        // 创建Hbase的配置文件,是Hadoop的子类
        Configuration config = HBaseConfiguration.create();
        // 创建表简写
        HTable table = new HTable(config, "test3");
        // get对象实现查询数据,并指定row 参数为字节数组 ,封装查询
    
        // 静态查询
        Get get = new Get(Bytes.toBytes("row1")); // 添加列族,列,值
        get.addColumn(Bytes.toBytes("data"), Bytes.toBytes(1));
        get.addColumn(Bytes.toBytes("data"), Bytes.toBytes(2));
        Result result = table.get(get);
        KeyValue kv = result.getColumnLatest(Bytes.toBytes("data"), Bytes.toBytes(1));
        System.out.println(Bytes.toString(kv.getKey()) + "-------------" + kv.getValue());
        table.close();
    }
    
  3. 扫描数据

    /**
     * 扫描表
     * 
     * @throws IOException
     */
    public static void scan() throws IOException {
        System.out.println("---------------------------scan-----------------------");
        // 创建Hbase的配置文件,是Hadoop的子类
        Configuration config = HBaseConfiguration.create();
        // 创建表简写
        HTable table = new HTable(config, "test3");
    
        Scan scan = new Scan();
        ResultScanner rs = table.getScanner(scan);
        for (Result result : rs) {
            System.out.println(Bytes.toString(result.getRow()) + ":"
                    + Bytes.toShort(result.getColumnLatestCell(Bytes.toBytes("data"), Bytes.toBytes(1)).getValue()));
        }
        rs.close();
        table.close();
    }
    
  4. 删除行

    /**
     * 删除行
     * 
     * @throws IOException
     */
    public static void delete() throws IOException {
        System.out.println("---------------------------delete-----------------------");
        // 创建Hbase的配置文件,是Hadoop的子类
        Configuration config = HBaseConfiguration.create();
        // 创建表简写
        HTable table = new HTable(config, "test3");
        Delete d = new Delete(Bytes.toBytes("row2"));
        table.delete(d);
        table.close();
    }
    
  5. 删除表

    /**
     * 删除表
     * 
     * @throws IOException
     */
    public static void DropTable() throws IOException {
        System.out.println("---------------------------DropTable-----------------------");
        // 创建Hbase的配置文件,是Hadoop的子类
        Configuration config = HBaseConfiguration.create();
        // 需要管理员
        HBaseAdmin admin = new HBaseAdmin(config);
        admin.disableTable("test3");
        admin.deleteTable("test3");
        admin.close();
    }
    

运行结果如下

[root@master local]# hbase com.xt.hbase.HbaseApp
---------------------------create-----------------------
2017-07-17 18:00:01,246 WARN  [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/hbase/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2017-07-17 18:00:03,592 INFO  [main] zookeeper.RecoverableZooKeeper: Process identifier=hconnection-0x2925bf5b connecting to ZooKeeper ensemble=slave1:2181,slave2:2181
2017-07-17 18:00:03,614 INFO  [main] zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2017-07-17 18:00:03,614 INFO  [main] zookeeper.ZooKeeper: Client environment:host.name=master
2017-07-17 18:00:03,614 INFO  [main] zookeeper.ZooKeeper: Client environment:java.version=1.8.0_131
2017-07-17 18:00:03,614 INFO  [main] zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation
2017-07-17 18:00:03,614 INFO  [main] zookeeper.ZooKeeper: Client environment:java.home=/usr/local/java/jre
2017-07-17 18:00:03,614 INFO  [main] zookeeper.ZooKeeper: Client environment:java.class.path=/usr/local/hbase/conf:/usr/local/java/lib/tools.jar:/usr/local/hbase:/usr/local/hbase/lib/activation-1.1.jar:/usr/local/hbase/lib/aopalliance-1.0.jar:/usr/local/hbase/lib/apacheds-i18n-2.0.0-M15.jar:/usr/local/hbase/lib/apacheds-kerberos-codec-2.0.0-M15.jar:/usr/local/hbase/lib/api-asn1-api-1.0.0-M20.jar:/usr/local/hbase/lib/api-util-1.0.0-M20.jar:/usr/local/hbase/lib/asm-3.1.jar:/usr/local/hbase/lib/avro-1.7.4.jar:/usr/local/hbase/lib/commons-beanutils-1.7.0.jar:/usr/local/hbase/lib/commons-beanutils-core-1.8.0.jar:/usr/local/hbase/lib/commons-cli-1.2.jar:/usr/local/hbase/lib/commons-codec-1.9.jar:/usr/local/hbase/lib/commons-collections-3.2.2.jar:/usr/local/hbase/lib/commons-compress-1.4.1.jar:/usr/local/hbase/lib/commons-configuration-1.6.jar:/usr/local/hbase/lib/commons-daemon-1.0.13.jar:/usr/local/hbase/lib/commons-digester-1.8.jar:/usr/local/hbase/lib/commons-el-1.0.jar:/usr/local/hbase/lib/commons-httpclient-3.1.jar:/usr/local/hbase/lib/commons-io-2.4.jar:/usr/local/hbase/lib/commons-lang-2.6.jar:/usr/local/hbase/lib/commons-logging-1.2.jar:/usr/local/hbase/lib/commons-math-2.2.jar:/usr/local/hbase/lib/commons-math3-3.1.1.jar:/usr/local/hbase/lib/commons-net-3.1.jar:/usr/local/hbase/lib/disruptor-3.3.0.jar:/usr/local/hbase/lib/findbugs-annotations-1.3.9-1.jar:/usr/local/hbase/lib/guava-12.0.1.jar:/usr/local/hbase/lib/guice-3.0.jar:/usr/local/hbase/lib/guice-servlet-3.0.jar:/usr/local/hbase/lib/hadoop-anno
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值