您的位置:首页 > 其它

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

2017-11-03 14:44 525 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: