ES怎样读取Json文件并添加索引
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,ES怎样读取Json文件并添加索引,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。今天学习了下,ES添加索引:添加方式 :
千家信息网最后更新 2025年12月02日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安全错误
数据库的锁怎样保障安全
南京宏贻科技软件开发有限公司
安徽学生网络技术开发技能
在数据库中查询系统
城市基础数据库建设的意义
全国著名网络安全干警
数据库管理系统是软件开发
lol服务器登不上去
数据库跨域导入数据
计算器软件开发考研
网络安全属于什么管理
公安机关dna数据库的作用
hb1200网络服务器
游戏服务器会被什么攻击
风筝守护连接服务器失败
环保软件开发的市场
山西放心软件开发设施参考价格
我的世界小服务器地址
福建电力软件开发
查询wbc088.com服务器
网络安全法数据分类
实现数据库安全
无法连接到服务器需要验证
邯郸专网络安全答案
小牛网络技术深圳有限公司
中企动力邮箱服务器
服务器怎么从机柜下架
共享店铺系统软件开发
云服务器云免
山东软件开发培训学校
数据库2008打开