docker pull elasticsearch:7.4.2 安装docker
docker pull kibana:7.4.2 elasticSearch的可视化工具
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
echo "http.host:0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
chmod -R 777 /mydata/elasticsearch
启动 9200端口是调用rest api时访问es的端口 9300是es集群节点的通信端口
discovery.type=single-node 表示单节点启动
ES_JAVA_OPTS=”-Xms64m -Xmx128m” 这个如果不指定的话,es一启动就会把虚拟机内存全部占用
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
> -e "discovery.type=single-node" \
> -e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
> -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
> -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
> -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
> -d elasticsearch:7.4.2
0663715d00b56c775539045c92ba4cb4221f2596d0035834d76e9c6769174f2a
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://192.168.56.10:9200 -p 5601:5601 -d kibana:7.4.2
动词 : 相当于mysql中的insert
名词 : 相当于mysql中的database
在index(索引)中,可以定义一个或多个类型.
类似于mysql中的table,每一种类型的数据放在一起
保存在index(索引)下的,type下,是json格式的,就像mysql中的一条条数据
http://192.168.56.10:9200/_cat/nodes 查询es所有节点信息
172.17.0.4 59 81 3 0.07 0.19 0.13 dilm * 1a7f6f65b36d *表示主节点的意思
http://192.168.56.10:5601/ kibana的可视化界面端口
GET /_cat/nodes 查看所有节点
GET /_cat/health 查看节点的健康状态
GET /_cat/master 查看主节点信息
GET /_cat/indices 查看所有索引
GET /bank/_mapping 查看索引下面文档的字段类型
PUT/POST /索引名/类型名/加上唯一标识 再加上json数据
返回的结果中带_开头的表示元数据信息 _shards表示分片信息,
PUT请求 : 必须带id 如果多次发送同一数据的话,result 会变成updated 版本号version也会叠加
POST请求: 可以不带id,es会自动生成一个id, 也可以带id ,带上id的话情况跟put一致.
可以看到es不建议指定type,而是直接在index下直接创建
通过_update关键字更新的话 需要加doc 如果多次更新相同的内容的话 result为noop 就是没有做任何操作,版本号也不会变
_bulk 关键字
直接运行这个命令,如果docker容器中没有的话,会自动下载的
docker run -p 80:80 --name nginx -d nginx:1.10
docker run -p 80:80 --name nginx -v /mydata/nginx/html:/usr/share/nginx/html -v /mydata/nginx/logs:/var/log/nginx -v /mydata/nginx/conf:/etc/nginx -d nginx:1.10
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.4.2
package com.buba.springbootdemo.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
*/
@Configuration
public class ESConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
/*builder.addHeader("Authorization", "Bearer " + TOKEN);
builder.setHttpAsyncResponseConsumerFactory(
new HttpAsyncResponseConsumerFactory
.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));*/
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient restHighLevelClient(){
RestClientBuilder builder = null;
builder = RestClient.builder(new HttpHost("192.168.56.10",9200,"http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
package com.buba.springbootdemo.springboot;
import com.alibaba.fastjson.JSON;
import com.buba.springbootdemo.config.ESConfig;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootApplicationTests {
@Autowired
RestHighLevelClient restHighLevelClient;
@Test
public void test() throws Exception{
IndexRequest request = new IndexRequest("aaaa");
request.id("1");
request.source(JSON.toJSONString(new User("kxj", 22)), XContentType.JSON);
IndexResponse index = restHighLevelClient.index(request, ESConfig.COMMON_OPTIONS);
System.out.println(index);
}
class User {
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public User() {
}
String name;
Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}