日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

hbase 按时刻查询_Hbase查询工具类,根据时间查询数据

發(fā)布時(shí)間:2024/9/27 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hbase 按时刻查询_Hbase查询工具类,根据时间查询数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1,需求:已知空氣監(jiān)測(cè)數(shù)據(jù)在hbase中存儲(chǔ),要求按照時(shí)間,查詢citycode為110000(北京)一個(gè)月的數(shù)據(jù),數(shù)據(jù)為每日的監(jiān)測(cè)數(shù)據(jù)

ID ,CITYCODE,SO2 ,CO,NO2 ,O3, PM10,PM2_5,AQI,MEASURE, TIMEPOINT

13110000020141120,? 110000,31,3.939,141,8,368,301,351,6,2014-11-20

5110000020150108,?? 110000,53,3.305,101,12,176,143,190,4,2015-01-08

key值設(shè)計(jì)規(guī)則:

String yearMoth= "201411"; //年月

String time="20141120"; //年月日

String citycode="1100000"; //城市編碼

//hbase 為20個(gè)分區(qū),數(shù)據(jù)進(jìn)入hbase的分區(qū)規(guī)則如下

int region=Math.abs((yearMoth+citycode).hashCode())%20; //結(jié)果:8

//hbase的key值為

String key =region+citycode+time;

這樣設(shè)計(jì)key的好處是:同一個(gè)城市,每個(gè)月的數(shù)據(jù)的分區(qū)相同,即region相同,只需要設(shè)置startRowkey與endRowKey即可

例如 查詢北京 2014 年 11 月的數(shù)據(jù)

startRowkey:13110000020141100

endRowkey:??? 13110000020141200? 即可

但:如果查詢? 2014 年11 月20 號(hào)? --- 2015年01月08號(hào)的數(shù)據(jù)? 則需要把每個(gè)月的數(shù)據(jù)查詢出來,合并到一起

以下 為工具類的的方法:

創(chuàng)建 時(shí)間范圍的javaBean對(duì)象

public class TimeRange {

private String startPoint ;

private String endPoint ;

public String getStartPoint() {

return startPoint;

}

public void setStartPoint(String startPoint) {

this.startPoint = startPoint;

}

public String getEndPoint() {

return endPoint;

}

public void setEndPoint(String endPoint) {

this.endPoint = endPoint;

}

public String toString() {

return startPoint + " - " + endPoint ;

}

}

計(jì)算時(shí)間范圍的工具類:

startStr =20141120

endStr=20150108

得到的結(jié)果 List 為? 20141120-20141201

20141201-20150101

20150101-20150108

/**

* 計(jì)算查詢時(shí)間范圍

*/

public static List getCallLogRanges(String startStr , String endStr){

try{

SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyyMMdd");

SimpleDateFormat sdfYM = new SimpleDateFormat("yyyyMM");

DecimalFormat df00 = new DecimalFormat("00");

//

List list = new ArrayList<>();

//字符串時(shí)間

String startPrefix = startStr.substring(0, 6);

String endPrefix = endStr.substring(0, 6);

int endDay = Integer.parseInt(endStr.substring(6, 8));

//結(jié)束點(diǎn)

String endPoint = endPrefix + df00.format(endDay + 1);

//日歷對(duì)象

Calendar c = Calendar.getInstance();

//同年月

if (startPrefix.equals(endPrefix)) {

TimeRange range = new TimeRange ();

range.setStartPoint(startStr); //設(shè)置起始點(diǎn)

range.setEndPoint(endPoint); //設(shè)置結(jié)束點(diǎn)

list.add(range);

} else {

//1.起始月

TimeRange range = new TimeRange ();

range.setStartPoint(startStr);

//設(shè)置日歷的時(shí)間對(duì)象

c.setTime(sdfYMD.parse(startStr));

c.add(Calendar.MONTH, 1);

range.setEndPoint(sdfYM.format(c.getTime()));

list.add(range);

//是否是最后一月

while (true) {

//到了結(jié)束月份

if (endStr.startsWith(sdfYM.format(c.getTime()))) {

range = new TimeRange ();

range.setStartPoint(sdfYM.format(c.getTime()));

range.setEndPoint(endPoint);

list.add(range);

break;

} else {

range = new TimeRange ();

//起始時(shí)間

range.setStartPoint(sdfYM.format(c.getTime()));

//增加月份

c.add(Calendar.MONTH, 1);

range.setEndPoint(sdfYM.format(c.getTime()));

list.add(range);

}

}

}

return list ;

}

catch(Exception e){

e.printStackTrace();

}

return null ;

}

Hbase 查詢的時(shí) 遍歷List

public List getLogByContion(String citycode, List ranges) {

Configuration conf = HBaseConfiguration.create();

Connection conn = ConnectionFactory.createConnection(conf);

TableName tableName = TableName.valueOf("hbase:airDay");

table=conn.getTable(tableName);

List list = new ArrayList<>();

try {

for (TimeRange range: ranges) {

Scan scan = new Scan();

//設(shè)置掃描起始行 結(jié)束行

scan.setStartRow(Bytes.toBytes(HbaseUtils.getStartRowkey(citycode,range.getStartPoint())));

scan.setStopRow(Bytes.toBytes(HbaseUtils.getStopRowkey(citycode,range.getStartPoint(),range.getEndPoint())));

ResultScanner rs = table.getScanner(scan);

Iterator it = rs.iterator();

byte[] f1 = Bytes.toBytes("f1");

byte[] caller = Bytes.toBytes("citycode");

byte[] callee = Bytes.toBytes("so2");

byte[] callTime = Bytes.toBytes("co");

byte[] callDuration = Bytes.toBytes("no2");

while (it.hasNext()) {

Result r = it.next();

Map map = new HashMap();

map.put("rowkey",Bytes.toString(r.getRow()));

map.put("caller",Bytes.toString(r.getValue(f1,citycode)));

map.put("callee",Bytes.toString(r.getValue(f1,so2)));

map.put("callTime",Bytes.toString(r.getValue(f1,co)));

map.put("callDuration",Bytes.toString(r.getValue(f1,no2)));

list.add(map);

}

}

}catch (Exception e){

}

return list;

}

public static String getStartRowkey(String citycode, String time) {

int region=Math.abs((yearMoth+citycode).hashCode())%20;

return region+citycode+time;

}

public static String getStopRowkey(String citycode,String starttime, String endtime) {

int region=Math.abs((yearMoth+citycode).hashCode())%20;

return region+citycode+endtime;

}

總結(jié)

以上是生活随笔為你收集整理的hbase 按时刻查询_Hbase查询工具类,根据时间查询数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。