Class Insert
- java.lang.Object
-
- com.datastax.driver.core.Statement
-
- com.datastax.driver.core.RegularStatement
-
- com.datastax.driver.core.querybuilder.BuiltStatement
-
- com.datastax.driver.core.querybuilder.Insert
-
public class Insert extends BuiltStatement
A builtINSERTstatement.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classInsert.OptionsThe options of anINSERTstatement.
-
Field Summary
-
Fields inherited from class com.datastax.driver.core.Statement
idempotent, NULL_PAYLOAD_VALUE
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description InsertdefaultNull()Appends aDEFAULT NULLclause to thisINSERT INTO ... JSONstatement.InsertdefaultUnset()Appends aDEFAULT UNSETclause to thisINSERT INTO ... JSONstatement.InsertifNotExists()Sets the 'IF NOT EXISTS' option for thisINSERTstatement.Insertjson(Object json)Inserts the provided object, using theINSERT INTO ... JSONsyntax introduced in Cassandra 2.2.Insert.Optionsusing()Returns the options for thisINSERTstatement.Insert.Optionsusing(Using using)Adds a new options for thisINSERTstatement.Insertvalue(String name, Object value)Adds a column/value pair to the values inserted by thisINSERTstatement.Insertvalues(String[] names, Object[] values)Adds multiple column/value pairs to the values inserted by this INSERT statement.Insertvalues(List<String> names, List<Object> values)Adds multiple column/value pairs to the values inserted by this INSERT statement.-
Methods inherited from class com.datastax.driver.core.querybuilder.BuiltStatement
escapeId, getKeyspace, getNamedValues, getObject, getObject, getQueryString, getRoutingKey, getValues, hasValues, isIdempotent, setForceNoValues, toString, usesNamedValues
-
Methods inherited from class com.datastax.driver.core.RegularStatement
getQueryString, hasValues, requestSizeInBytes
-
Methods inherited from class com.datastax.driver.core.Statement
disableTracing, enableTracing, getConsistencyLevel, getDefaultTimestamp, getFetchSize, getHost, getLastHost, getNowInSeconds, getOutgoingPayload, getPartitioner, getReadTimeoutMillis, getRetryPolicy, getSerialConsistencyLevel, isBatchIdempotent, isLWT, isTracing, setConsistencyLevel, setDefaultTimestamp, setFetchSize, setHost, setIdempotent, setLastHost, setNowInSeconds, setOutgoingPayload, setPagingState, setPagingState, setPagingStateUnsafe, setReadTimeoutMillis, setRetryPolicy, setSerialConsistencyLevel
-
-
-
-
Method Detail
-
value
public Insert value(String name, Object value)
Adds a column/value pair to the values inserted by thisINSERTstatement.- Parameters:
name- the name of the column to insert/update.value- the value to insert/update forname.- Returns:
- this
INSERTstatement. - Throws:
IllegalStateException- if this method is called and thejson(Object)method has been called before, because it's not possible to mixINSERT JSONsyntax with regularINSERTsyntax.
-
values
public Insert values(String[] names, Object[] values)
Adds multiple column/value pairs to the values inserted by this INSERT statement.- Parameters:
names- a list of column names to insert/update.values- a list of values to insert/update. Theith value invalueswill be inserted for theith column innames.- Returns:
- this INSERT statement.
- Throws:
IllegalArgumentException- ifnames.length != values.length.IllegalStateException- if this method is called and thejson(Object)method has been called before, because it's not possible to mixINSERT JSONsyntax with regularINSERTsyntax.
-
values
public Insert values(List<String> names, List<Object> values)
Adds multiple column/value pairs to the values inserted by this INSERT statement.- Parameters:
names- a list of column names to insert/update.values- a list of values to insert/update. Theith value invalueswill be inserted for theith column innames.- Returns:
- this INSERT statement.
- Throws:
IllegalArgumentException- ifnames.size() != values.size().IllegalStateException- if this method is called and thejson(Object)method has been called before, because it's not possible to mixINSERT JSONsyntax with regularINSERTsyntax.
-
json
public Insert json(Object json)
Inserts the provided object, using theINSERT INTO ... JSONsyntax introduced in Cassandra 2.2.With INSERT statements, the new
JSONkeyword can be used to enable inserting a JSON structure as a single row.The provided object can be of the following types:
- 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 JSONstatement on the same table. Note that it is not possible to insert function calls nor bind markers in a JSON string. - A
bind marker. 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. - Any object that can be serialized to JSON. Such objects can be used provided that a
matching
codecis registered with theCodecRegistryin 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 valuefoo"'barshould 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 aNULLvalue (which will result in a tombstone being created).- Parameters:
json- the JSON string, or a bind marker, or a JSON object handled by a specificcodec.- Returns:
- this INSERT statement.
- Throws:
IllegalStateException- if this method is called and any of thevalueorvaluesmethods have been called before, because it's not possible to mixINSERT JSONsyntax with regularINSERTsyntax.- See Also:
- JSON Support for CQL, JSON Support in Cassandra 2.2, Inserting JSON data
- 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
-
defaultUnset
public Insert defaultUnset()
Appends aDEFAULT UNSETclause to thisINSERT INTO ... JSONstatement.Support for
DEFAULT UNSEThas been introduced in Cassandra 3.10.- Returns:
- this
INSERTstatement. - Throws:
IllegalStateException- if this method is called and any of thevalueorvaluesmethods have been called before, because it's not possible to mixINSERT JSONsyntax with regularINSERTsyntax.
-
defaultNull
public Insert defaultNull()
Appends aDEFAULT NULLclause to thisINSERT INTO ... JSONstatement.Support for
DEFAULT NULLhas been introduced in Cassandra 3.10.- Returns:
- this
INSERTstatement. - Throws:
IllegalStateException- if this method is called and any of thevalueorvaluesmethods have been called before, because it's not possible to mixINSERT JSONsyntax with regularINSERTsyntax.
-
using
public Insert.Options using(Using using)
Adds a new options for thisINSERTstatement.- Parameters:
using- the option to add.- Returns:
- the options of this
INSERTstatement.
-
using
public Insert.Options using()
Returns the options for thisINSERTstatement.Chain this with
Insert.Options.and(Using)to add options.- Returns:
- the options of this
INSERTstatement.
-
ifNotExists
public Insert ifNotExists()
Sets the 'IF NOT EXISTS' option for thisINSERTstatement.An insert with that option will not succeed unless the row does not exist at the time the insertion is executed. The existence check and insertions are done transactionally in the sense that if multiple clients attempt to create a given row with this option, then at most one may succeed.
Please keep in mind that using this option has a non negligible performance impact and should be avoided when possible.
This will configure the statement as non-idempotent, see
Statement.isIdempotent()for more information.- Returns:
- this
INSERTstatement.
-
-