ELK研究(一):elasticsearch java api介面操作ES叢集 ---TransportClient的使用介紹 bulk批量提交資料

後開啟撒打發了發表於2017-11-09

Java client操作ES: 1:配置叢集物件資訊;2:建立客戶端;3:檢視叢集資訊
1、設定叢集名字
預設叢集名為elasticsearch,如果叢集名稱和指定的不一致則在使用節點資源時會報錯。

Settings settings = Settings.builder()
        .put("cluster.name", "myClusterName").build();
TransportClient client = new PreBuiltTransportClient(settings);
//Add transport addresses and do something with the client...

2、嗅探功能
通過client.transport.sniff啟動嗅探功能,這樣只需要指定叢集中的某一個節點(不一定是主節點),然後會載入叢集中的其他節點,這樣只要程式不停即使此節點當機仍然可以連線到其他節點。

Settings settings = Settings.builder()
        .put("client.transport.sniff", true).build();
TransportClient client = new PreBuiltTransportClient(settings);

3、建立client

// on startup

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));

// on shutdown

client.close();

4、完整示例–elasticsearch java api bulk批量提交資料
json資料儲存在文中,程式碼是通過讀檔案的方式:
{“10190”:0,”10071”:0,”10191”:0,”10070”:0,”48”:”122186”,”type”:”122186”,”10018”:0,”10117”:0,”332”:0,”333”:0,”999”:3202,”10178”:10131,”8503”:0,”8504”:0,”10135”:”SUSP”,”31”:0,”10116”:0,”35”:”UA3202”,”10185”:0,”10184”:0,”140”:100000,”387”:0,”8538”:”P010”,”10044”:0,”10187”:0,”10043”:0,”10186”:0,”10189”:0,”10068”:[],”10188”:0,”10202”:-1,”10146”:1,”10069”:[],”10204”:0,”10203”:-1}
{“10190”:0,”10071”:0,”10191”:0,”10070”:0,”48”:”122186”,”type”:”122186”,”10018”:0,”10117”:0,”332”:0,”333”:0,”999”:3202,”10178”:10131,”8503”:0,”8504”:0,”10135”:”SUSP”,”31”:0,”10116”:0,”35”:”UA3202”,”10185”:0,”10184”:0,”140”:100000,”387”:0,”8538”:”P010”,”10044”:0,”10187”:0,”10043”:0,”10186”:0,”10189”:0,”10068”:[],”10188”:0,”10202”:-1,”10146”:1,”10069”:[],”10204”:0,”10203”:-1}
{“10190”:0,”10071”:0,”10191”:0,”10070”:0,”48”:”122186”,”type”:”122186”,”10018”:0,”10117”:0,”332”:0,”333”:0,”999”:3202,”10178”:10131,”8503”:0,”8504”:0,”10135”:”SUSP”,”31”:0,”10116”:0,”35”:”UA3202”,”10185”:0,”10184”:0,”140”:100000,”387”:0,”8538”:”P010”,”10044”:0,”10187”:0,”10043”:0,”10186”:0,”10189”:0,”10068”:[],”10188”:0,”10202”:-1,”10146”:1,”10069”:[],”10204”:0,”10203”:-1}

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;                                                    
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import static org.elasticsearch.common.xcontent.XContentFactory.*;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;



public class Main {

    public static void main(String[] args) {

        try {

            //通過setting來制定叢集資訊,單機就不需要制定了叢集資訊了,去掉即可
            Settings settings = Settings.builder().
            put("cluster.name", "elk_test.cluster")
            .put("client.transport.sniff",true).build();

            //建立客戶端clients
            InetAddress address = InetAddress.getByName("192.168.0.153"); // ip
            InetSocketAddress socketAddress=new InetSocketAddress(address, 9300);  //socket address = ip + port
            TransportAddress transportAddress = new InetSocketTransportAddress(socketAddress);
            TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress);

            File file = new File("E:/json");
            FileReader reader=new FileReader(file);
            BufferedReader bfr=new BufferedReader(reader);
            String line;
            BulkRequestBuilder bulkRequest=client.prepareBulk();
            int count=0;

            while((line=bfr.readLine())!=null){

               bulkRequest.add(client.prepareIndex("dddddddd","article").setSource(line));
                if (count%10==0) {
                    bulkRequest.execute().actionGet();
                    bulkRequest=client.prepareBulk();  
                }
                count++;
                //System.out.println(line);
            }
            bulkRequest.execute().actionGet();

            bfr.close();
            reader.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

相關文章