java的api从hbase中读数据

10 篇文章 0 订阅

package hbase.com.cn.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
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;

public class TestHBase {
static Configuration conf=null;
static Connection conn=null;
public static void getConnection() {
conf=HBaseConfiguration.create();
conf.set(“hbase.zookeeper.quorum”, “centos01:2181,centos02:2181,centos03:2181”);
try {
conn = ConnectionFactory.createConnection(conf);
//table=(HTable)conn.getTable(TableName.valueOf(“user_info”));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void deletetable() throws Exception {
HTable table=new HTable(conf,Bytes.toBytes(“user_info”));
Listlist=new ArrayList<>();
for(int i=0;i<10;i++) {
Delete delete=new Delete((“baiyc_20150716_000”+i).getBytes());
if(i%2==0) {
delete.addColumn(“base_info”.getBytes(), “age”.getBytes());
}else {
//行簇 baiyc_20150716_000i 列簇 base_info 字段 name
delete.addColumn(“base_info”.getBytes(), “name”.getBytes());
}
list.add(delete);
}
table.delete(list);
}
public static void scandata() throws Exception {
HTable table=new HTable(conf,Bytes.toBytes(“user”));
Scan scan=new Scan();//不传参全表扫描
//Scan scan=new Scan(“rk001”.getBytes());
//scan.addColumn(“info1”.getBytes(),“id”.getBytes());
ResultScanner scanner=table.getScanner(scan);
Iterator iterator=scanner.iterator();
while(iterator.hasNext()) {
Result next=iterator.next();
List list=next.list();
for(KeyValue kv:list){
System.out.print(new String(kv.getRow())+"\t");
System.out.print(new String(kv.getFamily())+"\t");
System.out.print(new String(kv.getQualifier())+"\t");
System.out.println(new String(kv.getValue()));
}
}
}
public static void main(String[] args) throws Exception {
getConnection();
//deletetable();
scandata();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值