Exports the retrieved data from the index library to an external file in the format. JSON
1 Packagecom.juyun.test;2 3 ImportJava.io.BufferedWriter;4 ImportJava.io.File;5 ImportJava.io.FileWriter;6 Importjava.io.IOException;7 Importjava.net.InetAddress;8 Importjava.net.UnknownHostException;9 Ten ImportOrg.elasticsearch.action.search.SearchResponse; One Importorg.elasticsearch.client.transport.TransportClient; A Importorg.elasticsearch.common.settings.Settings; - Importorg.elasticsearch.common.transport.InetSocketTransportAddress; - ImportOrg.elasticsearch.index.query.QueryBuilder; the Importorg.elasticsearch.index.query.QueryBuilders; - Importorg.elasticsearch.search.SearchHits; - Importorg.elasticsearch.transport.client.PreBuiltTransportClient; - + Public classElasticsearchbulkout { - + /** A * Bulk export of documents in the index library to a file in JSON format at * @paramargs - */ - Public Static voidMain (string[] args) { - - Try { - //set the cluster name inSettings Settings = Settings.builder (). Put ("Cluster.name", "Elasticsearch"). Build (); - //Create client toTransportclient client =Newprebuilttransportclient (Settings) +. addtransportaddress (NewInetsockettransportaddress (Inetaddress.getbyname ("172.16.0.157"), 9300)); - theSearchResponse response = Client.preparesearch ("Flow") *. Settypes ("Data") $. SetSize (5000)Panax Notoginseng . Setquery (Querybuilders.matchallquery ()) - . Execute (). Actionget (); theSearchhits resulthits =response.gethits (); + AFile Article =NewFile ("C:/users/juyun/desktop/data.json"); theFileWriter FW =NewFileWriter (article); +BufferedWriter BFW =NewBufferedWriter (FW); - $ if(Resulthits.gethits (). length = = 0) { $System.out.println ("Find 0 data!")); - -}Else { the for(inti = 0; I < Resulthits.gethits (). length; i++) { -String Jsonstr =resulthits.gethits () [i]Wuyi . getsourceasstring (); the System.out.println (JSONSTR); - Bfw.write (JSONSTR); WuBfw.write ("\ n"); - } About } $ bfw.close (); - fw.close (); - -}Catch(unknownhostexception e) { A e.printstacktrace (); +}Catch(IOException e) { the e.printstacktrace (); - } $ } the the}
Elasticsearchbulkout
Elasticsearch5.0 Java Api (v)--Batch export index