ES讀取Json檔案新增索引

呆呆笨笨的魚發表於2017-04-15
今天學習了下,ES新增索引:
         新增方式 :1.讀取 JSON 檔案 獲取相應的索引欄位值
                         2.逐條讀取寫入ES建立索引。(注意:本例讀一條,寫一條效率非常低。因此可以改成批次寫入,大幅度提升效率)
                         3.程式入口 DataFactory


  1. package com.esindex;

  2. import com.esindex.CrmTeacherObject;
  3. import org.elasticsearch.action.index.IndexResponse;
  4. import org.elasticsearch.client.Client;
  5. import org.json.JSONObject;

  6. import java.io.*;
  7. import java.util.ArrayList;
  8. import java.util.List;

  9. /**
  10.  * Created by bin.zhang on 2017/4/14.
  11.  * 1.逐條處理 json檔案,得當相應的索引欄位
  12.  * 2.呼叫 JsonUtil.objToJsonData 得當所以欄位json字串
  13.  */
  14. public class DataFactory {
  15.     private static final String indexName = "索引名";
  16.     private static final String type = "索引型別main";
  17.     private static final String HOST = "ES叢集IP";
  18.     private static final int PORT = ES埠;

  19.     //讀取json資料檔案 list
  20.     public static void readJsonFile(){
  21.         //List list = new ArrayList();
  22.         BufferedReader br;
  23.         try{
  24.             //建立BufferedReader(FileReader)
  25.             //br = new BufferedReader(new FileReader("D:/Work_Space/es-index/src/test.txt"));
  26.             String line = null;
  27.             while ((line=br.readLine())!= null ){
  28.                 JSONObject dataJson = new JSONObject(line);
  29.                 //讀一行處理一行 (獲得需要的索引欄位)
  30.                 CrmTeacherObject teacherObject = new CrmTeacherObject(
  31.                         dataJson.get("teacherId")==JSONObject.NULL ? 0:dataJson.getLong("teacherId"),
  32.                         dataJson.get("realName") == JSONObject.NULL ? "":dataJson.getString("realName"),
  33.                         dataJson.get("mobile") == JSONObject.NULL ? "":dataJson.get("mobile").toString()
  34.                         );
  35.                 //交給JsonUtil處理,成建立索引需要的字串
  36.                 String JsonString = JsonUtil.objToJsonData(teacherObject);
  37.                 //建立索引
  38.                 ElasticSearchIndex esi = new ElasticSearchIndex();
  39.                 //得到ES連線 client
  40.                 Client client = esi.getClient(HOST,PORT);
  41.                 //建立索引
  42.                 IndexResponse indexResponse = esi.doCreateIndexResponse( client,indexName ,type ,JsonString);
  43.                 //System.out.println(indexResponse.getIndex());

  44.                 //查詢索引
  45.                 //GetResponse getResponse = esi.getIndexResponse(client);
  46.                 //System.out.println(getResponse.getIndex());

  47.                 //關閉ES連線
  48.                 client.close();
  49.             }
  50.             //關閉 BufferedReader
  51.             br.close();
  52.         }catch (IOException e){
  53.             e.printStackTrace();
  54.         }
  55.     }


  56.     public static void main(String[] args) {
  57.         readJsonFile();
  58.     }
  59. }



  1. package com.esindex;

  2. import org.elasticsearch.action.get.GetResponse;
  3. import org.elasticsearch.action.index.IndexResponse;
  4. import org.elasticsearch.action.search.SearchResponse;
  5. import org.elasticsearch.client.Client;
  6. import org.elasticsearch.client.transport.TransportClient;
  7. import org.elasticsearch.common.settings.ImmutableSettings;
  8. import org.elasticsearch.common.settings.Settings;
  9. import org.elasticsearch.common.transport.InetSocketTransportAddress;

  10. /**
  11.  * Created by Administrator on 2017/4/14.
  12.  */
  13. public class ElasticSearchIndex {
  14.     //獲得client 連線
  15.     public Client getClient(String host,int port ){
  16.         Client client = null;
  17.         Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.ping_timeout", "10s")
  18.                 .put("client.transport.ignore_cluster_name", true)
  19.                 .put("node.client", true)
  20.                 .put("client.transport.sniff", true).build();
  21.         client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(host, port));
  22.         return client;
  23.     }

  24.     //關閉client 連線
  25.     public void close(Client client) {
  26.         if (client != null) {
  27.             client.close();
  28.         }
  29.     }

  30.     //建立索引
  31.     public IndexResponse doCreateIndexResponse(Client client,String indexName, String type, String json) {
  32.         //Client client = getClient(HOST,PORT);
  33.         IndexResponse response = client.prepareIndex(indexName, type)
  34.                 .setSource(json)
  35.                 .execute()
  36.                 .actionGet();
  37.         return response;
  38.     }


  39.     //查詢索引
  40.     public SearchResponse doSerch(Client client,String indexName,String type){
  41.         SearchResponse response = client.prepareSearch(indexName).setTypes(type).execute().actionGet();
  42.         return response;
  43.     }


  44.     //刪除索引

  45. }


  1. package com.esindex;
  2. import org.elasticsearch.common.xcontent.XContentBuilder;
  3. import org.elasticsearch.common.xcontent.XContentFactory;

  4. import java.io.IOException;

  5. /**
  6.  * Created by bin.zhang on 2017/4/14.
  7.  * 用於生成新的JSON字串
  8.  */
  9. public class JsonUtil {

  10.     public static String objToJsonData(CrmTeacherObject crmTeacherObject){
  11.         String jsonData=null;
  12.         try{
  13.             XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();

  14.             jsonBuilder.startObject()
  15.                     .field("teacherId",crmTeacherObject.getTeacherId())
  16.                     .field("realName",crmTeacherObject.getRealName())
  17.                     .field("mobile",crmTeacherObject.getMobile())
  18.                     .endObject();
  19.             jsonData = jsonBuilder.string();
  20.         }catch (IOException e){
  21.             e.printStackTrace();
  22.         }
  23.         return jsonData;
  24.     }
  25. }


  1. package com.esindex;

  2. /**
  3.  * Created by bin.zhang on 2017/4/14.
  4.  */
  5. public class CrmTeacherObject {
  6.     private Long teacherId;
  7.     private String realName;
  8.     private String mobile;

  9.     //初始化賦值
  10.     public CrmTeacherObject(Long teacherId,String realName,String mobile){
  11.         this.teacherId = teacherId;
  12.         this.realName = realName;
  13.         this.mobile=mobile;
  14.     }
  15.     public Long getTeacherId() {
  16.         return teacherId;
  17.     }
  18.     public void setTeacherId(Long teacherId) {
  19.         this.teacherId = teacherId;
  20.     }
  21.     public String getRealName() {
  22.         return realName;
  23.     }
  24.     public void setRealName(String realName) {
  25.         this.realName = realName;
  26.     }
  27.     public String getMobile() {
  28.         return mobile;
  29.     }
  30.     public void setMobile(String mobile) {
  31.         this.mobile = mobile;
  32.     }
  33. }



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28929558/viewspace-2137334/,如需轉載,請註明出處,否則將追究法律責任。

相關文章