日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch2.3.2创建索引java代码及异常处理

發布時間:2024/1/23 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch2.3.2创建索引java代码及异常处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先本文代碼參考和引用了本博客的代碼:

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最主要的區別..."));String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介紹與簡單使用", "2016-06-19", "學習目標 掌握泛型的產生意義..."));String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基礎", "2016-06-19", "Hibernate框架基礎..."));String data5 = JsonUtil.model2Json(new Blog(5, "Shell基本知識", "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 {/* 創建客戶端 */// 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("創建成功!");}}client.close();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
在實際運行過程中,出現如下錯誤:

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的名字,如果沒有修改端口號,則客戶端的端口號為9300。

再次運行問題解決。

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

總結

以上是生活随笔為你收集整理的Elasticsearch2.3.2创建索引java代码及异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。