java 連線hbase
阿新 • • 發佈:2019-02-15
電信詳單,查詢某個號碼,某個月的通話清單,包括通話號碼,通話型別,通話時間等資訊,如何設計? 表名:t_cdr rowkey設定:號碼+時間 一個列族:cf1 欄位:dest(對方號碼),type(通話型別),time(通話時間) package com.lhj.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; 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.rest.protobuf.generated.ScannerMessage.Scanner; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test; public class HbaseTest { @Test public void test1() throws Exception { Configuration conf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "node1,node2,node3"); HBaseAdmin admin=new HBaseAdmin(conf); String table="t_cdr"; if(admin.isTableAvailable(table)){ admin.disableTable(table); admin.deleteTable(table); } HTableDescriptor t=new HTableDescriptor(table.getBytes()); HColumnDescriptor cf1=new HColumnDescriptor("cf1".getBytes()); //cf1.setMaxVersions(8); //cf1.setMinVersions(0); t.addFamily(cf1); admin.createTable(t); admin.close(); } //hbase(main):007:0> list //TABLE //t_cdr @Test public void test2() throws Exception{ Configuration conf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "node1,node2,node3"); HTable table=new HTable(conf, "t_cdr"); String rowkey="18933945820_"+System.currentTimeMillis(); Put put=new Put(rowkey.getBytes()); put.add("cf1".getBytes(), "dest".getBytes(), "123456789".getBytes()); put.add("cf1".getBytes(), "type".getBytes(), "1".getBytes()); put.add("cf1".getBytes(), "time".getBytes(), "2015-11-20 13:27:30".getBytes()); table.put(put); table.close(); } //hbase(main):013:0> scan 't_cdr' //ROW COLUMN+CELL // 18933945820_1447997665063 column=cf1:dest, timestamp=1447997661766, value=123456789 // 18933945820_1447997665063 column=cf1:time, timestamp=1447997661766, value=2015-11-20 13:27:30 // 18933945820_1447997665063 column=cf1:type, timestamp=1447997661766, value=1 // @Test public void test3() throws Exception{ Configuration conf=HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "node1,node2,node3"); HTable table=new HTable(conf, "t_cdr"); //Get get=new Get("18933945820_1447997665063".getBytes()); //Result res=table.get(get); //Cell c1=res.getColumnLatestCell("cf1".getBytes(), "dest".getBytes()); //System.out.println(new String(c1.getValue())); //Cell c2=res.getColumnLatestCell("cf1".getBytes(), "time".getBytes()); //System.out.println(new String(c2.getValue())); //Cell c3=res.getColumnLatestCell("cf1".getBytes(), "type".getBytes()); //System.out.println(new String(c3.getValue())); Scan scan=new Scan(); scan.setStartRow("18933945820_1447997665000".getBytes()); scan.setStopRow("18933945820_1447997665100".getBytes()); scan.setMaxVersions(); //指定最多返回的Cell數目。用於防止一行中有過多的資料,導致OutofMemory錯誤。 ResultScanner rs=table.getScanner(scan); //row:18933945820_1447997665063, family:cf1, qualifier:dest, qualifiervalue:123456789, timestamp:1447997661766. //row:18933945820_1447997665063, family:cf1, qualifier:time, qualifiervalue:2015-11-20 13:27:30, timestamp:1447997661766. //row:18933945820_1447997665063, family:cf1, qualifier:type, qualifiervalue:1, timestamp:1447997661766. for (Result r : rs) { for (KeyValue kv : r.raw()) { System.out.println(String.format("row:%s, family:%s, qualifier:%s, qualifiervalue:%s, timestamp:%s.", Bytes.toString(kv.getRow()), Bytes.toString(kv.getFamily()), Bytes.toString(kv.getQualifier()), Bytes.toString(kv.getValue()), kv.getTimestamp())); } rs.close(); } table.close(); } }