服务搭建
阿里云可以1块钱包月:1元开通ELK 购买的时候要注意版本,活动什么时候结束就不知道了,配置也很简单,就不介绍了,不懂的可以评论问我
创建索引并指定数据结构相关解析
以下是在kibana中使用的,记住这种结构,也方便后面使用java来操作时更好理解相关代码。
###创建索引,指定数据结构 PUT /book { "settings": { ###分片数 "number_of_shards": 5, ###备份数 "number_of_replicas": 1 }, ###指定数据结构 "mappings": { ###类型 Type "novel":{ ### Field "properties":{ ###Field 属性名 "name":{ ###类型 "type": "text", ###指定分词器 "analyzer": "ik_max_word", ###指定当前Field可以被作为查询条件 默认值true "index": true, ###是否需要额外存储 默认值false "store": false }, "author":{ "type": "keyword" }, "onSale":{ "type": "date", "format": "yyyy-MM-dd HH:mm:ss" }, "descr":{ "type": "text", "analyzer": "ik_max_word" } } } } }
JAVA操作Elasticsearch
RestHighLevelClient方式
pom.xml
<!-- elasticsearch --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.5.4</version> </dependency> <!-- elasticsearch-rest-high-level-client --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.5.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency>
ESClient
public class ESClient { public static RestHighLevelClient client = null; public static RestHighLevelClient getClient(){ //不需要用户名和密码的认证 //client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", "9200", "http"))); //需要用户名和密码的认证 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("账号", "密码")); RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("如果使用阿里云实例,这里填公网地址", 9200, "http")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); client = new RestHighLevelClient(restClientBuilder); return client; } }
对索引的基本操作
在DemoOne中是对索引的一些基本操作,这里需要注意根据操作的不同,获取的request对象是不同的
import com.example.esdemo.utils.ESClient; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.junit.Test; import java.io.IOException; /** * 操作索引 */ public class DemoOne { RestHighLevelClient client = ESClient.getClient(); String index = "person"; String type = "man"; /** * 创建索引 * @throws IOException */ @Test public void createIndex() throws IOException { //准备关于索引的settings Settings.Builder settings = Settings.builder() .put("number_of_shards",3) .put("number_of_replicas",1); //准备关于索引的结构mappings //这里的startObject() endObject()相当于{} 是成对出现的 XContentBuilder mappings = JsonXContent.contentBuilder() .startObject() .startObject("properties") .startObject("name") .field("type","text") .endObject() .startObject("age") .field("type","integer") .endObject() .startObject("birthday") .field("type","date") .field("format","yyyy-MM-dd") .endObject() .endObject() .endObject(); //把settings和mappings封装到一个Request对象中 CreateIndexRequest request = new CreateIndexRequest(index) .settings(settings) .mapping(type,mappings); //通过client对象去连接es并执行创建索引 CreateIndexResponse indexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(indexResponse.toString()); } /** * 检查索引是否存在 * @throws IOException */ @Test public void exists() throws IOException { //获取request对象 GetIndexRequest request = new GetIndexRequest(); //设置索引 request.indices(index); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println("是否存在:"+exists); } /** * 删除索引 * @throws IOException */ @Test public void delete() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest(); request.indices(index); AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged()); } }
对文档的基本操作
这里先贴出Person类
public class Person { @JsonIgnore private Integer id; private String name; private Integer age; @JsonFormat(pattern = "yyyy-MM-dd") private Date birthday; public Person() { } public Person(Integer id, String name, Integer age, Date birthday) { this.id = id; this.name = name; this.age = age; this.birthday = birthday; } getter()....setter().... }
文档的增删改
/** * 操作文档 */ public class DemoTwo { ObjectMapper mapper = new ObjectMapper(); RestHighLevelClient client = ESClient.getClient(); String index = "person"; String type = "man"; /** * 创建文档 * @throws IOException */ @Test public void createDoc() throws IOException { //准备一个json数据 Person person = new Person(1,"张三",25,new Date()); //序列化成json字符串 String json = mapper.writeValueAsString(person); //准备request对象(手动指定id) IndexRequest request = new IndexRequest(index,type,person.getId().toString()); request.source(json, XContentType.JSON); //通过client执行添加 IndexResponse response = client.index(request, RequestOptions.DEFAULT); //返回结果 System.out.println(response.getResult().toString()); } /** * 修改文档 */ @Test public void updateDoc() throws IOException { //创建一个Map,指定需要修改的内容 Map<String,Object> doc = new HashMap<>(); doc.put("name","李晓明"); //在创建时指定了id是1 String docId = "1"; //创建request对象,封装数据 UpdateRequest request = new UpdateRequest(index, type, docId); request.doc(doc); //通过client对象去执行 UpdateResponse response = client.update(request, RequestOptions.DEFAULT); //输出返回结果 System.out.println(response.getResult().toString()); } /** * 删除文档 * @throws IOException */ @Test public void deleteDoc() throws IOException { String docId = "1"; DeleteRequest request = new DeleteRequest(index, type, docId); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); System.out.println(response.getResult().toString()); } }
但是一个一个太慢了,来看一下批量操作文档,还是在Demo2中测试
/** * 批量创建文档 */ @Test public void bulkCreateDoc() throws IOException { //准备数据 Person p1 = new Person(1,"李四",23,new Date()); Person p2 = new Person(2,"王五",24,new Date()); Person p3 = new Person(3,"麻溜",25,new Date()); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); //创建request,把准备好的数据封装进去 BulkRequest bulkRequest = new BulkRequest(); list.forEach(item -> { try { String json = mapper.writeValueAsString(item); bulkRequest.add(new IndexRequest(index,type,item.getId().toString()).source(json,XContentType.JSON)); } catch (JsonProcessingException e) { e.printStackTrace(); } }); //用client执行 BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT); //输出返回结果 System.out.println(response.toString()); } /** * 批量删除文档 * @throws IOException */ @Test public void bulkDeleteDoc() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(new DeleteRequest(index,type,"1")); bulkRequest.add(new DeleteRequest(index,type,"2")); BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(response.toString()); }