com.datastax.driver.core.querybuilder.Insert.json()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(90)

本文整理了Java中com.datastax.driver.core.querybuilder.Insert.json()方法的一些代码示例,展示了Insert.json()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Insert.json()方法的具体详情如下:
包路径:com.datastax.driver.core.querybuilder.Insert
类名称:Insert
方法名:json

Insert.json介绍

[英]Inserts the provided object, using the INSERT INTO ... JSON syntax introduced in Cassandra 2.2.

With INSERT statements, the new JSON keyword can be used to enable inserting a JSON structure as a single row.

The provided object can be of the following types:

  1. A raw string. In this case, it will be appended to the query string as is. It should NOT be surrounded by single quotes. Its format should generally match that returned by a SELECT JSON statement on the same table. Note that it is not possible to insert function calls nor bind markers in a JSON string.
  2. A QueryBuilder#bindMarker(). In this case, the statement is meant to be prepared and no JSON string will be appended to the query string, only a bind marker for the whole JSON parameter.
  3. Any object that can be serialized to JSON. Such objects can be used provided that a matching com.datastax.driver.core.TypeCodec is registered with the CodecRegistry in use. This allows the usage of JSON libraries, such as the Java API for JSON processing, the popular Jackson library, or Google's Gson library, for instance.

Case-sensitive column names

When passing raw strings to this method, users are required to handle case-sensitive column names by surrounding them with double quotes.

For example, to insert into a table with two columns named “myKey” and “value”, you would do the following:

insertInto("mytable").json("{\"\\\"myKey\\\"\": 0, \"value\": 0}");

This will produce the following CQL:

INSERT INTO mytable JSON '{"\"myKey\"": 0, "value": 0}';

Escaping quotes in column values

When passing raw strings to this method, double quotes should be escaped with a backslash, but single quotes should be escaped in the CQL manner, i.e. by another single quote. For example, the column value foo"'bar should be inserted in the JSON string as "foo"''bar".

Null values and tombstones

Any columns which are omitted from the JSON string will be defaulted to a NULL value (which will result in a tombstone being created).
[中]插入提供的对象,使用插入到。。。Cassandra 2.2中引入的JSON语法。
对于INSERT语句,可以使用新的JSON关键字将JSON结构作为单行插入。
提供的对象可以是以下类型:
1.原始字符串。在本例中,它将按原样追加到查询字符串中。它不应该用单引号括起来。其格式通常应与同一表上的SELECT JSON语句返回的格式匹配。请注意,无法在JSON字符串中插入函数调用或绑定标记。
1.查询生成器#bindMarker()。在本例中,语句是准备好的,不会向查询字符串追加任何JSON字符串,只会为整个JSON参数添加一个绑定标记。
1.任何可以序列化为JSON的对象。如果匹配com,则可以使用此类对象。数据税。驾驶员果心TypeCodec已在使用中的CodecRegistry中注册。这允许使用JSON库,例如Java API for JSON processing、流行的Jackson库或Google的{$2$}库。
####区分大小写的列名
当将原始字符串传递给此方法时,用户需要通过用双引号括起来处理区分大小写的列名。
例如,要插入到一个有两列名为“myKey”和“value”的表中,可以执行以下操作:

insertInto("mytable").json("{\"\\\"myKey\\\"\": 0, \"value\": 0}");

这将产生以下CQL:

INSERT INTO mytable JSON '{"\"myKey\"": 0, "value": 0}';

####在列值中转义引号
将原始字符串传递到此方法时,双引号应使用反斜杠转义,但单引号应以CQL方式转义,即通过另一个单引号转义。例如,列值foo“'bar应作为“foo\”bar插入JSON字符串中。
####空值和墓碑
JSON字符串中省略的任何列都将默认为空值(这将导致创建墓碑)。

代码示例

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

assertThat(
    insertInto("example")
      .json(
        "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}")
      .toString())
assertThat(
    insertInto("users")
      .json("{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}")
      .toString())
  .isEqualTo(
    "INSERT INTO users JSON '{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}';");
assertThat(insertInto("users").json(bindMarker()).toString())
  .isEqualTo("INSERT INTO users JSON ?;");
assertThat(insertInto("users").json(bindMarker("json")).toString())
  .isEqualTo("INSERT INTO users JSON :json;");
assertThat(
    insertInto("example")
      .json(
        "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}")
      .defaultNull()
assertThat(
    insertInto("example")
      .json(
        "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}")
      .defaultUnset()

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

.execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultUnset())
      .bind("{\"k\": 0, \"v2\": 2}"));
assertThat(session().execute(select().from(table))).containsExactly(row(0, 0, 2));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultNull())
      .bind("{\"k\": 0, \"v2\": 2}"));
assertThat(session().execute(select().from(table))).containsExactly(row(0, null, 2));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultNull())
      .bind("{\"k\": 0}"));
assertThat(session().execute(select().from(table))).containsExactly(row(0, null, null));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()))
      .bind("{\"k\": 1, \"v1\": 1, \"v2\": 1}"));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultUnset())
      .bind("{\"k\": 1, \"v1\": null}"));
assertThat(session().execute(select().from(table).where(eq("k", 1))))

代码示例来源:origin: com.datastax.dse/dse-java-driver-core

assertThat(
    insertInto("example")
      .json(
        "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}")
      .toString())
assertThat(
    insertInto("users")
      .json("{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}")
      .toString())
  .isEqualTo(
    "INSERT INTO users JSON '{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}';");
assertThat(insertInto("users").json(bindMarker()).toString())
  .isEqualTo("INSERT INTO users JSON ?;");
assertThat(insertInto("users").json(bindMarker("json")).toString())
  .isEqualTo("INSERT INTO users JSON :json;");
assertThat(
    insertInto("example")
      .json(
        "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}")
      .defaultNull()
assertThat(
    insertInto("example")
      .json(
        "{\"id\": 0, \"tupleval\": [1, \"abc\"], \"numbers\": [1, 2, 3], \"letters\": [\"a\", \"b\", \"c\"]}")
      .defaultUnset()

代码示例来源:origin: com.datastax.dse/dse-java-driver-core

.execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultUnset())
      .bind("{\"k\": 0, \"v2\": 2}"));
assertThat(session().execute(select().from(table))).containsExactly(row(0, 0, 2));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultNull())
      .bind("{\"k\": 0, \"v2\": 2}"));
assertThat(session().execute(select().from(table))).containsExactly(row(0, null, 2));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultNull())
      .bind("{\"k\": 0}"));
assertThat(session().execute(select().from(table))).containsExactly(row(0, null, null));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()))
      .bind("{\"k\": 1, \"v1\": 1, \"v2\": 1}"));
  .execute(
    session()
      .prepare(insertInto(table).json(bindMarker()).defaultUnset())
      .bind("{\"k\": 1, \"v1\": null}"));
assertThat(session().execute(select().from(table).where(eq("k", 1))))

相关文章