TestApiController.java
4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.yoho.search.consumer.test;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.FileUtils;
import com.yoho.search.consumer.index.increment.bulks.CommonBulkService;
import com.yoho.search.consumer.job.ProductHeatValueJob;
import com.yoho.search.consumer.job.ProductModelValueJob;
import com.yoho.search.core.es.model.ESBluk;
import com.yoho.search.dal.SknImageVectorsMapper;
import com.yoho.search.dal.model.SknImageVectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class TestApiController {
private static final Logger logger = LoggerFactory.getLogger(TestApiController.class);
@Autowired
private ProductHeatValueJob productHeatValueJob;
@Autowired
private ProductModelValueJob productModelValueJob;
@Autowired
private CommonBulkService commonBulkService;
@Autowired
private SknImageVectorsMapper sknImageVectorsMapper;
@RequestMapping(value = "/test")
@ResponseBody
public Map<String, Object> test() {
Map<String, Object> testResult = new HashMap<String, Object>();
testResult.put("data", null);
testResult.put("code", 200);
testResult.put("message", "success");
return testResult;
}
@RequestMapping(value = "/doCleanHeatValue")
@ResponseBody
public Map<String, Object> doCleanHeatValue() {
Map<String, Object> returnMap = new HashMap<String, Object>();
try {
returnMap.put("code", 200);
returnMap.put("message", "success");
productHeatValueJob.clean();
} catch (Exception e) {
logger.error(e.getMessage(), e);
returnMap.put("code", 400);
returnMap.put("message", e.getMessage());
}
return returnMap;
}
@RequestMapping(value = "/doCleanProductModelValue")
@ResponseBody
public Map<String, Object> doCleanProductModelValue() {
Map<String, Object> returnMap = new HashMap<String, Object>();
try {
returnMap.put("code", 200);
returnMap.put("message", "success");
productModelValueJob.clean();
} catch (Exception e) {
logger.error(e.getMessage(), e);
returnMap.put("code", 400);
returnMap.put("message", e.getMessage());
}
return returnMap;
}
@RequestMapping(value = "/putImageVectorDataToDB")
@ResponseBody
public Map<String, Object> putData() throws UnsupportedEncodingException {
for (int j = 1; j <= 10; j++) {
String fileName = j + "_feat_out_32_.txt";
String filePath = this.getClass().getResource("/").getPath();
List<String> lines = FileUtils.readFile(filePath + fileName);
System.out.println("lines done " + j);
try {
List<SknImageVectors> sknImageVectorsList = new ArrayList<>();
for (int i = 0; i < lines.size(); i++) {
String skn = lines.get(i).substring(0, 8);
String factors = lines.get(i).substring(9);
//Map<String, Object> indexData = new HashMap<String, Object>();
//indexData.put("bin_sigs", factors);
SknImageVectors sknImageVectors = new SknImageVectors();
sknImageVectors.setProductSkn(Integer.valueOf(skn));
sknImageVectors.setImgUrl("url");
sknImageVectors.setVectors_32(factors);
sknImageVectors.setVectors_128("");
sknImageVectors.setCreateTime(DateUtil.getCurrentTimeSecond());
sknImageVectorsList.add(sknImageVectors);
//updateProductIndexWithDataMap(indexData, skn);
if (i > 0) {
if (i % 1000 == 0 || i == 9999) {
sknImageVectorsMapper.insertBatch(sknImageVectorsList);
sknImageVectorsList.clear();
System.out.println("batch done_" + j + "_" + i);
}
}
}
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
Map<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("message", "success");
return result;
}
protected final void updateProductIndexWithDataMap(Map<String, Object> indexData, String skn) {
JSONObject jsonObject = new JSONObject(indexData);
commonBulkService.add(new ESBluk(jsonObject.toJSONString(), skn, "images_test", "images_test", false));
}
}