package test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class ReplaceItem1 {
public static void getStrings() {
FileInputStream fis;
InputStreamReader isr;
BufferedReader br = null;
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("D://dim_items.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;
// ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
while ((str = br.readLine()) != null) {
strings[0] = str;
String[] str1 = str.split(" ");
//分割“ ”
Map<String, Object> map = new HashMap<String, Object>();
//for(int k=0;k<3;k++)
if(str1.length>2)
{
map.put("item_id",str1[0]);
map.put("cat_id", str1[1]);
map.put("terms", str1[2]);
jedis.set(str1[0].getBytes(), writeObject(map));
}
// list.add(map);
}
} catch (IOException e) {
e.printStackTrace();
}
// return list;
}
/**
* * 二分查找算法 * *
*
* @param srcArray
* 有序数组 *
* @param des
* 查找元素 *
* @return des的数组下标,没找到返回-1
*/
public static int binarySearch(ArrayList<Map<String, Object>> list, String des){
int low = 0;
int high = list.size()-1;
while(low <= high) {
int middle = (low + high)/2;
if(des.equals(list.get(middle).get("item_id"))) {
return middle;
}else if(Double.parseDouble(des) <Double.parseDouble((String) list.get(middle).get("item_id")) ) {
high = middle - 1;
}else {
low = middle + 1;
}
}
return -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;
}
private static byte[] writeObject(Object obj)//写对象
{
byte[] array = null;
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject(obj);
array =baos.toByteArray();
System.out.println("序列化成功。");
System.out.println(array);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return array;
}
private static Object readObject( byte[] array)//读对象
{
try
{
ByteArrayInputStream bais = new ByteArrayInputStream(array);
ObjectInputStream is = new ObjectInputStream(bais);
Object temp = (Object) is.readObject();
if (temp != null)
{
System.out.println("反序列化成功。");
// System.out.println("age"+temp.age);
System.out.println(temp);
return temp;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
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 void main(String args[])
{
Jedis jedis;
String host = "10.20.100.5";
int port = 6379;
jedis = new Jedis(host, port);
getStrings();
byte[] mapbyte=jedis.get("29".getBytes());
Object mapobj= readObject(mapbyte);
System.out.println("id"+((Map<String, Object>) mapobj).get("cat_id"));
//
// ArrayList<Map<String, Object>> list=getStrings();
//
// Map<String, Object> map = new HashMap<String, Object>();
// String jsonString = JsonTools.createJsonString("objects", list);
// System.out.println(jsonString);
// jedis.set("list", jsonString);
map=list.get(binarySearch(list, "41"));
System.out.println(map.get("terms"));
System.out.println(map.get("cat_id"));
String fileName = "D://ToCat.txt";
// String fileName = "/public/home/dsj/Public/sundujing/fpgrowth/ToCat.txt";
String fileName = "/public/home/dsj/Public/sundujing/fpgrowth/ToTerms1.txt";
String content;
// // content=jsonString;
// // appendMethod(fileName, content);
//
//
// jedis.set("list".getBytes(), writeObject(list));
// byte[] listbyte=jedis.get("list".getBytes());
// Object listobject= readObject(listbyte);
// ArrayList<Map<String, Object>> list=(ArrayList<Map<String, Object>>) listobject;
// int index=Collections.binarySearch(list, list.get("1240033"));
// content=(String)( list.get(binarySearch(list, "1240033")).get("terms"));
// appendMethod(fileName, content);
// //替换item为第二个属性
FileInputStream fis;
InputStreamReader isr;
BufferedReader br = null;
try {
fis = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/dim_fashion_matchsets.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];
String str;
try {
while ((str = br.readLine()) != null) {
strings[0] = str;
String[] str1 = str.split(" ");
// System.out.println("str1[1]"+str1[1]);
String[] str2 = str1[1].split(";");
// System.out.println(str2.length);
// System.out.println(str2[0]);
for(int k=0;k<str2.length;k++)
{
// System.out.println(str2[k].length());
if(str2[k].length()>7)
{
String[] str3 = str2[k].split(",");
for(int j=0;j<str3.length;j++)
{
System.out.println(str3[j]);
//need replace
// map=list.get(binarySearch(list, str3[j]));
mapbyte=jedis.get(str3[j].getBytes());
mapobj= readObject(mapbyte);
System.out.println("id"+((Map<String, Object>) mapobj).get("terms"));
//write file append
content=(String)((Map<String, Object>) mapobj).get("terms");
appendMethod(fileName, content.replaceAll(",", " "));
appendMethod(fileName, ",");
}
}else
{
System.out.println(str2[k]);
//need replace
// map=list.get(binarySearch(list, str2[k]));
mapbyte=jedis.get(str2[k].getBytes());
mapobj= readObject(mapbyte);
System.out.println("id"+((Map<String, Object>) mapobj).get("terms"));
//write file append
content=(String)((Map<String, Object>) mapobj).get("terms");
appendMethod(fileName, content.replaceAll(",", " "));
appendMethod(fileName, ",");
}
}
appendMethod(fileName, "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
//替换item为第三个属性
//分别关联分析,得出抽象类间的关联
//根据test中的id-〉item的id-〉到第二个属性和第三个属性的关联结果-〉结果(200)
//具体类目关联-社会网络
}
}
将原先的matchsets中的产品替换为它的分词结果
最新推荐文章于 2023-04-29 16:02:26 发布