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,則需要手動去除
Elastic App Search 後端連接有兩種方式,一種是透過ELK的Elastic 去取得 Elastic App 的資料,第二種是直接用Java 打API的方式,去取得 Elastic App Search 的資料 一、透過ELK查詢 Elastic App Search 取得Elastic App Search 的 index,可以從Kibana取得所有的Elastic 列表,GET _cat/indices?v&index=*engine* 回傳的結果
Apr 8, 2021Elastic App Search 透過前端連接來執行搜尋的動作,本文僅示範JavaScript連接 一、支援的客戶端 JavaScript Node.js PHP Python Ruby
Apr 8, 2021Elasticsearch docker 透過 Spring 實做本地連接功能 一、依賴項 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.12.0</version> </dependency> 二、寫入資料
Apr 8, 2021安裝 IKAnalyzer 一、安裝步驟 如果是使用官方ELK Docker yml 安裝的話,會有三個 cluster,這裡示範在es01安裝中文分詞 進入 elasticsearch bash docker exec -it ex01 bash 在 Elasticsearch bash 中,下載並安裝 IKAnalyzer
Apr 8, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up