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

本文介绍了一个使用Elasticsearch进行博文索引的实例,包括定义Blog类、将对象转换为JSON字符串、通过TransportClient连接Elasticsearch集群并创建索引等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先本文代码参考和引用了本博客的代码:

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")));  

参考:http://stackoverflow.com/questions/25912572/java-elasticsearch-none-of-the-configured-nodes-are-available

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Soyoger

听说打赏的都进了福布斯排行榜。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值