遠端連接

Elastic cloud 透過 Spring 實做遠端連接功能


一、依賴項

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>ElasticTest</artifactId> <version>1.0-SNAPSHOT</version> <name>ElasticTest</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <repositories> <repository> <id>elasticsearch-releases</id> <url>https://artifacts.elastic.co/maven</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.12.0</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>

二、取資料

package org.example; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.*; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.Map; public class App { public static void main(String[] args ) throws IOException { // 建立連線端 String username = "yourUserName"; String password = "yourPassword"; String host = "yourHost"; // like 0b48338694f94e48a6a2ba77d7ddcbba.asia-east1.gcp.elastic-cloud.com int port = 9243; int nextPort = 9244; String protocol = "https"; final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(username, password)); RestClientBuilder Builder = RestClient.builder( new HttpHost(host, port, protocol), new HttpHost(host, nextPort, protocol)) .setHttpClientConfigCallback((h) -> h.setDefaultCredentialsProvider(credentialsProvider)); try (RestHighLevelClient client = new RestHighLevelClient(Builder)) { SearchSourceBuilder searchBuilder = SearchSourceBuilder.searchSource(); // index 的名稱 SearchRequest request = new SearchRequest("item").source(searchBuilder); SearchHits hits = client.search(request, RequestOptions.DEFAULT).getHits(); System.out.println(hits); // 逐筆取資料 for(SearchHit hit : hits) { Map<String, Object> sourceAsMap = hit.getSourceAsMap(); System.out.println(sourceAsMap); String user = (String) sourceAsMap.get("user"); String post_date = (String) sourceAsMap.get("post_date"); String message = (String) sourceAsMap.get("message"); System.out.println(String.format("user:%s data:%s message:%s",user , post_date, message)); } }catch (IOException ignored) { } } }

在建立Elastic Cloud的時候,會產生一份包含使用者名稱與密碼的Excel檔案

Host 取得參照下圖,如果Host 含有Protocol 與 Port,則需要手動去除