码迷,mamicode.com
首页 > 其他好文 > 详细

The number of object passed must be even but was [1]

时间:2018-11-19 20:25:18      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:on()   string   structs   cep   out   void   sim   turn   int   

1.失败,使用TransportClient

    public static void bulkInsert(TransportClient client) throws IOException {
        List<Person> ps=new ArrayList<Person>();
        Person one=new Person();
        one.setId("1");
        one.setAddress("add");
        one.setMobile("1111");
        one.setSex("f");
        one.setUsername("www");
        ps.add(one);
        
        Person two=new Person();
        two.setId("2");
        two.setAddress("add");
        two.setMobile("2222");
        two.setSex("f");
        two.setUsername("www");
        ps.add(two);
        
        Persons pss=new Persons();
        pss.setPersons(ps);
        pss.setUuid("uiid");
        
        String jsonStr=JSON.toJSONString(pss);
        System.out.println(jsonStr);

        IndexResponse response = client.prepareIndex("www", "www").setSource(jsonStr).get();
        System.out.println("创建成功!");

    }

原因是:setSource方法不支持json,源码如下:

    /**
     * Constructs a simple document with a field name and value pairs.
     * <p>
     * <b>Note: the number of objects passed to this method must be an even
     * number. Also the first argument in each pair (the field name) must have a
     * valid String representation.</b>
     * </p>
     */
    public IndexRequestBuilder setSource(Object... source) {
        request.source(source);
        return this;
    }

2.成功,使用RestHighLevelClient 可以直接使用json

    public static void InsertByJson(RestHighLevelClient client) throws IOException {
        List<Person> ps=new ArrayList<Person>();
        Person one=new Person();
        one.setId("1");
        one.setAddress("add");
        one.setMobile("1111");
        one.setSex("f");
        one.setUsername("www");
        ps.add(one);
        
        Person two=new Person();
        two.setId("2");
        two.setAddress("add");
        two.setMobile("2222");
        two.setSex("f");
        two.setUsername("www");
        ps.add(two);
        
        Persons pss=new Persons();
        pss.setPersons(ps);
        pss.setUuid("uiid");
        
        String jsonStr=JSON.toJSONString(pss);
        System.out.println(jsonStr);
        IndexRequest req = new IndexRequest("www", "www");    
        req.source(jsonStr, XContentType.JSON);
        IndexResponse response = client.index(req);
        System.out.println("创建成功!");

    }

 

The number of object passed must be even but was [1]

标签:on()   string   structs   cep   out   void   sim   turn   int   

原文地址:https://www.cnblogs.com/davidwang456/p/9984516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!