统计同时购买的商品和购买时间
2015-11-02 16:57
225 查看
package test; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import redis.clients.jedis.Jedis; public class BuyedSameTime { public static void appendMethod(String fileName, String content) { try { //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static String[] getStrings() { FileInputStream fis1; InputStreamReader isr1; BufferedReader br1 = null; try { fis1 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/buyers.txt"); isr1 = new InputStreamReader(fis1, "UTF-8"); br1 = new BufferedReader(isr1); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } String[] strings = new String[5462]; int i=0; String str; try { while ((str = br1.readLine()) != null) { strings[i] = str; i++; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return strings; } public static String[] getStrings1() { FileInputStream fis3; InputStreamReader isr3; BufferedReader br3 = null; try { fis3 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/buyedTime.txt"); isr3 = new InputStreamReader(fis3, "UTF-8"); br3 = new BufferedReader(isr3); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } String[] strings = new String[5462]; int i=0; String str; try { while ((str = br3.readLine()) != null) { strings[i] = str; i++; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return strings; } public static String[] getStrings2() { FileInputStream fis2; InputStreamReader isr2; BufferedReader br2 = null; try { fis2 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/user_bought_history.txt"); isr2 = new InputStreamReader(fis2, "UTF-8"); br2 = new BufferedReader(isr2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } String[] strings = new String[13611038]; int i=0; String str; try { while ((str = br2.readLine()) != null) { strings[i] = str; i++; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return strings; } /** * * 二分查找算法 * * * * @param srcArray * 有序数组 * * @param des * 查找元素 * * @return des的数组下标,没找到返回-1 */ public static int binarySearch1(int[] srcArray, int des){ int low = 0; int high = srcArray.length-1; while(low <= high) { int middle = (low + high)/2; if(des == srcArray[middle]) { return middle; }else if(des <srcArray[middle]) { high = middle - 1; }else { low = middle + 1; } } return -1; } public static void main(String[] args) { Jedis jedis; String host = "10.20.100.5"; int port = 6379; jedis = new Jedis(host, port); String[] strings = getStrings();//strings[i] buyers String[] strings1=getStrings1();//strings1[i]buyedTime String fileName = "/public/home/dsj/Public/sundujing/fpgrowth/buyedSameTime.txt"; String content; for(int i=0;i<5462;i++) { if((strings[i].length()==0||strings[i].equals(""))&&(strings[i]!=null||strings[i].equals(null))){appendMethod(fileName,"\n");continue;} else { String[] str2 = strings[i].split(","); for(int j=0;j<str2.length;j++)//each buyer { content=jedis.get(str2[j]); appendMethod(fileName,content); appendMethod(fileName,","); // String[] str3=content.split(","); // for(int t=0;t<str3.length;t++) // { // String[] str4=str3[t].split(" "); // String time=str4[1]; // if(strings1[i]=="1") // { // if(time.charAt(4)=='0'&&(time.charAt(5)=='2'||time.charAt(5)=='3'||time.charAt(5)=='3')) // { // appendMethod(fileName,"str4[0]"); // appendMethod(fileName,","); // } // }else if(strings1[i]=="2") // { // if(time.charAt(4)=='0'&&(time.charAt(5)=='5'||time.charAt(5)=='6'||time.charAt(5)=='7')) // { // appendMethod(fileName,"str4[0]"); // appendMethod(fileName,","); // } // } // else if(strings1[i]=="3") // { // if((time.charAt(4)=='0'&&(time.charAt(5)=='8'||time.charAt(5)=='9'))||(time.charAt(4)=='1'&&time.charAt(5)=='0')) // { // appendMethod(fileName,"str4[0]"); // appendMethod(fileName,","); // } // }else if(strings1[i]=="4") // { // if((time.charAt(4)=='1'&&(time.charAt(5)=='1'||time.charAt(5)=='2'))||(time.charAt(4)=='0'&&time.charAt(5)=='1')) // { // appendMethod(fileName,"str4[0]"); // appendMethod(fileName,","); // } // } // } } } appendMethod(fileName,"\n"); } // } }
每行代表每个预测商品所有购买者在一个季节购买的其他商品及购买时间,用逗号隔开
相关文章推荐
- java JTable 动态拉取数据显示及自动刷新
- 递归
- java 基础知识
- Android 对Canvas的translate方法总结
- 第十周项目3-用二叉树遍历思想解决问题
- 第十周--项目一--二叉树算法
- 第九周 项目4-广义表算法库及应用
- mysql 操作常用命令
- 使用libcurl实现的下载器
- jQuery轮播图插件
- JS window.open()
- 杭电1335-任意进制的转换
- 希腊选手Pavlos Xanthopoulos在2015 WSOPE赛事6夺冠
- Oracle数据库零散知识07 -- Materialized view(转)
- 设置pdb随cdb启动
- 第10周上机实践项目2—— 二叉树遍历的递归算法
- 常用js之 验证非中文 全中文
- 第十周项目2—二叉树的遍历递归算法
- 第十周 项目二-二叉树遍历的递归算法
- SQL必知必会