ES怎樣讀取Json文件并添加索引,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
今天學習了下,ES添加索引:
添加方式 :1.讀取 JSON 文件 獲取相應的索引字段值
2.逐條讀取寫入ES創建索引。(注意:本例讀一條,寫一條效率非常低。因此可以改成批量寫入,大幅度提升效率)
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文件,得當相應的索引字段
* 2.調用 JsonUtil.objToJsonData 得當所以字段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數據文件 list
public static void readJsonFile(){
//List list = new ArrayList();
BufferedReader br;
try{
//創建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處理,成創建索引需要的字符串
String JsonString = JsonUtil.objToJsonData(teacherObject);
//創建索引
ElasticSearchIndex esi = new ElasticSearchIndex();
//得到ES連接 client
Client client = esi.getClient(HOST,PORT);
//創建索引
IndexResponse indexResponse = esi.doCreateIndexResponse( client,indexName ,type ,JsonString);
//System.out.println(indexResponse.getIndex());
//查詢索引
//GetResponse getResponse = esi.getIndexResponse(client);
//System.out.println(getResponse.getIndex());
//關閉ES連接
client.close();
}
//關閉 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;
}
//關閉client 連接
public void close(Client client) {
if (client != null) {
client.close();
}
}
//創建索引
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;
}
}看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。