ES怎样读取Json文件并添加索引
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,ES怎样读取Json文件并添加索引,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。今天学习了下,ES添加索引:添加方式 :
千家信息网最后更新 2026年11月01日ES怎样读取Json文件并添加索引
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; }}看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
索引
文件
字段
字符
字符串
处理
得当
一行
效率
逐条
学习
帮助
查询
清楚
入口
内容
大幅度
对此
数据
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
耶加雪菲
互联网科技有限公司经营范围
港股互联网科技股
中资互联网科技
科技与互联网发展
互联网与新科技
嘟嘟瓜互联网科技
亿账通互联网科技
上游互联网科技
互联网科技产品
中国的互联网科技企业的崛起
中国航天科技大学国际工业互联网
恒生互联网科技指数跳水
合肥互联网科技公司公章图片
富国互联网科技002692
上海同邻互联网科技有限公司规模
互联网公司属于科技吗
青岛易易互联网科技
互联网科技大会大佬
中国主要互联网科技公司
云南咖啡
广州善班互联网科技
互联网科技公司做什么
镁科技互联网资讯平台
北京三清互联网科技
深圳市优智互联网科技有限公司
互联网相关科技技术
互联网与数字科技关系
中国互联网公司的科技投入
唐山双旺合互联网科技产业园