Elasticsearch5.0 Java Api(五) -- 大量匯出索引

來源:互聯網
上載者:User

標籤:action   ==   建立   mon   void   UI   socket   display   query   

  從索引庫中將檢索到的資料匯出到外部檔案,檔案格式為.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 args24      */25     public static void main(String[] args) {26 27         try {28             // 設定叢集名稱 29             Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();30             // 建立client31             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

 

Elasticsearch5.0 Java Api(五) -- 大量匯出索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.