ES怎样读取Json文件并添加索引,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联公司专注于企业全网营销推广、网站重做改版、白朗网站定制设计、自适应品牌网站建设、H5建站、商城开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为白朗等各大城市提供网站开发制作服务。
今天学习了下,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; } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。