HBase程序查询
来源:互联网 发布:mac应用程序图标大小 编辑:程序博客网 时间:2024/06/09 15:04
代码:
import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import java.util.Random;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.MasterNotRunningException;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.ZooKeeperConnectionException;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.junit.After;import org.junit.Before;import org.junit.Test;public class HbaseDemo { HBaseAdmin hBaseAdmin; String TN="phone"; HTable hTable; @Before public void begin() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ Configuration conf=new Configuration(); conf.set("hbase.zookeeper.quorum","node1,node2,node3"); hBaseAdmin=new HBaseAdmin(conf); hTable=new HTable(conf, TN); } @After public void end(){ if(hBaseAdmin!=null){ try { hBaseAdmin.close(); } catch (IOException e) { e.printStackTrace(); } } if(hTable!=null){ try { hTable.close(); } catch (IOException e) { e.printStackTrace(); } } } Random r=new Random(); //随机生成手机号码 public String getPhoneNum(String prefix){ return prefix+String.format("%08d", r.nextInt(99999999)); } //随机生成时间 public String getDate(String year){ return year+String.format("%02d%02d%02d%02d%02d", new Object[]{r.nextInt(12)+1,r.nextInt(29)+1, r.nextInt(60),r.nextInt(60),r.nextInt(60)}); } //插入10个手机号 100条通话记录,满足查询 时间降序排序 @Test public void insertDB() throws IOException{ List<Put> puts=new ArrayList<Put>(); for(int i=0;i<10;i++){ String rowkey; String phoneNum=getPhoneNum("186"); for(int j=0;j<100;j++){ String phoneDate=getDate("2016"); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss"); try { long dataLong=sdf.parse(phoneDate).getTime(); rowkey=phoneNum+(Long.MAX_VALUE-dataLong); System.out.println(rowkey); Put put=new Put(rowkey.getBytes()); put.add("cf1".getBytes(),"type".getBytes(),(r.nextInt(2)+"").getBytes()); put.add("cf1".getBytes(),"time".getBytes(),(phoneDate).getBytes()); put.add("cf1".getBytes(),"pnum".getBytes(),(getPhoneNum("170")).getBytes()); puts.add(put); } catch (ParseException e) { e.printStackTrace(); } } } hTable.put(puts); } //查询某手机某个月份的所有通话详单 @Test public void scanDB() throws ParseException, IOException{ //186 9651 3780 二月份的通话详单 //18619376327 Scan scan=new Scan(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss"); String startRowkey="18619376327"+(Long.MAX_VALUE-sdf.parse("20160401000000").getTime()); scan.setStartRow(startRowkey.getBytes()); String stopRowkey="18619376327"+(Long.MAX_VALUE-sdf.parse("20160201000000").getTime()); scan.setStopRow(stopRowkey.getBytes()); ResultScanner rss=hTable.getScanner(scan); System.out.println("hehe"); for(Result rs:rss){ System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf1".getBytes(), "type".getBytes())))+"-"+new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf1".getBytes(), "time".getBytes())))+"-"+new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf1".getBytes(), "pnum".getBytes())))); } }}
0 0
- HBase程序查询
- Hbase查询
- HBase的条件查询
- HBase的条件查询
- HBase的条件查询
- hbase的内容查询
- hbase条件查询
- hbase的内容查询
- hbase条件查询
- hbase的内容查询
- hbase分页查询
- HBase 简单SQL查询
- hive查询hbase
- Hbase实现Like查询
- Hbase 创建,插入,查询
- Hbase 查询为什么快
- hbase查询的过程
- hbase的内容查询
- 职场达人教你如何做高效工作汇报!
- C++如何使用ACL 连接Redis
- TortoiseSVN与VisualSVN Server搭建SVN版本控制系统
- java 自定义类排序
- 处理器相关的概念
- HBase程序查询
- vue组件之间的传值
- QT 上建立一个小窗口
- 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
- Android Draw 实现时钟
- Codeforces 798D 构造
- 第23节:Scala外部命令和正则表达式
- YAML部署实践
- TCP/IP详解--举例明白发送/接收缓冲区、滑动窗口协议之间的关系