问题:用java读取文件到内存实现简单的聚合计算,如下图,将如下文件先整理成{1={21,33},2={21,32}},这种形式然后就可以遍历map聚合了
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<String,List<String>> map_all=new HashMap<String,List<String>>();
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();
List<String> times = new ArrayList<String>();
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 Map<String,String> countTime(Map<String,List<String>> map_all) throws ParseException {
Map<String,String> map_result=new HashMap<String, String>();
for (Map.Entry<String,List<String>> entry : map_all.entrySet()) {
entry.getKey();
List<String> list1=entry.getValue();
long dif_all=0;
for(int i=0;i<list1.size()-1;i++){
dif_all=dif_all+list1.get(i);
}
map_result.put(entry.getKey(),dif_all);
}
return map_result;
}
个人观点,欢迎指正。