Elasticsearch2.3.2创建索引java代码及异常处理
生活随笔
收集整理的這篇文章主要介紹了
Elasticsearch2.3.2创建索引java代码及异常处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先本文代碼參考和引用了本博客的代碼:
http://www.cnblogs.com/coderdxj/p/6856145.html
package com.test.entity; public class Blog {private Integer id;private String title;private String posttime;private String content;public Blog() {}public Blog(Integer id, String title, String posttime, String content) {this.id = id;this.title = title;this.posttime = posttime;this.content = content;}//setter and getterpublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getPosttime() {return posttime;}public void setPosttime(String posttime) {this.posttime = posttime;}public String getContent() {return content;}public void setContent(String content) {this.content = content;} }package com.test.entity;import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory;import java.io.IOException;public class JsonUtil {public static String model2Json(Blog blog) {String jsonData = null;try {XContentBuilder jsonBuild = XContentFactory.jsonBuilder();jsonBuild.startObject().field("id", blog.getId()).field("title", blog.getTitle()).field("posttime", blog.getPosttime()).field("content",blog.getContent()).endObject();jsonData = jsonBuild.string();//System.out.println(jsonData);} catch (IOException e) {e.printStackTrace();}return jsonData;}}
package com.test.entity;import java.util.ArrayList; import java.util.List;public class DataFactory {public static DataFactory dataFactory = new DataFactory();private DataFactory() {}public DataFactory getInstance() {return dataFactory;}public static List<String> getInitJsonData() {List<String> list = new ArrayList<String>();String data1 = JsonUtil.model2Json(new Blog(1, "git簡介", "2016-06-19", "SVN與Git最主要的區(qū)別..."));String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介紹與簡單使用", "2016-06-19", "學(xué)習(xí)目標(biāo) 掌握泛型的產(chǎn)生意義..."));String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基礎(chǔ)", "2016-06-19", "Hibernate框架基礎(chǔ)..."));String data5 = JsonUtil.model2Json(new Blog(5, "Shell基本知識(shí)", "2016-06-19", "Shell是什么..."));list.add(data1);list.add(data2);list.add(data3);list.add(data4);list.add(data5);return list;}} package com.test.entity;import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress;import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashMap; import java.util.List; import java.util.Map;public class ElasticSearchHandler {public static void main(String[] args) {try {/* 創(chuàng)建客戶端 */// client startupMap<String, String> map = new HashMap();map.put("cluster.name", "application");Settings.Builder settings = Settings.builder().put(map);Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300));List<String> jsonData = DataFactory.getInitJsonData();for (int i = 0; i < jsonData.size(); i++) {IndexResponse response = client.prepareIndex("blog", "article").setSource(jsonData.get(i)).get();if (response.isCreated()) {System.out.println("創(chuàng)建成功!");}}client.close();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
在實(shí)際運(yùn)行過程中,出現(xiàn)如下錯(cuò)誤:
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]]
解決:如果你配置了es集群的cluster.name,則需要在settings中添加cluster.name的名字,如果沒有修改端口號(hào),則客戶端的端口號(hào)為9300。
再次運(yùn)行問題解決。
Map<String, String> map = new HashMap(); map.put("cluster.name", "es_test"); Settings.Builder settings = Settings.builder().put(map); Client client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("localhost"), Integer.parseInt("9300")));參考:http://stackoverflow.com/questions/25912572/java-elasticsearch-none-of-the-configured-nodes-are-available
總結(jié)
以上是生活随笔為你收集整理的Elasticsearch2.3.2创建索引java代码及异常处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv安装与python cv2安
- 下一篇: 大数据架构中使用JSON-RPC好,还是