ES讀取Json檔案新增索引
今天學習了下,ES新增索引:
新增方式 :1.讀取 JSON 檔案 獲取相應的索引欄位值
2.逐條讀取寫入ES建立索引。(注意:本例讀一條,寫一條效率非常低。因此可以改成批次寫入,大幅度提升效率)
3.程式入口 DataFactory
新增方式 :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;
- }
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28929558/viewspace-2137334/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JQuery讀取本地json檔案jQueryJSON
- solr索引庫新增新的索引,使用json檔案或者xml檔案的資料Solr索引JSONXML
- Java讀取Json檔案工具類JavaJSON
- C#讀取Json配置檔案C#JSON
- 索引器的妙用,讀取配置檔案索引
- 如何讀取和寫入JSON檔案JSON
- C#讀取指定json配置檔案C#JSON
- operties檔案的讀取、新增、修改、清空、另存
- shell讀取構建檔案資訊生成json字串JSON字串
- jquery簡單ajax示例_讀取json檔案資料jQueryJSON
- Solr json,xml等檔案資料匯入(新增索引)linux下操作SolrJSONXML索引Linux
- Json.NET實現json的讀取,新增,刪除,修改JSON
- Python中Spark讀取parquet檔案並獲取schema的JSON表示PythonSparkJSON
- Java 讀取檔案Java
- tiff檔案讀取
- 任意檔案讀取
- Asp .Net Core 讀取appsettings.json配置檔案APPJSON
- python讀取檔案——python讀取和儲存mat檔案Python
- viper 讀取配置檔案
- go配置檔案讀取Go
- iOS讀取.csv檔案iOS
- php 讀取超大檔案PHP
- JAVA 讀取xml檔案JavaXML
- WinForm讀取Excel檔案ORMExcel
- java讀取properties檔案Java
- 前端讀取excel檔案前端Excel
- 用友任意檔案讀取
- IOC - 讀取配置檔案
- 【node】如何在ES modules中匯入JSON檔案JSON
- 檔案快取(配合JSON陣列)快取JSON陣列
- [python] 資料夾所有檔案讀取,正則化,json使用PythonJSON
- .NET Core中如何讀取appsetting.json配置檔案APPJSON
- 讀取.net core配置檔案appsetting.json內容APPJSON
- VB讀取文字檔案的例子:逐行讀取
- ElasticSearch 獲取es資訊以及索引操作Elasticsearch索引
- Asp.Net Core 3.1學習-讀取、監聽json配置檔案(7)ASP.NETJSON
- cocos讀取plist檔案
- go–讀取檔案的方式Go