Elasticsearch5.0 Java Api (vi)--Bulk Import Index

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.