TestApiController.java 4.76 KB
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));
    }
}