Elasticsearch5.0 Java Api(五) -- 批量导出索引

  从索引库中将检索到的数据导出到外部文件,文件格式为.json

 1 package com.juyun.test;
 2
 3 import java.io.BufferedWriter;
 4 import java.io.File;
 5 import java.io.FileWriter;
 6 import java.io.IOException;
 7 import java.net.InetAddress;
 8 import java.net.UnknownHostException;
 9
10 import org.elasticsearch.action.search.SearchResponse;
11 import org.elasticsearch.client.transport.TransportClient;
12 import org.elasticsearch.common.settings.Settings;
13 import org.elasticsearch.common.transport.InetSocketTransportAddress;
14 import org.elasticsearch.index.query.QueryBuilder;
15 import org.elasticsearch.index.query.QueryBuilders;
16 import org.elasticsearch.search.SearchHits;
17 import org.elasticsearch.transport.client.PreBuiltTransportClient;
18
19 public class ElasticsearchBulkOut {
20
21     /**
22      * 把索引库中的文档以json格式批量导出到文件中
23      * @param args
24      */
25     public static void main(String[] args) {
26
27         try {
28             // 设置集群名称
29             Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
30             // 创建client
31             TransportClient client = new PreBuiltTransportClient(settings)
32                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.0.157"), 9300));
33
34             SearchResponse response = client.prepareSearch("flow")
35                     .setTypes("data")
36                     .setSize(5000)
37                     .setQuery(QueryBuilders.matchAllQuery())
38                     .execute().actionGet();
39             SearchHits resultHits = response.getHits();
40
41             File article = new File("C:/Users/juyun/Desktop/data.json");
42             FileWriter fw = new FileWriter(article);
43             BufferedWriter bfw = new BufferedWriter(fw);
44
45             if (resultHits.getHits().length == 0) {
46                 System.out.println("查到0条数据!");
47
48             } else {
49                 for (int i = 0; i < resultHits.getHits().length; i++) {
50                     String jsonStr = resultHits.getHits()[i]
51                             .getSourceAsString();
52                     System.out.println(jsonStr);
53                     bfw.write(jsonStr);
54                     bfw.write("\n");
55                 }
56             }
57             bfw.close();
58             fw.close();
59
60         } catch (UnknownHostException e) {
61             e.printStackTrace();
62         } catch (IOException e) {
63             e.printStackTrace();
64         }
65     }
66
67 }

ElasticSearchBulkOut

时间: 2024-10-27 12:16:32

Elasticsearch5.0 Java Api(五) -- 批量导出索引的相关文章

Elasticsearch5.0 Java Api(六) -- 批量导入索引

将计算机本地的.json格式文件中的数据,批量导入到索引库中 1 package com.juyun.test; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.net.InetAddress; 9 imp

Elasticsearch5.0 Java Api(一) -- 插入索引

Elasticsearch5.X官方手册 Elasticsearch5.X官方手册中文版 Elasticsearch5.X Java Api官方手册 导入maven依赖 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.0.0</version> </depend

Elasticsearch5.0 Java Api(四) -- 检索索引

测试检索索引库的功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 5 import org.elasticsearch.client.Client; 6 import org.elasticsearch.common.settings.Settings; 7 import org.elasticsearch.common.transport.InetSocketTransportAddress; 8 import or

Elasticsearch5.0 Java Api(三) -- 删除索引

测试删除索引的功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5 6 import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; 7 import org.elasticsearch.action.admin.indices.create.CreateInd

Elasticsearch5.0 Java Api(二) -- 更新索引

elasticsearch提供了多种更新索引的方式,这里简单介绍其中五种 1 package com.juyun.test; 2 3 import java.io.IOException; 4 import java.net.InetAddress; 5 import java.util.concurrent.ExecutionException; 6 7 import org.elasticsearch.action.index.IndexRequest; 8 import org.elast

Elasticsearch5.0 Java Api(七) -- 聚合查询

测试聚合查询功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 import java.util.List; 5 6 import org.elasticsearch.action.search.SearchResponse; 7 import org.elasticsearch.client.Client; 8 import org.elasticsearch.common.settings.Settings; 9 i

Elasticsearch5.0 Java Api(八) -- 常用DSL查询

测试常用的DSL组合查询功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 5 import org.elasticsearch.action.search.SearchResponse; 6 import org.elasticsearch.client.Client; 7 import org.elasticsearch.common.settings.Settings; 8 import org.elasticse

elasticsearch-5.x JAVA API(002)

elasticsearch-5.x JAVA API 第二部分:集群健康与段合并 1. 集群健康 首先定义一个类来存储集群健康信息 package com.zw.elasticsearch.cluster; public class ClusterHealth { // 集群名 private String clusterName; // 集群中节点数目 private int number_of_node; // 活跃总分片数目 private int number_of_activeShar

使用Lucene的java api 写入和读取索引库

import org.apache.commons.io.FileUtils;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.NumericDocValuesField;import org.a