统计同时购买的商品和购买时间

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");
        }

    //  
    }

}

每行代表每个预测商品所有购买者在一个季节购买的其他商品及购买时间,用逗号隔开
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值