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. [php] try - catch exceptiong handler

    //http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warningOne possibility is to set your ...

  2. Create Your Content and Structure

    The original page source Content is the most important aspect of any site. So let's design for the c ...

  3. [C#]SharpSSH-一个可以使用SSH连接的.NET库

    A Secure Shell (SSH) library for .NET 觉得有用,就记录下来了 http://www.tamirgal.com/blog/page/SharpSSH.aspx ht ...

  4. laravel字段自增/自减

    DB::table('users')->increment('votes');DB::table('users')->increment('votes', 5);DB::table('us ...

  5. Emmet常用语法

    Emmet常用语法1.输入!和html:5(不能大写),按下TAB 键,快速生成一个 HTML5 的标准文档初始结构. html:xt 生成 HTML4 过渡型 html:4s 生成 HTML4 严格 ...

  6. 项目笔记---事半功倍之GhostDoc(二)

    前言 前一篇文章<项目笔记---事半功倍之StyleCop(一)>提到如何约束代码,规范代码风格,这一节,我们将了解如何快速生成符合规则的代码注释---GhostDoc 一.安装Ghost ...

  7. 运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决

    运行spark官方的graphx 示例 ComprehensiveExample.scala报错解决 在Idea中,直接运行ComprehensiveExample.scala,报需要指定master ...

  8. 【加密算法】AES

    一.简介 AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高. 用AES加密2000年10月,NIST(美国国家标准和技术协会 ...

  9. Flask系列03--Flask的路由 app.route中的参数, 动态参数路由

    Flask–路由 添加路由的两种方式 第一种 @app.route("/my_de") def detail() 第二种(了解即可) app.add_url_rule(" ...

  10. Exp3 免杀原理与实践 20164321 王君陶

    Exp3 免杀原理与实践 20164321 王君陶 1实验要求 1.1 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5分),veil-evasion(0.5分), ...