繁体   English   中英

如何在Java版本6.5.1中使用Elastic Search

[英]How to Use Elastic Search with Java version 6.5.1

我需要的

  • 我需要构建Crud Insert Update从Mysql删除和获取数据。

我看过Elastic search的文档

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html

我确实在Postman Rest客户端中尝试了一些示例。

我找到了链接

https://www.javacodegeeks.com/2018/03/elasticsearch-tutorial-beginners.html

适用于Elastic 6.2.1

作为6.5.1 Pom.xml

                    <?xml version="1.0" encoding="UTF-8"?>
            <project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
              <modelVersion>4.0.0</modelVersion>
              <groupId>org.elasticsearch</groupId>
              <artifactId>elasticsearch</artifactId>
              <version>6.5.1</version>
              <dependencies>
                <dependency>
                  <groupId>org.elasticsearch</groupId>
                  <artifactId>elasticsearch-core</artifactId>
                  <version>6.5.1</version>
                  <scope>compile</scope>
                </dependency>
                <dependency>
                  <groupId>org.elasticsearch.client</groupId>
                  <artifactId>elasticsearch-rest-high-level-client</artifactId>
                  <version>6.5.1</version>
                </dependency>
                <dependency>
                  <groupId>org.elasticsearch</groupId>
                  <artifactId>elasticsearch-secure-sm</artifactId>
                  <version>6.5.1</version>
                  <scope>compile</scope>
                </dependency>

Application.java

                    package com.javacodegeeks.example;

                import com.fasterxml.jackson.core.JsonProcessingException;
                import com.fasterxml.jackson.databind.ObjectMapper;
                import org.apache.http.HttpHost;
                import org.elasticsearch.ElasticsearchException;
                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.update.UpdateRequest;
                import org.elasticsearch.action.update.UpdateResponse;
                import org.elasticsearch.client.RestClient;
                import org.elasticsearch.client.RestHighLevelClient;
                import org.elasticsearch.common.xcontent.XContentType;
                import org.elasticsearch.client.*;

                import java.io.IOException;
                import java.util.HashMap;
                import java.util.Map;
                import java.util.UUID;

                public class Application {

                 //The config parameters for the connection
                 private static final String HOST = "localhost";
                 private static final int PORT_ONE = 9200;
                 private static final int PORT_TWO = 9201;
                 private static final String SCHEME = "http";

                 private static RestHighLevelClient restHighLevelClient;
                 private static ObjectMapper objectMapper = new ObjectMapper();

                 private static final String INDEX = "persondata";
                 private static final String TYPE = "person";

                 /**
                  * Implemented Singleton pattern here
                  * so that there is just one connection at a time.
                  * @return RestHighLevelClient
                  */
                 private static synchronized RestHighLevelClient makeConnection() {

                  if (restHighLevelClient == null) {
                   /*restHighLevelClient client = new RestHighLevelClient(
                           RestClient.builder(
                                   new HttpHost("localhost", 9200, "http"),
                                   new HttpHost("localhost", 9201, "http")));
                                   */
                   restHighLevelClient = new RestHighLevelClient(
                    RestClient.builder(
                     new HttpHost(HOST, PORT_ONE, SCHEME),
                     new HttpHost(HOST, PORT_TWO, SCHEME)));
                  }

                  return restHighLevelClient;
                 }

                 private static synchronized void closeConnection() throws IOException {
                  restHighLevelClient.close();
                  restHighLevelClient = null;
                 }

                 private static Person insertPerson(Person person) {
                  person.setPersonId(UUID.randomUUID().toString());
                  Map < String, Object > dataMap = new HashMap < String, Object > ();
                  dataMap.put("personId", person.getPersonId());
                  dataMap.put("name", person.getName());
                  IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, person.getPersonId())
                   .source(dataMap);
                  try {
                   IndexResponse response = restHighLevelClient.index(indexRequest);
                  } catch (ElasticsearchException e) {
                   e.getDetailedMessage();
                  } catch (java.io.IOException ex) {
                   ex.getLocalizedMessage();
                  }
                  return person;
                 }

                 private static Person getPersonById(String id) {
                  GetRequest getPersonRequest = new GetRequest(INDEX, TYPE, id);
                  GetResponse getResponse = null;
                  try {
                   getResponse = restHighLevelClient.get(getPersonRequest);
                  } catch (java.io.IOException e) {
                   e.getLocalizedMessage();
                  }
                  return getResponse != null ?
                   objectMapper.convertValue(getResponse.getSourceAsMap(), Person.class) : null;
                 }

                 private static Person updatePersonById(String id, Person person) {
                  UpdateRequest updateRequest = new UpdateRequest(INDEX, TYPE, id)
                   .fetchSource(true); // Fetch Object after its update
                  try {
                   String personJson = objectMapper.writeValueAsString(person);
                   updateRequest.doc(personJson, XContentType.JSON);
                   UpdateResponse updateResponse = restHighLevelClient.update(updateRequest);
                   return objectMapper.convertValue(updateResponse.getGetResult().sourceAsMap(), Person.class);
                  } catch (JsonProcessingException e) {
                   e.getMessage();
                  } catch (java.io.IOException e) {
                   e.getLocalizedMessage();
                  }
                  System.out.println("Unable to update person");
                  return null;
                 }

                 private static void deletePersonById(String id) {
                  DeleteRequest deleteRequest = new DeleteRequest(INDEX, TYPE, id);
                  try {
                   DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest);
                  } catch (java.io.IOException e) {
                   e.getLocalizedMessage();
                  }
                 }

                 public static void main(String[] args) throws IOException {

                  makeConnection();

                  System.out.println("Inserting a new Person with name testst...");
                  Person person = new Person();
                  person.setName("Testttt");
                  person = insertPerson(person);
                  System.out.println("Person inserted --> " + person);

                  System.out.println("Changing name to testst...");
                  person.setName("testst");
                  updatePersonById(person.getPersonId(), person);
                  System.out.println("Person updated  --> " + person);

                  System.out.println("Getting testst...");
                  Person personFromDB = getPersonById(person.getPersonId());
                  System.out.println("Person from DB  --> " + personFromDB);

                  System.out.println("Deleting teststss...");
                  deletePersonById(personFromDB.getPersonId());
                  System.out.println("Person Deleted");

                  closeConnection();
                 }
                }

Person.java

            package com.javacodegeeks.example;

        public class Person {

        private String personId;
        private String name;

        public String getPersonId() {
            return personId;
        }

        public void setPersonId(String personId) {
            this.personId = personId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return String.format("Person{personId='%s', name='%s'}", personId, name);
        }
        }

任何人都可以建议使用6.5版本指导示例示例的任何教程。

HTTP://本地主机:9200 /

杰森输出

            {
        name: "MIT22",
        cluster_name: "elasticsearch",
        cluster_uuid: "KMJcFFe9ST6H7bbir3OPzQ",
        version: {
        number: "6.5.1",
        build_flavor: "default",
        build_type: "zip",
        build_hash: "8c58350",
        build_date: "2018-11-16T02:22:42.182257Z",
        build_snapshot: false,
        lucene_version: "7.5.0",
        minimum_wire_compatibility_version: "5.6.0",
        minimum_index_compatibility_version: "5.0.0"
        },
        tagline: "You Know, for Search"
        }

Refrence

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html

https://www.elastic.co/blog/logstash-jdbc-input-plugin

https://github.com/jprante/elasticsearch-jdbc#quick-links

谢谢

Elasticsearch拥有自己的官方文档以及足够的示例。 您可以从这里开始。 页面的右侧提供了完整的内容,其中涵盖了您需要了解的几乎所有内容。

由于将使用Java应用程序执行CRUD操作,因此需要使用Elastic Search高级Rest客户端,该客户端是连接到Elastic并执行CRUD的Java客户端。 您可以在此处找到文档。

我想阅读有关某些主题的详细说明,您可以在这里找到官方的权威指南。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM