目录
- 索引映射文件示例
- 索引mapping创建
- 索引数据清空
- 测试数据添加
代码
import com.csg.dse.es.client.OkHttpUtil;
import com.csg.dse.es.config.EsComponUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.yaml.snakeyaml.Yaml;
import test.util.EsClientMe;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: tianya
* @Date: 2019/8/06
* @Description:创建es索引
*/
public class EsIndexCreate {
String index = "";
String urlEsBase = "http://192.168.10.124:9200/";
String urlEsQuery = "http://192.168.10.124:9200/_xpack/sql?format=json";
@Test
public void createWarnlog() {
createIndex("test20190821");
}
private void createIndex(String index) {
final URL resource = EsIndexCreate.class.getResource("/config/esIndexMapping.yml");
try (FileInputStream inputStream = new FileInputStream(new File(resource.getPath()))) {
Map<String, Object> map = (Map<String, Object>) new Yaml().load(inputStream);
//修改index名称
Object json = map.get(index);
System.out.println(json.toString());
String str = OkHttpUtil.putResponseWithParamsInJson(urlEsBase + index, json.toString());
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void addData() throws Exception {
index = "test20190821";
IndexRequest indexRequest = new IndexRequest(index);
HashMap<String, Object> map = new HashMap<>();
map.put("esid", "1001111111");
map.put("srvcCode", "服务C");
map.put("srvcName", "serviceM");
map.put("srvcEngName", "100");
map.put("monitorTarget", "100");
map.put("monitorTime", EsComponUtil.sourceTime2Time8("2022-08-09 09:15:00"));
//map.put("monitorTime", "2022-08-09 09:15:00");
map.put("trigger", "连续三次");
map.put("noticeMethod", "服务M ldskjflsdlfl");
map.put("noticePerson", "服务M ldskjflsdlfl");
map.put("alarmNotice", "服务M ldskjflsdlfl");
map.put("noticeCount", "服务M ldskjflsdlfl");
map.put("ruleid", "2");
map.put("isNotice", "已通知");
indexRequest.source(map, XContentType.JSON);
RestHighLevelClient client = EsClientMe.getRhlClient();
client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("OK");
}
@Test
public void emptyInd() {
String index = "test20190821";
final String res = emptyIndex(index);
System.out.println(res);
}
/**
* 清空es方法
*/
public String emptyIndex(String index) {
String jb = "{" + "\"query\": {" + " \"match_all\": {}" + " }" + "}";
String delUrl = urlEsBase + index + "/_doc/_delete_by_query";
return OkHttpUtil.postResponseWithParamsInJson(delUrl, jb);
}
/**
*获取yml文件信息
*/
@Test
public void getYmlInfo() {
final URL resource = EsIndexCreate.class.getResource("/config/esIndexMapping.yml");
System.out.println(resource.getPath());
try (FileInputStream inputStream = new FileInputStream(new File(resource.getPath()))) {
final Map<String, Object> map = (Map<String, Object>) new Yaml().load(inputStream);
System.out.println(map.get("warnlog"));
} catch (Exception e) {
e.printStackTrace();
}
}
yml文件示例
test20190821:
'{
"mappings" : {
"properties" : {
"esid" : {"type" : "keyword"},
"srvcCode" : {"type" : "keyword"},
"warnTime" : {"type" : "date"},
"isNotice" : {"type" : "keyword"}
}
},
"settings" : {
"number_of_replicas" : 1,
"number_of_shards" : 3
}
}'