hbase学习-- 5 使用HBase客户端API读取,删除数据,扫描数据

本文介绍了如何使用HBase客户端API进行数据操作。首先讲解了通过HTable的get()方法读取特定列族的特定列数据,接着展示了删除数据的步骤,特别是使用delete()方法默认删除行号为1的所有数据。最后,文章阐述了扫描数据的过程,包括展示的keyvalues格式,如keyvalues={行号/列族:列/时间戳/Put/vlen=长度/seqid=序列号},并提到了扫描器关闭的情况。

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

1 读取数据

使用HTable类中的get()方法。这种方法需要Get类的一个实例。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class ReadTable {

    public static void main(String[] args) throws IOException {

       //实例化Configuration类
        Configuration conf = HBaseConfiguration.create();

        //实例化HTable类
        HTable hTable = new HTable(conf, "People");

        //实例化获得类
        Get get = new Get(Bytes.toBytes("1"));
     
	//选择获取的数据   
	get.addFamily(Bytes.toBytes("professional data")); //
     get.addColumn(Bytes.toBytes("personal data"),Bytes.toBytes("name"));
     get.addColumn(Bytes.toBytes("personal data"),Bytes.toBytes("age"));
        
        //获取结果
        Result result = hTable.get(get);

        
        byte[] name = result.getValue(Bytes.toBytes("personal data"), Bytes.toBytes("name"));

        byte[] age = result.getValue(Bytes.toBytes("personal data"), Bytes.toBytes("age"));

        System.out.println("name: " + Bytes.toString(name) + " age: " + Bytes.toString(age));
    }
}
注意代码中间高亮的部分为选择获取的数据: 要得到一个特定的列族的所有列,使用 get.addFamily(Bytes.toBytes("professional data"))方法,

要得到一个特定的列族的特定列,使用 get.addColumn(Bytes.toBytes("professional data"),Bytes.toBytes("name"))方法,

如果不采用高亮部分的代码的话,使用hTable.get(get)方法得到的结果 result 默认获取表中行号为 1 的所有数据。


2  删除数据

    使用HTable类的delete()方法删除HBase表数据

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class DeleteTable {
    public static void main(String[] args) throws IOException {

        //实例化HBaseConfiguration
        Configuration conf = HBaseConfiguration.create();

        //实例化HTable 
        HTable hTable = new HTable(conf,"People");

        //实例化Delete
        Delete delete = new Delete(Bytes.toBytes("1"));

        //选择要删除的数据
        delete.addFamily(Bytes.toBytes("professional data"));

        delete.addColumn(Bytes.toBytes("personal data"),Bytes.toBytes("name"));

        //删除数据
        hTable.delete(delete);

        hTable.close();

        System.out.println("data deleted");
    }
}

如果不选择要删除的数据的话,默认删除表中 行号为 1 的所有数据


3 扫描数据

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
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 java.io.IOException;

public class ScanTable {
    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();

        HTable hTable = new HTable(conf, "People");

        Scan scan = new Scan();

        scan.addFamily(Bytes.toBytes("personal data"));

        ResultScanner scanner = hTable.getScanner(scan);
        for (Result result = scanner.next(); result != null; result = scanner.next()) {
            System.out.println(result);
        }
        scanner.close();
        System.out.println("scanner closed");
    }
}

输出结果:

keyvalues={1/personal data:name/1509697491235/Put/vlen=3/seqid=0}
keyvalues={2/personal data:name/1509697491247/Put/vlen=3/seqid=0}
scanner closed




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值