2019独角兽企业重金招聘Python工程师标准>>>
//XuXuZhuFuShopBuyOutLets.java
import java.util.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
public class XuXuZhuFuShopBuyOutLets{
private static ArrayList createDateList(String Date1, String Date2) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
Date date1 = sdf.parse(Date1);
Date date2 = sdf.parse(Date2);
ArrayList datelist = new ArrayList();
cal.setTime(date1);
while (cal.getTime().getTime()<=date2.getTime()){
datelist.add(sdf.format(cal.getTime()));
cal.add(Calendar.DATE,1);
//System.out.println(sdf.format(cal.getTime()));
} //for(Object xx: datelist){
//for(Object xx: datelist){
// System.out.println((String)xx);
//}
return datelist;
}
private static JSONObject getFartherSerMapDict(File FileMap) throws Exception{
JSONObject sermapdict = new JSONObject();
String temp = null;
BufferedReader br = new BufferedReader(new FileReader(FileMap));
while((temp=br.readLine())!=null){
sermapdict = JSONObject.fromObject(temp);
}
//System.out.println(sermapdict.toString());
return sermapdict;
}
@SuppressWarnings("unchecked")
private static HashMap getSerDauDict(File FileAct, JSONObject SerMapDict) throws Exception{
HashMap serdaudict = new HashMap();
String temp = null;
BufferedReader br = new BufferedReader(new FileReader(FileAct));
while((temp=br.readLine())!=null){
String[] line = temp.split("\t");
String uid = line[1];
String server = (String)SerMapDict.get(line[2]);
if (serdaudict.containsKey(server)){
serdaudict.get(server).add(uid);
}else{
HashSet hs = new HashSet();
hs.add(uid);
serdaudict.put(server, hs);
}
}
//for (HashSet xx: serdaudict.values()){
// System.out.println(xx.size());
//}
return serdaudict;
}
@SuppressWarnings("unchecked")
private static HashMap getShopOutletsDict(File FileSpend, JSONObject SerMapDict) throws Exception{
HashMap shopoutletsdict = new HashMap();
String temp = null;
BufferedReader br = new BufferedReader(new FileReader(FileSpend));
while((temp=br.readLine())!=null){
String[] line = temp.split("\t");
String uid = line[1];
String server = (String)SerMapDict.get(uid.substring(0,uid.length()-7));
String goodstype = line[7];
//HashMap args = new HashMap(line[13]);
JSONObject args = JSONObject.fromObject(line[13]); //注意此处将字典形式的字符串,转变为JSONObject。
if (goodstype.equals("shop.outlets_buy")){
String shopid = ((JSONArray)args.get("shop_id")).getString(0);
if (shopoutletsdict.get(server)!=null){
//(shopoutletsdict.get(server).get(shopid)==null)?shopoutletsdict.get(server).put(shopid, 1):shopoutletsdict.get(server).get(shopid)=shopoutletsdict.get(server).get(shopid)+1;
//if(shopoutletsdict.get(server).containsKey(shopid)){
// shopoutletsdict.get(server).put(shopid, (int)shopoutletsdict.get(server).get(shopid)+1);
//}else{
// shopoutletsdict.get(server).put(shopid,1);
//}
shopoutletsdict.get(server).put(shopid, (shopoutletsdict.get(server).get(shopid)==null)?1:(int)shopoutletsdict.get(server).get(shopid)+1); //使用嵌套三目表达式其实现功能与上面注释的部分相同
}else{
shopoutletsdict.put(server, new HashMap());
shopoutletsdict.get(server).put(shopid,1);
}
}
}
return shopoutletsdict;
}
public static void main(String[] args) throws Exception{
String date1 = "20160315";
String date2 = "20160321";
ArrayList datelist = createDateList(date1, date2);
String path = "/home/data/superhero_vietnam/";
String acthead = path + "active_user/dw_superhero_activeuser_";
String spendhead = path + "spendlog/spendlog_";
String sermaphead = path + "viso_config/father_server_map_";
for (Object day : datelist){
File fileact = new File(acthead + (String)day);
File filespend = new File(spendhead + (String)day);
File filesermap = new File(sermaphead + (String)day);
JSONObject sermapdict = getFartherSerMapDict(filesermap);
HashMap serdaudict = getSerDauDict(fileact, sermapdict);
HashMap shopoutletsdict = getShopOutletsDict(filespend, sermapdict);
Iterator it = serdaudict.keySet().iterator();
while(it.hasNext()){
String ser = (String) it.next();
int dau = serdaudict.get(ser).size();
System.out.print(day.toString()+"\t"+ser+"\t"+dau);
for (String str:new String[]{"1","2","3","4","5","6"}){
//int person = 0;
//if (shopoutletsdict.containsKey(ser)){
// if (shopoutletsdict.get(ser).get(str)==null){
// person = 0;
// }else{
// person = (int)shopoutletsdict.get(ser).get(str);
// }
//}
int person = (shopoutletsdict.get(ser)==null)?0:((shopoutletsdict.get(ser).get(str)==null)?0:(int)shopoutletsdict.get(ser).get(str)); //使用嵌套三目表达式,其实现功能与上面注释的相同。
System.out.print("\t"+person);
}
System.out.println();
}
}
}
}