安装包:见本人主页资源目录
集群方式:单服务器三台ES集群(多台服务器集群可参照修改配置文件)
上传文件到指定目录(/usr/local)
useradd -m esuser
passwd esuser
123456
mkdir elasticsearch
cd elasticsearch
#上传压缩包到此文件夹下
tar -zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz
mv (解压后的文件名) es1
tar -zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz
mv (解压后的文件名) es2
tar -zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz
mv (解压后的文件名) es3
rm -rf ./elasticsearch-7.6.2-linux-x86_64.tar.gz
cd ../
#赋权限
chmod 777 elasticsearch
chown -R esuser:esuser elasticsearch
#修改完配置文件之后可以启动(后面有两个服务器的配置,更改完之后再启动)
cd elasticsearch
./es1/bin/elasticsearch -d
./es2/bin/elasticsearch -d
./es3/bin/elasticsearch -d
#稍等片刻查看端口占用情况,端口都被占用则启动成功
netstat -nplt
conf下yml文件配置
#配置文件1(es1)PS:有需要的话可以配置下存储数据的路径 百度一大堆
cluster.name: kingOfTheKill
node.name: node-1
#表示它可以被选为master
node.master: true
#表示可以进行存储
node.data: true
node.ingest: false
network.host: 0.0.0.0
http.port: 9200
transport.port: 9300
discovery.seed_hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
cluster.initial_master_nodes: ["node-1"]
http.cors.enabled: true
http.cors.allow-origin: "*"
#配置文件2(es2)PS:有需要的话可以配置下存储数据的路径 百度一大堆
cluster.name: kingOfTheKill
node.name: node-2
#表示它可以被选为master
node.master: true
#表示可以进行存储
node.data: true
node.ingest: false
network.host: 0.0.0.0
http.port: 9201
transport.port: 9301
discovery.seed_hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
cluster.initial_master_nodes: ["node-1"]
http.cors.enabled: true
http.cors.allow-origin: "*"
#配置文件3(es3)PS:有需要的话可以配置下存储数据的路径 百度一大堆
cluster.name: kingOfTheKill
node.name: node-3
#表示它可以被选为master
node.master: true
#表示可以进行存储
node.data: true
node.ingest: false
network.host: 0.0.0.0
http.port: 9202
transport.port: 9302
discovery.seed_hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
cluster.initial_master_nodes: ["node-1"]
http.cors.enabled: true
http.cors.allow-origin: "*"
服务器配置:
#进入 /etc/security/limits.conf
* hard nofile 65536
* soft nofile 65536
#然后:
vi /etc/sysctl.conf
#添加下面的配置
vm.max_map_count=655360
#退出后执行下面的命令
sysctl -p
#部分服务器可能由于配置问题(JVM内存不足) 无法启动 修改jvm.options
安装elasticsearch-head
安装包:见本人主页资源目录
解压即可
双击bat文件(需要node.js环境 百度装一个即可)
ES简单使用 ES获取客户端 依赖:<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>7.15.2version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
<version>7.15.2version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-clientartifactId>
<version>7.15.2version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-client-snifferartifactId>
<version>7.15.2version>
dependency>
获取客户端:
public class ESClient {
private static ESClient ESClient;
private String host = "2.36.80.1:9200";
private RestClientBuilder builder;
private static Sniffer sniffer;
private static RestHighLevelClient highClient;
private ESClient(){
}
public static ESClient getInstance() {
if (ESClient == null) {
synchronized (ESClient.class) {
if (ESClient == null) {
ESClient = new ESClient();
ESClient.getRestClientBuilder();
}
}
}
return ESClient;
}
public RestClientBuilder getRestClientBuilder() {
String[] hosts = host.split(",");
HttpHost[] httpHosts = new HttpHost[hosts.length];
for (int i = 0; i < hosts.length; i++) {
String[] host = hosts[i].split(":");
httpHosts[i] = new HttpHost(host[0], Integer.parseInt(host[1]), "http");
}
builder = RestClient.builder(httpHosts);
//region 在Builder中设置请求头
// 1.设置请求头
Header[] defaultHeaders = new Header[]{
new BasicHeader("Content-Type", "application/json")
};
builder.setDefaultHeaders(defaultHeaders);
//endregion
// RestClient restClient = builder.build();
return builder;
}
public RestHighLevelClient getHighLevelClient() {
if (highClient == null) {
synchronized (ESClient.class) {
if (highClient == null) {
highClient = new RestHighLevelClient(builder);
// //十秒刷新并更新一次节点
sniffer = Sniffer.builder(highClient.getLowLevelClient())
.setSniffIntervalMillis(5000)
.setSniffAfterFailureDelayMillis(15000)
.build();
}
}
}
return highClient;
}
/**
*
* 关闭sniffer client
*/
public void closeClient() {
if (null != highClient) {
try {
sniffer.close(); //需要在highClient close之前 *** 作
highClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
*** 作(CRUD)
/**
* 增删改es数据工具类
*
*/
@Component
public class ESUtils {
@Autowired
private SysDeptService deptService;
/**
* 向es中添加一条数据
*
* @throws Exception
*/
public ReplicationResponse.ShardInfo insertESTaskIssueVo(ESTaskIssueVo esTaskIssueVo) throws Exception {
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
Map<String, Object> jsonMap = objectToMap(esTaskIssueVo);
IndexRequest request = new IndexRequest("task_issue_person")
.source(jsonMap);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
//ESClient.getInstance().closeClient();
// 返回的json信息
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
return shardInfo;
}
/**
* 向es中添加一条数据SamplingPerson
*
* @throws Exception
*/
public ReplicationResponse.ShardInfo insertSamplingPerson(SamplingPerson samplingPerson) throws Exception {
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
Map<String, Object> jsonMap = objectToMap(samplingPerson);
IndexRequest request = new IndexRequest("task_sampling_person")
.id(String.valueOf(samplingPerson.getId())).source(jsonMap);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
// 返回的json信息
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
return shardInfo;
}
/**
* es中修改一条SamplingPerson数据
*
* @return
* @throws Exception
*/
public ReplicationResponse.ShardInfo updateSamplingPerson(SamplingPerson samplingPerson) throws Exception {
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
Map<String, Object> jsonMap = objectToMap(samplingPerson);
UpdateRequest request = new UpdateRequest("task_sampling_person", String.valueOf(samplingPerson.getId()))
.doc(jsonMap);
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
ReplicationResponse.ShardInfo shardInfo = updateResponse.getShardInfo();
return shardInfo;
}
/**
* es中修改一条PersonInfo数据
*
* @return
* @throws Exception
*/
public ReplicationResponse.ShardInfo updateESTaskIssueVo(ESTaskIssueVo esTaskIssueVo) throws Exception {
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (esTaskIssueVo.getPersonId() != null) {
boolQueryBuilder.must(QueryBuilders.matchQuery("personId", esTaskIssueVo.getPersonId()).operator(Operator.AND));
}
boolQueryBuilder.must(QueryBuilders.matchQuery("taskId", esTaskIssueVo.getTaskId()).operator(Operator.AND));
builder.query(boolQueryBuilder);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("task_issue_person");
searchRequest.source(builder);
SearchResponse searchResponse = null;
try {
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
SearchHit[] hits = searchResponse.getHits().getHits();
String index = "";
if (hits != null && hits.length > 0) {
index = JSONObject.parseObject(hits[0].toString()).getString("_id");
}
Map<String, Object> jsonMap = objectToMap(esTaskIssueVo);
UpdateRequest request = new UpdateRequest("task_issue_person", index)
.doc(jsonMap);
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
ReplicationResponse.ShardInfo shardInfo = updateResponse.getShardInfo();
return shardInfo;
}
/**
* 查询person Info 的list
*
* @param esTaskIssueVo
* @return
*/
public List<ESTaskIssueVo> getESTaskIssueVoList(ESTaskIssueVo esTaskIssueVo) {
SysUser user = SecurityUtils.getLoginUser().getUser();
String[] cydIds = null;
if (!user.isAdmin()) {
List<SysDept> depts = deptService.selectChildDeptCYD(user.getDeptId().toString());
List<Long> deptIds = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
List<String> collect = deptIds.stream().map(x -> x + "").collect(Collectors.toList());
cydIds = collect.toArray(new String[collect.size()]);
}
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("taskId", esTaskIssueVo.getTaskId()).operator(Operator.AND));
if (cydIds != null) {
boolQueryBuilder.must(QueryBuilders.termsQuery("cydId", cydIds));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getStatus())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("status", esTaskIssueVo.getStatus()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getCydId())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("cydId", esTaskIssueVo.getCydId()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getFlagWhsStatus())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("flagWhsStatus", esTaskIssueVo.getFlagWhsStatus()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getName())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("name", esTaskIssueVo.getName()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getPhone())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("phone", esTaskIssueVo.getPhone()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getPersonIdCard())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("personIdCard", esTaskIssueVo.getPersonIdCard()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getXqName())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("xqName", esTaskIssueVo.getXqName()).operator(Operator.AND));
}
if (esTaskIssueVo.getPageNum() != null && esTaskIssueVo.getPageNum() != 0) {
int size = esTaskIssueVo.getPageSize();
int from = (esTaskIssueVo.getPageNum() - 1) * size;
builder.from(from);
builder.size(size);
}
builder.query(boolQueryBuilder);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("task_issue_person");
searchRequest.source(builder);
SearchResponse searchResponse = null;
CountRequest countRequest = new CountRequest();
countRequest.indices("task_issue_person");
countRequest.source(builder);
try {
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
CountResponse response = client.count(countRequest, RequestOptions.DEFAULT);
long count = response.getCount();
} catch (IOException e) {
throw new RuntimeException(e);
}
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
ESTaskIssueVo resultItem = new ESTaskIssueVo();
List<ESTaskIssueVo> TaskIssueVos = new ArrayList<>();
for (SearchHit item : hits1) {
resultItem = JSONObject.parseObject(item.toString()).getObject("_source", ESTaskIssueVo.class);
TaskIssueVos.add(resultItem);
}
return TaskIssueVos;
}
/**
* 查询person Info list 总数
*
* @param esTaskIssueVo
* @return
*/
public long getESTaskIssueVoListCount(ESTaskIssueVo esTaskIssueVo) {
SysUser user = SecurityUtils.getLoginUser().getUser();
String[] cydIds = null;
if (!user.isAdmin()) {
List<SysDept> depts = deptService.selectChildDeptCYD(user.getDeptId().toString());
List<Long> deptIds = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
List<String> collect = deptIds.stream().map(x -> x + "").collect(Collectors.toList());
cydIds = collect.toArray(new String[collect.size()]);
}
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("taskId", esTaskIssueVo.getTaskId()).operator(Operator.AND));
if (cydIds != null) {
boolQueryBuilder.must(QueryBuilders.termsQuery("cydId", cydIds));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getStatus())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("status", esTaskIssueVo.getStatus()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getCydId())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("cydId", esTaskIssueVo.getCydId()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getFlagWhsStatus())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("flagWhsStatus", esTaskIssueVo.getFlagWhsStatus()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getName())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("name", esTaskIssueVo.getName()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getPhone())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("phone", esTaskIssueVo.getPhone()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getPersonIdCard())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("personIdCard", esTaskIssueVo.getPersonIdCard()).operator(Operator.AND));
}
if (StringUtils.isNotEmpty(esTaskIssueVo.getXqName())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("xqName", esTaskIssueVo.getXqName()).operator(Operator.AND));
}
if (esTaskIssueVo.getPageNum() != null && esTaskIssueVo.getPageNum() != 0) {
int size = esTaskIssueVo.getPageSize();
int from = (esTaskIssueVo.getPageNum() - 1) * size;
builder.from(from);
builder.size(size);
}
builder.query(boolQueryBuilder);
CountResponse response = null;
CountRequest countRequest = new CountRequest();
countRequest.indices("task_issue_person");
countRequest.source(builder);
try {
response = client.count(countRequest, RequestOptions.DEFAULT);
return response.getCount();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 单个查询一条personInfo
*
* @param idCard
* @param name
* @param taskId
* @return
*/
public ESTaskIssueVo getESTaskIssueVoList(String idCard, String name, String taskId) {
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("ID_CARD_NUM", idCard).operator(Operator.AND));
boolQueryBuilder.must(QueryBuilders.matchQuery("NAME", name).operator(Operator.AND));
boolQueryBuilder.must(QueryBuilders.matchQuery("taskId", taskId).operator(Operator.AND));
builder.query(boolQueryBuilder);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("task_issue_person");
searchRequest.source(builder);
SearchResponse searchResponse = null;
try {
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
ESTaskIssueVo esTaskIssueVo = null;
SearchHit[] hits = searchResponse.getHits().getHits();
if (hits != null && hits.length > 0) {
esTaskIssueVo = JSONObject.parseObject(hits[0].toString()).getObject("_source", ESTaskIssueVo.class);
}
return esTaskIssueVo;
}
/**
* 批量插入issue表里的数据到es
*
* @param esTaskIssueVos
* @throws Exception
*/
public void insertESTaskIssueVoList(List<ESTaskIssueVo> esTaskIssueVos) throws Exception {
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
for (ESTaskIssueVo esTaskIssueVo : esTaskIssueVos) {
Map<String, Object> jsonMap = objectToMap(esTaskIssueVo);
IndexRequest request = new IndexRequest("task_issue_person")
.source(jsonMap);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
//ESClient.getInstance().closeClient();
// 返回的json信息
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
System.out.println("插入es信息:\n 姓名:" + esTaskIssueVo.getName() +"任务id:" +esTaskIssueVo.getTaskId());
}
}
/**
* 批量删除es中数据
*
* @throws Exception
*/
public BulkResponse esDeleteFile(String ids) throws Exception {
RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
BulkRequest request = new BulkRequest();
String[] arrIds = Convert.toStrArray(ids);
for (int i = 0; i < arrIds.length; i++) {
request.add(new DeleteRequest("task_issue_person").id(arrIds[i]));
}
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
//ESClient.getInstance().closeClient();
return bulkResponse;
}
/**
* 将Object对象里面的属性和值转化成Map对象
*
* @param obj
* @return
* @throws IllegalAccessException
*/
public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap<>();
// 获取f对象对应类中的所有属性域
Field[] fields = obj.getClass().getDeclaredFields();
for (int i = 0, len = fields.length; i < len; i++) {
String varName = fields[i].getName();
// 获取原来的访问控制权限
boolean accessFlag = fields[i].isAccessible();
// 修改访问控制权限
fields[i].setAccessible(true);
// 获取在对象f中属性fields[i]对应的对象中的变量
Object o = fields[i].get(obj);
if (o != null && StringUtils.isNotBlank(o.toString().trim())) {
map.put(varName, o.toString().trim());
// 恢复访问控制权限
fields[i].setAccessible(accessFlag);
}
}
return map;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)