对商品类别进行聚类
2015-11-02 16:49
316 查看
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<String,HashSet> map = new Map<String,HashSet>(); // 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
相关文章推荐
- 使用JDBC处理mysql
- css sprites 多张图片整合在一张图片上
- 服务器监控客户端系统状态3.0
- 【POI2012】【BZOJ2795】A Horrible Poem
- Ajax快速学习笔记
- 第十周--项目2二叉树遍历递归算法
- 第9周项目3 稀疏矩阵的三元组表示的实现与应用(2)
- 第九周 项目3-稀疏矩阵的三元表示的实现及应用(2)
- 设置unity 编译文件到android项目
- AFNetworking 需要导入的框架
- 网上看到了一些ios面试题,看着解答解答
- linux基础---管道和I/O重定向
- hdu 1078 FatMouse and Cheese (记忆化搜索)
- 第10周 项目2-二叉树遍历的递归算法
- 快速安装zabbix服务端
- 第10周—项目1 二叉树算法库
- 点击空白处隐藏键盘的几种方法
- 程序员的数学世界
- C语言enum枚举类型解析
- css 水平垂直居中