gistfile1.txt package com.es.controller;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.PrintStream;import java.net.UnknownHostException;import java.util.Map;import org.springframework.boot.autoconfigure.Sprin
package com.es.controller;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.UnknownHostException;
import java.util.Map;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.es.backStage.ESClient;
import com.es.backStage.ToolClass;
import net.sf.json.JSONObject;
@SpringBootApplication // 不加这个会运行出错
@RestController
@RequestMapping("/es")
public class ESClientController {
ESClient esClient;
/**
* 构造器
*
* @throws UnknownHostException
*/
ESClientController() {
try {
this.esClient = new ESClient("127.0.0.1", 9300, "elasticsearch");
} catch (Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
String exception = baos.toString();
System.out.println("初始化错误");
System.out.println(exception);
}
}
@PostMapping("/create/indexWithSR")
@ResponseBody
public JSONObject createIndex(String index,
@RequestParam(value = "shardCount", required = false, defaultValue = "1") int shardCount,
@RequestParam(value = "replicaCount", required = false, defaultValue = "1") int replicaCount)
throws IOException {
try {
return ToolClass.resultToJson("data", esClient.createIndex(index, shardCount, replicaCount));
} catch (Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
String exception = baos.toString();
return ToolClass.resultToJson("exception", exception);
}
}
@PostMapping("/create/indexWithSRKV")
@ResponseBody
public JSONObject createIndex(String index,
@RequestParam(value = "shardCount", required = false, defaultValue = "1") int shardCount,
@RequestParam(value = "replicaCount", required = false, defaultValue = "1") int replicaCount,
Map
map) throws IOException {
try {
return ToolClass.resultToJson("data", esClient.createIndex(index, shardCount, replicaCount, map));
} catch (Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
String exception = baos.toString();
return ToolClass.resultToJson("exception", exception);
}
}
@PostMapping("/create/doc")
@ResponseBody
public JSONObject createRecord(String index,
@RequestParam(value = "type", required = false, defaultValue = "type") String type, String id,
Map
map) throws IOException { try { return ToolClass.resultToJson("data", esClient.creatDoc(index, type, id, map)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/allIndex") @ResponseBody public JSONObject getAllIndex() { try { return ToolClass.resultToJson("data", esClient.getAllIndex()); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/indexCount") @ResponseBody public JSONObject getIndexCount() { try { return ToolClass.resultToJson("data", esClient.getIndexCount()); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } /** * * * @param index * @param type * @return * @throws IOException */ @GetMapping("/get/mapping") @ResponseBody public JSONObject getMapping(String index, @RequestParam(value = "type", required = false, defaultValue = "type") String type) throws IOException { try { return ToolClass.resultToJson("data", esClient.getMapping(index, type)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/allType") @ResponseBody public JSONObject getMapping(String index) throws IOException { try { return ToolClass.resultToJson("data", esClient.getTypeByIndex(index)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/allField") @ResponseBody public JSONObject getAllFields(String index, @RequestParam(value = "type", required = false, defaultValue = "type") String type) throws IOException { try { return ToolClass.resultToJson("data", esClient.getAllFields(index, type)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/replicaCount") @ResponseBody public JSONObject getReplicaCount(String index) throws IOException { try { return ToolClass.resultToJson("data", esClient.getAllReplace(index)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/shardCount") @ResponseBody public JSONObject getShardCount(String index) throws IOException { try { return ToolClass.resultToJson("data", esClient.getAllShardCount(index)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/clusterHealthStatu") @ResponseBody public JSONObject getClusterHealthStatu() { try { return ToolClass.resultToJson("data", esClient.getClusterHealthStatuFromClust()); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/clusterName") @ResponseBody public JSONObject getClusterName() throws IOException { try { return ToolClass.resultToJson("data", esClient.getClusterNameFromClust()); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/doc") @ResponseBody public JSONObject getDoc(String index, @RequestParam(value = "type", required = false, defaultValue = "type") String type, String id) throws IOException { try { return ToolClass.resultToJson("data", esClient.getDoc(index, type, id)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/get/docCount") @ResponseBody public JSONObject getDocCount(String index) throws IOException { try { return ToolClass.resultToJson("data", esClient.getDocCount(index)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } /** * 注意,在这个方法中,如果一个对象中包括其他对象,而其他对象又包含null值,本地打印没有问题,但是输出值web的时候就汇报空指针错误,在此,将jsonobj对象转换为string格式再输出可以解决。 * * @param index * @param type * @param searchType * @param input * @param offset * @param size * @return */ @GetMapping("/get/docs") @ResponseBody public String getDocs(String index, @RequestParam(value = "type", required = false, defaultValue = "type") String type, int searchType, @RequestParam(value = "input", required = false, defaultValue = "") String input, int offset, int size) { try { return ToolClass.resultToJson("data", esClient.getDocs(index, type, searchType, input, offset, size)) .toString(); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception).toString(); } } /** * bug解决了 * * @param index * @return * @throws IOException */ @GetMapping("/get/setting") @ResponseBody public JSONObject getSetting(String index) throws IOException { try { return ToolClass.resultToJson("data", esClient.getSetting(index)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @DeleteMapping("/delete/doc") @ResponseBody public JSONObject deleteRecordByIndexAndTypeAndId(String index, @RequestParam(value = "type", required = false, defaultValue = "type") String type, String id) throws IOException { try { return ToolClass.resultToJson("data", esClient.delDoc(index, type, id)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @GetMapping("/delete/index") @ResponseBody public JSONObject deleteIndex(String index) throws IOException { try { return ToolClass.resultToJson("data", esClient.delIndex(index)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @PostMapping("/update/doc") @ResponseBody public JSONObject updateRecord(String index, @RequestParam(value = "type", required = false, defaultValue = "type") String type, String id, Map
map) throws IOException { try { return ToolClass.resultToJson("data", esClient.creatDoc(index, type, id, map)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @PostMapping("/update/mapping") @ResponseBody public JSONObject updateMapping(String index, @RequestParam(value = "type", required = false, defaultValue = "type") String type, Map
map) throws IOException { try { return ToolClass.resultToJson("data", esClient.setMapping(index, type, map)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } @PostMapping("/update/replica") @ResponseBody public JSONObject updateReplica(String index, int repliceCount) throws IOException { try { return ToolClass.resultToJson("data", esClient.setReplicaCountCount(index, repliceCount)); } catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); return ToolClass.resultToJson("exception", exception); } } /** * test * * @param args */ public static void main(String[] args) { ESClientController esc = new ESClientController(); System.out.println( ToolClass.resultToJson("", esc.getDocs("website_formal_20160612_disaster", "type", 3, "", 0, 2))); } }
