1. 程式人生 > >java的api從hbase中讀資料

java的api從hbase中讀資料

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(); }

}