Elasticsearch版本客户端与服务端版本不一致问题

初学ES,总是遇到一些小问题。今天遇到的问题是,在java中pom.xml导入了

<dependency>
  <groupId>org.elasticsearchgroupId>
  <artifactId>elasticsearchartifactId>
  <version>2.3.3version>
dependency>

的jar包
在创建client连接时参考api写的代码如下:

Client client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

运行时一直卡住,不报错也连接不成功。
经过排查,是因为我电脑上安装的Elasticsearch是1.7版本的。2.3版本和1.7版本里面的API不太一样,高版本的客户端不能调用低版本的服务端。后来我把pom.xml的引用改为:

<dependency>
     <groupId>org.elasticsearchgroupId>
     <artifactId>elasticsearchartifactId>
     <version>1.7.1version>
dependency>

就可以运行了。
下面是1.7版本的代码:

/**
 * 
 */
package com.odianyun.steel.test.es;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
/**
 * @author diaoxingguo
 *
 */
public class Test {

    public static void main(String[] args) {

        //创建客户端
        try {
            Client client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));

            System.out.println("连接成功");
            List list = getData();
            for(String str : list){
                IndexResponse response = client.prepareIndex("blog","article").setSource(str).get();
                if(response.isCreated()){
                    System.out.println("创建成功!");
                }
            }

            client.close();

        } catch (Exception e) {
            e.printStackTrace();
        }




    }


    public static List getData(){
        List list = new ArrayList();

        String blog1 = model2Json(new Blog(1,"第1个title","2016-10-14 10:31","第1个content"));
        String blog2 = model2Json(new Blog(2,"第2个title","2016-10-14 10:32","第2一个content"));
        String blog3 = model2Json(new Blog(3,"第3个title","2016-10-14 10:33","第3个content"));
        String blog4 = model2Json(new Blog(4,"第4个title","2016-10-14 10:34","第4个content"));
        String blog5 = model2Json(new Blog(5,"第5个title","2016-10-14 10:35","第5个content"));
        String blog6 = model2Json(new Blog(6,"第6个title","2016-10-14 10:36","6个content"));
        String blog7 = model2Json(new Blog(7,"第7个title","2016-10-14 10:37","第7个content"));
        String blog8 = model2Json(new Blog(8,"第8个title","2016-10-14 10:36","8个content"));

        list.add(blog1);
        list.add(blog2);
        list.add(blog3);
        list.add(blog4);
        list.add(blog5);
        list.add(blog6);
        list.add(blog7);
        list.add(blog8);

        return list;
    }

     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;
        }
}

你可能感兴趣的:(elasticsearch)