java 聚合计算 引擎_java 用map实现聚合计算

问题:用java读取文件到内存实现简单的聚合计算,如下图,将如下文件先整理成{1={21,33},2={21,32}},这种形式然后就可以遍历map聚合了

38ccd875ac3200168cf8d40d51431953.png

public static void main(String[] args) throws IOException {

String filename="/Users/data/test.json";

String id="_id";

String ts="_ts";

count(filename,id,ts,tablename);

}

//格式整理

public static void count(String filename,String id,String ts) throws IOException {

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(filename));

// 第一种读取文件方式

System.out.println("Reading the file using readLine() method: ");

String contentLine;

String str="";

String id="";

String time="";

Map> map_all=new HashMap>();

while ((contentLine = br.readLine()) != null) {

if(!contentLine.equals("")) {

Map maps = (Map) JSON.parse(contentLine);

device_id = maps.get(id).toString();

gps_time = maps.get(ts).toString();

Listtimes = new ArrayList();

if (!map_all.containsKey(device_id)) {

times.add(gps_time);

map_all.put(device_id, times);

} else {

map_all.get(device_id).add(gps_time);

}

}

}

System.out.println(map_all);

countTime(map_all);

}catch (Exception e){

e.printStackTrace();

}finally {

if(br!=null){

br.close();

}

}

}

//聚合

public static MapcountTime(Map> map_all) throws ParseException {

Mapmap_result=new HashMap();

for (Map.Entry> entry : map_all.entrySet()) {

entry.getKey();

Listlist1=entry.getValue();

long dif_all=0;

for(int i=0;i

dif_all=dif_all+list1.get(i);

}

map_result.put(entry.getKey(),dif_all);

}

return map_result;

}

个人观点,欢迎指正。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值