In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
ES how to read Json files and add indexes, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Today, we learned that ES adds an index:
Add method: 1. Read the JSON file to get the corresponding index field value
two。 Read and write ES one by one to create an index. (note: in this example, it is very inefficient to read one and write one. Therefore, it can be changed to batch writing, which greatly improves the efficiency.
3. Program entry 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. Deal with the json file one by one, appropriate to the corresponding index field * 2. Call JsonUtil.objToJsonData properly so that the fields json string * / public class DataFactory {private static final String indexName = "index name"; private static final String type = "index type main"; private static final String HOST = "ES cluster IP"; private static final int PORT = ES port; / / read json data file list public static void readJsonFile () {/ / List list = new ArrayList (); BufferedReader br Try {/ / create 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) / / read a row to process a row (to get the required index fields) 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 () / / give it to JsonUtil to create the string String JsonString = JsonUtil.objToJsonData (teacherObject) needed to create the index; / / create the index ElasticSearchIndex esi = new ElasticSearchIndex (); / / get the ES connection client Client client = esi.getClient (HOST,PORT) / / create an index IndexResponse indexResponse = esi.doCreateIndexResponse (client,indexName, type, JsonString); / / System.out.println (indexResponse.getIndex ()); / / query index / / GetResponse getResponse = esi.getIndexResponse (client); / / System.out.println (getResponse.getIndex ()) / / close ES connection client.close ();} / / 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 {/ / obtain client connection 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 } / / close client connection public void close (Client client) {if (client! = null) {client.close ();}} / / create index 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;} / / query index public SearchResponse doSerch (Client client,String indexName,String type) {SearchResponse response = client.prepareSearch (indexName) .setTypes (type). Execute (). ActionGet (); return response } / / Delete index}
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. * used to generate a new JSON string * / 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; / / initialize assignment 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 it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.