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());
}
}
}