//HanPengUserWeekPayCoinAndWeekSpendCoin.java
import java.util.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.text.SimpleDateFormat;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class HanPengUserWeekPayCoinAndWeekSpendCoin{
//@SuppressWarnings("unchecked")
private static ArrayList createDateList (String Monday, int days) throws Exception{
Calendar cal=Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
ArrayList list = new ArrayList();
Date monday = sdf.parse(Monday);
cal.setTime(monday);
for(int i=0;i
String temp = sdf.format(cal.getTime());
list.add(temp);
//System.out.print(temp+",");
cal.add(Calendar.DATE,1);
}
return list;
}
@SuppressWarnings("unchecked")
private static void printcollection (Collection col) throws Exception{
Iterator it= col.iterator();
while(it.hasNext()){
String temp = (String)it.next();
System.out.print(temp+",");
}
System.out.println();
System.out.println(Collections.max(col)+"\t"+Collections.min(col));
}
private static HashMap createPayUserSetAndCoin(File FilePay, List DateList) throws Exception{
HashMap paysetandcoindict = new HashMap();
HashSet payuidset = new HashSet();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
BufferedReader br = new BufferedReader (new FileReader(FilePay));
String temp = null;
int pay = 0;
while((temp=br.readLine())!=null){
String[] line = temp.split("\t");
int coin = Integer.parseInt(line[5]);
String orderday = line[7].substring(0,10).replace("-","");
//System.out.println(orderday);
String platform = line[8];
String uid = line[13];
String plat = uid.substring(0,1);
//System.out.println(plat);
if (plat.equals("g") && !platform.equals("admin_test") && DateList.contains(orderday)){
payuidset.add(uid);
pay = pay + coin;
}
paysetandcoindict.put("payuidset", (Object)payuidset);//java只能返回一个值,但可以使用容器对返回值进行封装。此处使用map进行封装, 一个为Set,另一个为int。
paysetandcoindict.put("paycoin",(Object)pay);//
}
return paysetandcoindict;
}
private static Integer getSpendCoin(String FileHead, List DateList, HashSet PayUserSet) throws Exception{
Iterator it = DateList.iterator();
int spendcoin = 0;
while(it.hasNext()){
String day = (String) it.next();
File filespend = new File(FileHead + day);
String temp = null;
BufferedReader br = new BufferedReader(new FileReader(filespend));
while((temp=br.readLine())!=null){
String [] line = temp.split("\t");
String uid = line[1];
int coin = Integer.parseInt(line[4]);
if (PayUserSet.contains(uid)){
spendcoin = spendcoin + coin;
}
}
}
return spendcoin;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception{
//String [] mondayarray = {"20151228","20160104","20160111","20160118","20160125","20160201","20160208","20160215","20160222","20160229"};
String [] mondayarray = {"20150302","20150309","20150316","20150323","20150330","20150406","20150413","20150420","20150427","20150504","20150511","20150518","20150525","20150601","20150608","20150615","20150622","20150629","20150706","20150713","20150720","20150727","20150803","20150810","20150817","20150824","20150831"};
for(int i=0; i
ArrayList datelist = createDateList(mondayarray[i], 14);
//System.out.println();
List firstweek = datelist.subList(0,7);
List secondweek = datelist.subList(7,14);
//printcollection(firstweek);
//printcollection(secondweek);
String pathpay = "/home/data/superhero/paylog/";
String payname = "paylog_20160316_all";
String pathspend = "/home/data/superhero/wang/hadoop/";
String spendname = "spendlog_";
File filepay = new File(pathpay + payname);
HashMap payObjDict = createPayUserSetAndCoin(filepay, firstweek);
HashSet payuserset = (HashSet)payObjDict.get("payuidset");
int paycoin = (int) payObjDict.get("paycoin");
//printcollection(payuserset);
int firstspendcoin = getSpendCoin(pathspend+spendname, firstweek, payuserset);
int secondspendcoin = getSpendCoin(pathspend+spendname, secondweek, payuserset);
System.out.println(mondayarray[i]+"\t"+payuserset.size()+"\t"+paycoin+"\t"+firstspendcoin+"\t"+secondspendcoin);
}
}
}