ES怎樣讀取Json文件并添加索引,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、白朗網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為白朗等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
今天學(xué)習(xí)了下,ES添加索引:
添加方式 :1.讀取 JSON 文件 獲取相應(yīng)的索引字段值
2.逐條讀取寫(xiě)入ES創(chuàng)建索引。(注意:本例讀一條,寫(xiě)一條效率非常低。因此可以改成批量寫(xiě)入,大幅度提升效率)
3.程序入口 DataFactory
package com.esindex; import com.esindex.CrmTeacherObject; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.json.JSONObject; import java.io.*; import java.util.ArrayList; import java.util.List; /** * Created by bin.zhang on 2017/4/14. * 1.逐條處理 json文件,得當(dāng)相應(yīng)的索引字段 * 2.調(diào)用 JsonUtil.objToJsonData 得當(dāng)所以字段json字符串 */ public class DataFactory { private static final String indexName = "索引名"; private static final String type = "索引類型main"; private static final String HOST = "ES集群IP"; private static final int PORT = ES端口; //讀取json數(shù)據(jù)文件 list public static void readJsonFile(){ //List list = new ArrayList(); BufferedReader br; try{ //創(chuàng)建BufferedReader(FileReader) //br = new BufferedReader(new FileReader("D:/Work_Space/es-index/src/test.txt")); String line = null; while ((line=br.readLine())!= null ){ JSONObject dataJson = new JSONObject(line); //讀一行處理一行 (獲得需要的索引字段) CrmTeacherObject teacherObject = new CrmTeacherObject( dataJson.get("teacherId")==JSONObject.NULL ? 0:dataJson.getLong("teacherId"), dataJson.get("realName") == JSONObject.NULL ? "":dataJson.getString("realName"), dataJson.get("mobile") == JSONObject.NULL ? "":dataJson.get("mobile").toString() ); //交給JsonUtil處理,成創(chuàng)建索引需要的字符串 String JsonString = JsonUtil.objToJsonData(teacherObject); //創(chuàng)建索引 ElasticSearchIndex esi = new ElasticSearchIndex(); //得到ES連接 client Client client = esi.getClient(HOST,PORT); //創(chuàng)建索引 IndexResponse indexResponse = esi.doCreateIndexResponse( client,indexName ,type ,JsonString); //System.out.println(indexResponse.getIndex()); //查詢索引 //GetResponse getResponse = esi.getIndexResponse(client); //System.out.println(getResponse.getIndex()); //關(guān)閉ES連接 client.close(); } //關(guān)閉 BufferedReader br.close(); }catch (IOException e){ e.printStackTrace(); } } public static void main(String[] args) { readJsonFile(); } }
package com.esindex; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; /** * Created by Administrator on 2017/4/14. */ public class ElasticSearchIndex { //獲得client 連接 public Client getClient(String host,int port ){ Client client = null; Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.ping_timeout", "10s") .put("client.transport.ignore_cluster_name", true) .put("node.client", true) .put("client.transport.sniff", true).build(); client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(host, port)); return client; } //關(guān)閉client 連接 public void close(Client client) { if (client != null) { client.close(); } } //創(chuàng)建索引 public IndexResponse doCreateIndexResponse(Client client,String indexName, String type, String json) { //Client client = getClient(HOST,PORT); IndexResponse response = client.prepareIndex(indexName, type) .setSource(json) .execute() .actionGet(); return response; } //查詢索引 public SearchResponse doSerch(Client client,String indexName,String type){ SearchResponse response = client.prepareSearch(indexName).setTypes(type).execute().actionGet(); return response; } //刪除索引 }
package com.esindex; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; /** * Created by bin.zhang on 2017/4/14. * 用于生成新的JSON字符串 */ public class JsonUtil { public static String objToJsonData(CrmTeacherObject crmTeacherObject){ String jsonData=null; try{ XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); jsonBuilder.startObject() .field("teacherId",crmTeacherObject.getTeacherId()) .field("realName",crmTeacherObject.getRealName()) .field("mobile",crmTeacherObject.getMobile()) .endObject(); jsonData = jsonBuilder.string(); }catch (IOException e){ e.printStackTrace(); } return jsonData; } }
package com.esindex; /** * Created by bin.zhang on 2017/4/14. */ public class CrmTeacherObject { private Long teacherId; private String realName; private String mobile; //初始化賦值 public CrmTeacherObject(Long teacherId,String realName,String mobile){ this.teacherId = teacherId; this.realName = realName; this.mobile=mobile; } public Long getTeacherId() { return teacherId; } public void setTeacherId(Long teacherId) { this.teacherId = teacherId; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
當(dāng)前名稱:ES怎樣讀取Json文件并添加索引
本文鏈接:http://muchs.cn/article8/ghedop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、企業(yè)網(wǎng)站制作、App開(kāi)發(fā)、網(wǎng)站導(dǎo)航、定制開(kāi)發(fā)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)