基本搭建配置
=================================基本环境配置==================================
配置linux系统环境(参考:http://blog.youkuaiyun.com/satiling/article/details/59697916)
(1)编辑limits.conf 添加类似如下内容
[itstar@bigdata111 elasticsearch-5.6.1]$ sudo vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
(2)修改配置sysctl.conf
[itstar@bigdata111 elasticsearch-5.6.1]$ sudo vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
[itstar@bigdata111 elasticsearch-5.6.1]$ sudo sysctl -p
=================================elasticsearch.yml==================================
#集群名称
cluster.name: my-application
#节点名称,其他节点分别依次递增
node.name: node-1
# 表示该节点会不会作为主节点,true表示会;false表示不会
node.master: true
# 当前节点是否用于存储数据,是:true、否:false
node.data: true
#数据地址
path.data: /opt/module/elasticsearch-7.6.2/data
#日志地址
path.logs: /opt/module/elasticsearch-7.6.2/logs
#内存超出后是否锁定
bootstrap.memory_lock: false
#是否检测支持SecComp,Centos6为不支持
bootstrap.system_call_filter: false
#网卡设置,其他阶段写各自的主机名
network.host: bigdata111
#是否发现该节点,写自己的ip地址,我之前写主机名好像不行.此段自测
discovery.seed_hosts: ["192.168.220.112", "192.168.220.113", "192.168.220.114"]
#是否发现成为主节点,道理同上
cluster.initial_master_nodes: ["192.168.220.112", "192.168.220.113", "192.168.220.114"]
Pom提供
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 使用自定义ES 版本 -->
<elasticsearch.version>7.6.2</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
JavaAPI代码
下边是ES的增删改查基本JavaAPI代码
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.security.user.User;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class ElasticSearchConfig {
static RestHighLevelClient client;
@Before
public void client() throws IOException {
client = new RestHighLevelClient(RestClient.builder(
new HttpHost("bigdata111", 9200, "http")
));
}
//创建索引
@Test
public void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("index");
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.index());
}
//删除索引
@Test
public void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("index");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
boolean isSuccessful = delete.isAcknowledged();
System.out.println(isSuccessful);
}
//返回索引是否存在
@Test
public void existIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//删除文档
@Test
public void deleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("index", "2");
request.timeout("1s");
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
}
//查询
@Test
public void search() throws IOException {
SearchRequest request = new SearchRequest("index");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// SearchRequest 搜索请求
// SearchSourceBuilder 条件构造
// HighlightBuilder 构建高亮
// TermQueryBuilder 精确查询
// MatchAllQueryBuilder .....
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
sourceBuilder.query(matchAllQueryBuilder)
.timeout(new TimeValue(60, TimeUnit.SECONDS));
request.source(sourceBuilder);
SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits(), true));
System.out.println("===================================");
for (SearchHit documentFields : searchResponse.getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
//批量插入数据
@Test
public void BulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest()
.timeout("5s");
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
List<User> users = Arrays.asList(new User("dai1", list), new User("dai2", list), new User("dai3", list));
for (User user : users) {
bulkRequest.add(new IndexRequest("index")
//.id("xxx")
.source(JSON.toJSONString(user), XContentType.JSON));
}
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//是否失败,false表示成功
System.out.println(bulkResponse.hasFailures());
System.out.println(bulkResponse.status());
}
//更新文档
@Test
public void updateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("index", "1");
request.timeout("1s");
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
User user = new User("dai22", list);
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
}
常见问题:
1. 最好是非root用户
useradd es
chown -R es.es /opt/module
2.es 7.6.2的header google插件用之前的还不能显示,暂时没时间解决.有时间更.
3. es 7.6 的 jdk显示用jdk11的版本,不用管,我依旧用的 jdk8