Bulk import of data from your computer's local. JSON format file into the index library
1 Packagecom.juyun.test;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.File;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileReader;7 Importjava.io.IOException;8 Importjava.net.InetAddress;9 Importjava.net.UnknownHostException;Ten Importjava.util.ArrayList; One Importjava.util.List; A - ImportOrg.elasticsearch.action.bulk.BulkRequestBuilder; - Importorg.elasticsearch.client.Client; the Importorg.elasticsearch.client.transport.TransportClient; - Importorg.elasticsearch.common.settings.Settings; - Importorg.elasticsearch.common.transport.InetSocketTransportAddress; - Importorg.elasticsearch.transport.client.PreBuiltTransportClient; + - Public classElasticsearchbulkin { + A Private Staticclient client; at - /** - * Bulk import of data from external files - * @paramargs - */ - Public Static voidMain (string[] args) { in - Try { to + //set the cluster name -Settings Settings = Settings.builder (). Put ("Cluster.name", "Elasticsearch"). Build (); the //Create client *Client =Newprebuilttransportclient (Settings) $. addtransportaddress (NewInetsockettransportaddress (Inetaddress.getbyname ("172.16.0.157"), 9300));Panax Notoginseng - //firstthread ft=new firstthread ();//Create a Firstthread object the //Ft.start (); + AFile Article =NewFile ("C:/users/juyun/desktop/al.json"); theFileReader fr=NewFileReader (article); +BufferedReader bfr=NewBufferedReader (FR); -String line=NULL; $Bulkrequestbuilder bulkrequest=Client.preparebulk (); $ intCount=0; - /*While ((Line=bfr.readline ())!=null) { - Bulkrequest.add (Client.prepareindex ("NewIndex", "Newhttp"). SetSource (line));//Progressive write the count++; - System.out.println (count);Wuyi Bulkrequest.execute (). Actionget (); the }*/ - Bulkrequest.execute (). Actionget (); Wu bfr.close (); - fr.close (); About}Catch(unknownhostexception e) { $ e.printstacktrace (); -}Catch(FileNotFoundException e) { - e.printstacktrace (); -}Catch(IOException e) { A e.printstacktrace (); + } the } - $ the classFirstthreadextendsthread{ the @Override the Public voidrun () { the Try{ -File Article =NewFile ("C:/users/juyun/desktop/al.json"); inFileReader fr=NewFileReader (article); theBufferedReader bfr=NewBufferedReader (FR); theString line=NULL; AboutBulkrequestbuilder bulkrequest=Client.preparebulk (); the intCount=0; the while((Line=bfr.readline ())! =NULL){ theBulkrequest.add (Client.prepareindex ("NewIndex", "Newhttp"). SetSource (line));//Line-wise write +count++; - System.out.println (count); the Bulkrequest.execute (). Actionget ();Bayi } the Bulkrequest.execute (). Actionget (); the bfr.close (); - fr.close (); -}Catch(Exception e) { the } the } the } the}Elasticsearchbulkin
Elasticsearch5.0 Java Api (vi)--Bulk Import Index