创建ES专栏很久了,但是写的文章非常的少,实在是项目比较忙,2018年最后一天了也该总结一下对es的封装的东西了,这篇博客我们通过java对es批量插入为主流程,来介绍一下java对接es 的全部过程;
需求处理流程:
从mysql中插入手机号7位所有的组合,然后通过程序处理补充后四位所有的可能,然后组成一个庞大的手机号码库,然后在讲手机号加密,为其他的应用提供 手机号明密文转换服务;
1、引入jar
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.1</version>
</dependency>
2、根据配置创建索引
2.1、配置文件内容:
es.cluster.name=elasticsearch
es.cluster.nodes=39.**.45.**:9201 #集群ip端口都好分隔
es.client.useragent=ACS/Search 1.0
es.index.casetel={"settings":{"number_of_shards":2,"number_of_replicas":1,"max_result_window":10000000},"mappings":{"casetel":{"properties":{"telId":{"type":"long"},"tel":{"type":"keyword"},"encryptTel":{"type":"keyword"}}}}}
2.2、 配置文件对应 操作类
package com.zqf.search.es.model;
import com.zqf.common.utils.JsonUtils;
/**
* @description
* @param
* @date 10:18 2018/12/9
* @return
* @author zhenghao
*/
public class ElasticSearchConfig {
private String clusterName;
private String nodes;
private String userAgent;
private String caseTel;
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public String getNodes() {
return nodes;
}
public void setNodes(String nodes) {
this.nodes = nodes;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public String getCaseTel() {
return caseTel;
}
public void setCaseTel(String caseTel) {
this.caseTel = caseTel;
}
@Override
public String toString() {
return JsonUtils.writeValue(this);
}
}
通过 spring管理配置实体类:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-lazy-init="false">
<bean id="esNodeConfig" class="com.zqf.search.es.model.ElasticSearchConfig">
<property name="clusterName" value="${es.cluster.name}" />
<property name="nodes" value="${es.cluster.nodes}" />
<property name="userAgent" value="${es.client.useragent}" />
<property name="caseTel" value="${es.index.casetel}" />
</bean>
</beans>
2.3、系统启动检查是否有索引,如果没有根据配置创建相关索引
package com.zqf.search.es.service;
import java.util.Collections;
import java.util.Map;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zqf.search.es.model.ElasticSearchConfig;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.main.MainResponse;
import org.elasticsearch.action.search.*;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
/**
* @description
* @param
* @date 10:18 2018/12/9
* @return
* @author zhenghao
*/
@Service
public class ElasticSearchService implements ApplicationListener<ContextRefreshedEvent> {
private final Logger log = LoggerFactory.getLogger(ElasticSearchService.class);
public static final int CONNECT_TIMEOUT_MILLIS = 60 * 1000;
public static final int SOCKET_TIMEOUT_MILLIS = 5 * 60 * 1000;
public static final int MAX_RETRY_TIMEOUT_MILLIS = SOCKET_TIMEOUT_MILLIS;
public static