The number of object passed must be even but was [1]
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]的更多相关文章
- ES添加文档 踩坑之 —— The number of object passed must be even but was [1]
读取文件,获取json格式的数据doc, 然后使用 bulkRequestBuilder.add(client.prepareIndex(index, type, id).setSource(doc) ...
- 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 = ...
- 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 ...
- [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. ...
- elasticsearch-java异常
1. Unsupported major.minor version 52.0 java的jdk版本过低导致,需要更换为jdk1.8+ 2. elasticsearch 的version在pom中提示 ...
- elasticsearch 导入基础数据并索引之 geo_point
elasticsearch 中的地理信息存储, 有geo_point形式和geo_shape两种形式 此篇只叙述geo_point, 地理位置需要声明为特殊的类型, 不显示在mapping中定义的话, ...
- 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 ...
- 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 ...
- Directive Definition Object
不知道为什么这个我并没有想翻译过来的欲望,或许我并没有都看熟透,不好误人子弟,原版奉上. Here's an example directive declared with a Directive D ...
随机推荐
- Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题
//php中的数组数据格式 Array ( [all] => Array ( [title] => ALL [room_promotion_id] => all ) [best_av ...
- 在MS单元测试中引发期望异常
首先准备一个引发异常的方法. public static void ThrowException() { throw new ArgumentException(); } 然后在单元测试项目中,写下测 ...
- 你好,Azure DevOps Server 2019;再见,Team Foundation Server
微软正式发布Azure DevOps Server 2019的第一个版本,作为Team Foundation Server (TFS)2018的升级版本和替代产品. 这是目前市面上唯一一款将产品名称冠 ...
- Git小技巧:VIM中如何填写注释信息
使用Git命令行工具的时候,经常一不小心就进入VIM界面,例如git commit没有填写任何描述信息.对于习惯了Windows可视化操作界面的用户,可能一下子会觉得无所适从,只能在键盘上一顿短按.下 ...
- oracle 11g Enterprise Manager配置失败
Enterprise Manager以下简称em,Database Configuration Assistant简称DBCA. 病症 监听程序未启动或数据库服务未注册到该监听程序.启动该监听程序并注 ...
- dorado7-HelloWorld
1.首先在Tomat中将 Auto reloding enable去掉,去掉的目的不用每次更改代码,都要重新部署 2.创建dorado视图文件 2.1 视图文件的格式为xml 2.2 在view中添加 ...
- C#不用union,而是有更好的方式实现
用过C/C++的人都知道有个union,特别好用,似乎char数组到short,int,float等的转换无所不能,也确实是能,并且用起来十分方便.那C#为什么没有这个关键字呢?怎么实现这个功能?其实 ...
- NetCore入门篇:(六)Net Core项目使用Controller之一
一.简介 1.当前最流行的开发模式是前后端分离,Controller作为后端的核心输出,是开发人员使用最多的技术点. 2.个人所在的团队已经选择完全抛弃传统mvc模式,使用html + webapi模 ...
- 第七章 ReentrantLock总结
常用方式: int a = 12; //注意:通常情况下,这个会设置成一个类变量,比如说Segement中的段锁与copyOnWriteArrayList中的全局锁 final ReentrantLo ...
- Flask从入门到精通之自定义错误界面
如果你在浏览器的地址栏中输入了不可用的路由,那么会显示一个状态码为404 的错误页面.现在这个错误页面太简陋.平庸,而且样式和使用了Bootstrap 的页面不一致. 像常规路由一样,Flask 允许 ...