对商品类别进行聚类

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.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;

import redis.clients.jedis.Jedis;

//with one cat
public class CatPolymerization {
     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 HashSet getStrings() 
    {
        FileInputStream fis;
        InputStreamReader isr;
        BufferedReader br = null;
        HashSet h =new HashSet();
        Jedis jedis;
        String host = "10.20.100.5";
        int port = 6379;
        jedis = new Jedis(host, port);
        try
        {
    //  fis = new FileInputStream("D://te.txt");
        fis = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/dim_items.txt");
        isr = new InputStreamReader(fis, "UTF-8");
        br = new BufferedReader(isr);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    String[] strings = new String[1];
    int i=0;
    String str;
    try {
        while ((str = br.readLine()) != null) {
            strings[0] = str;
            String[] str1 = str.split(" ");
            //分割“ ”
//          Map map = new Map();


//               map.put("item_id",str1[0]);
//              map.put("cat_id", str1[1]);
            //  map.put("terms", str1[2]);
                String s=str1[0];
                if(jedis.get(str1[1])!=null)
                {
                    System.out.println("1 "+jedis.get(str1[1]));
                    s=jedis.get(str1[1])+","+s;
                 jedis.set(str1[1],s); 
                 System.out.println("2 "+jedis.get(str1[1]));
                }
                else{
                    jedis.set(str1[1],s);
                    System.out.println("3"+jedis.get(str1[1]));
                }
                h.add(str1[1]);
            System.out.println("4 "+jedis.get(str1[1]));
            }
    //      list.add(map);


    } catch (IOException e) {
        e.printStackTrace();
    }
        return h;

    }
    public static void main(String[] args)
    {
        Jedis jedis;
        String host = "10.20.100.5";
        int port = 6379;
        jedis = new Jedis(host, port);
    HashSet h=getStrings();
    //String fileName = "D:\\Catpoly.txt";
    String fileName = "/public/home/dsj/Public/sundujing/fpgrowth/Catpoly.txt";
    String content;
     Iterator it=h.iterator();
     while(it.hasNext())
     {
         Object o=it.next();
         System.out.println(o);
         String s=o.toString();
     content=jedis.get(o.toString());
        appendMethod(fileName,s);
        appendMethod(fileName," ");
    appendMethod(fileName, content);
    appendMethod(fileName, "\n");
     }
    }

}

输出文件
6 是商品类别,后面是商品类别为6的所有商品id
对商品类别进行聚类_第1张图片

你可能感兴趣的:(天池黄金联赛)