es/_mapping查出的数据批量导入

es/_mapping查出的数据批量导入



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] s) throws Exception {
        ClassPathResource resource = new ClassPathResource("json/10.json");
        File filePath = null;
        filePath = resource.getFile();
        String input = FileUtils.readFileToString(filePath, "UTF-8");
        Map<String, JSONObject> maps = (Map<String, JSONObject>) JSON.parse(input);
        List<String> errorList = Lists.newArrayListWithExpectedSize(120);
        for (String key : maps.keySet()) {
            JSONObject mapping = maps.get(key);
            testHttp(key, mapping.toString(), errorList);

        }
        for (String errorIndex : errorList) {
            System.out.println(errorIndex);
        }
    }

    public static void testHttp(String indexName, String mapping, List<String> errorList) {

        String result = "";
        try {
            URL postURL = new URL("http://ip:9200/" + indexName);
            HttpURLConnection conn = (HttpURLConnection) postURL.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
//            conn.setConnectTimeout(5 * 1000);
            // PUT请求
            conn.setRequestMethod("PUT");
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);
            // json格式上传的模式
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
//            OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
            //解决中文乱码
            PrintWriter osw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
            osw.write(mapping);
            osw.flush();
            osw.close();
            if (conn.getResponseCode() == 200) {
                System.out.println(indexName+"=============>success");
            } else {
                errorList.add(indexName);
                System.out.println(indexName+"=====================================>faile");
                System.out.println(conn.getErrorStream());
            }
            if (conn.getResponseCode() == 200) {
                InputStreamReader isr = new InputStreamReader(conn.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String inputLine = null;
                while ((inputLine = br.readLine()) != null) {
                    result += inputLine;
                }
                isr.close();
                conn.disconnect();
            } else {
                //如果出错,一定要检查URL对没有!
                BufferedReader br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
                String jsontxt = br.readLine();
                br.close();
            }

            conn.disconnect();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值