首先本文代码参考和引用了本博客的代码:
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 getter
public 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 startup
Map<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")));