要獲取Elasticsearch(ES)數(shù)據(jù)庫中的數(shù)據(jù),可以使用ES的Java客戶端API。以下是一個簡單的例子:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.0</version>
</dependency>
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class ESClientExample {
public static void main(String[] args) {
try (RestHighLevelClient client = new RestHighLevelClient()) {
SearchRequest searchRequest = new SearchRequest("index_name");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits().getHits()) {
System.out.println(hit.getSourceAsString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
這段代碼將連接到名為"index_name"的ES索引,并執(zhí)行一個match_all查詢。然后,它將遍歷查詢結(jié)果并打印每個文檔的JSON表示。
請注意,這只是一個簡單的例子。根據(jù)您的具體需求,您可能需要更復(fù)雜的查詢和數(shù)據(jù)處理邏輯。您可以查閱ES的官方文檔以獲取更多關(guān)于Java客戶端API的信息和示例代碼。