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]的更多相关文章

  1. ES添加文档 踩坑之 —— The number of object passed must be even but was [1]

    读取文件,获取json格式的数据doc, 然后使用 bulkRequestBuilder.add(client.prepareIndex(index, type, id).setSource(doc) ...

  2. ElasticSearch-The number of object passed must be even but was [1]-问题解决

    ES版本:6.4.3 1.The number of object passed must be even but was [1] 问题代码: IndexRequest indexRequest = ...

  3. Javascript中类型: undefined, number ,string ,object ,boolean

    var a1; var a2 = true;var a3 = 1;var a4 = "Hello";var a5 = new Object();var a6 = null;var ...

  4. [bug] Unrecognized token 'code': was expecting (JSON String, Number, Array, Object,'true', 'false' or 'null')

    JSON格式有误,需用JSON.stringify()函数转换一下 参考 https://www.cnblogs.com/sunyanblog/p/13788740.html https://www. ...

  5. elasticsearch-java异常

    1. Unsupported major.minor version 52.0 java的jdk版本过低导致,需要更换为jdk1.8+ 2. elasticsearch 的version在pom中提示 ...

  6. elasticsearch 导入基础数据并索引之 geo_point

    elasticsearch 中的地理信息存储, 有geo_point形式和geo_shape两种形式 此篇只叙述geo_point, 地理位置需要声明为特殊的类型, 不显示在mapping中定义的话, ...

  7. SAP NUMBER RANGE维护配置object FBN1 Deletion only possible if status is initial

    背景: 错误日志: SAP FBN1 Deletion only possible if status is initial 场景: 如果目标机已有NUMBER RANGE 不为0,需要删除配置年为9 ...

  8. Chapter 3 Discovering Classes and Object

    Chatper 3 Discovering Classes and Object Exercises: 1.What is a class? A class is a template for man ...

  9. Directive Definition Object

    不知道为什么这个我并没有想翻译过来的欲望,或许我并没有都看熟透,不好误人子弟,原版奉上. Here's an example directive declared with a Directive D ...

随机推荐

  1. Python学习-32.Python中os模块的一些方法

    首先肯定是要引入os模块了. import os getcwd方法: print(os.getcwd()) 上面的语句将会输出当前的工作目录,相当于C#中的Environment.CurrentDir ...

  2. LeetCode136:Single Number

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  3. 关于Java_Web连接Oracle数据库

    1.前提条件 1>装有Oracle数据库(因为连接的时候需要开启两项服务) 2>myeclipse或eclipse(支持WebProject的版本)开发环境,本机以myeclipse为例, ...

  4. SQL处理数据并发,解决ID自增

    1 创建MaxIdProcess表,由于存储ID的最大值 CREATE TABLE [dbo].[MaxIdProcess]( ,) NOT NULL, --自增ID ) NOT NULL, --存储 ...

  5. 对于equals和==的理解

    很多时候equals和==大家都分不太清楚怎么样来使用,今天小编就来教大家怎么使用 equals比较的是两个变量的值是否相等 而==则比较的是这个变量的内存地址是否相同 打个比方来说 String a ...

  6. SFML从入门到放弃(3) 视角和碰撞检测

    SFML从入门到放弃(3) 视角和碰撞检测 视角 window.draw();所画出的对象是在世界坐标的绝对位置. 视角可以选定在窗口中显示世界坐标下的的哪一个区域. sf::View init_vi ...

  7. 《Python绝技:运用Python成为顶级黑客》 Python实用小工具

    1.实现简单探测 使用socket模块,connect()方法建立与指定IP和端口的网络连接:revc(1024)方法将读取套接字中接下来的1024B数据 mport socket import sy ...

  8. jzoj5347

    tj:80pts:維護f[i][j]表示當前第i個方塊必須選,且選了j個的最優解,設w[i]為第i個方塊長度 則可以枚舉上次選了第k個方塊,則f[i][j]=max{f[k][j-1]+w[i]*(i ...

  9. 二叉查找树的实现——c++

    二叉查找树的c++实现: 1. 节点和二叉查找树的定义 1.1 二叉查找树节点 template <class T> class BSTNode{ public: T key; // 关键 ...

  10. 对数组排序三种方式sort、asort与ksort

    关联数组是 键值(key)=>值(value)形式. sort只对值进行排序,键值不参与排序: asort对值进行排序,键值参与排序: ksort对键值进行排序,值参与排序: 实例,比如数组: ...