java-api操作ElasticSearch
上兩篇文章我們介紹了Elasticsearch以及其相關中間件的部署和基本操作,現在簡單的介紹一下java-api連接Es并創建索引,
public class EsConnectTest {
private static Logger logger = LoggerFactory.getLogger(EsConnectTest.class);
public final static String HOST = "127.0.0.1";
public final static int PORT = 9300;//http請求的埠是9200,客戶端是9300
public static void main(String[] args) {
List<ArchivesBaseInfo> archivesBaseInfos = new ArrayList<>();
ArchivesBaseInfo archivesBaseInfo = new ArchivesBaseInfo();
archivesBaseInfo.setInfoId("e962774e-5435-4584-8673-d9873feded8e");
archivesBaseInfo.setBoxId("73556be0-44c1-4c7f-9e89-a568cef53bd9");
archivesBaseInfo.setTitle("阿薩德 asd");
archivesBaseInfo.setArchiveNumber("141-WS.4500-10年-辦文處-0001");
archivesBaseInfo.setArchiveUserName("斑紋出");
archivesBaseInfo.setReferenceNumber("斑紋虎[2020]66號");
archivesBaseInfo.setCreateTime(new Date());
archivesBaseInfo.setPersonLiable("習近平");
archivesBaseInfos.add(archivesBaseInfo);
try {
createIndex(archivesBaseInfos);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
public static Client getClient() throws UnknownHostException{
/*Settings settings = Settings.settingsBuilder().put("cluster.name","elasticsearch").build();
TransportClient transportClient = TransportClient.builder().settings(settings).build();
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST),PORT));*/
Client client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST),PORT));
return client;
}
public static void createIndex(List<ArchivesBaseInfo> archivesBaseInfos) throws UnknownHostException, JsonProcessingException {
Client client = getClient();
if (client.admin().indices().prepareExists("test_index").get().isExists()){
client.admin().indices().prepareDelete("test_index").get();
}
//自定義mapping
String mapperStr = "{ \"goods\" : { \"properties\": { \"id\": { \"type\": \"long\" }, \"name\": {\"type\": \"string\", \"analyzer\": \"ik_max_word\"}, \"regionIds\": {\"type\": \"string\",\"index\": \"not_analyzed\"}}}}";
//使用物體類做mapping
client.admin().indices().prepareCreate("archive").addMapping("content", ArchiveContentVo.class).get();
ObjectMapper objectMapper = new ObjectMapper();
//批量處理request
BulkRequestBuilder requestBuilder = client.prepareBulk();
byte[] json;
for (ArchivesBaseInfo archive:archivesBaseInfos
) {
json = objectMapper.writeValueAsBytes(archive);
requestBuilder.add(new IndexRequest("archive","content",archive.getInfoId() + "").source(json));
}
BulkResponse bulkItemResponses = requestBuilder.get();
//處理錯誤資訊
if (bulkItemResponses.hasFailures()){
logger.error("===================創建索引時出現錯誤=====================");
int count = 0;
for (BulkItemResponse item:bulkItemResponses
) {
logger.error("發生錯誤的索引id為:"+item.getId()+",錯誤資訊為:"+item.getFailureMessage());
count++;
}
logger.error("================批量創建索引時出錯,共計:"+count+"條=============================");
}
client.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/265881.html
標籤:其他
上一篇:Hadoop完全分布式搭建
