Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Using Tuples with the Java driver

Using Tuples with the Java driver¶

Cassandra allows to use tuple data types in tables and user-defined types:

CREATE TABLE ks.collect_things (
  pk int,
  ck1 text,
  ck2 text,
  v tuple<int, text, float>,
  PRIMARY KEY (pk, ck1, ck2)
);

Fetching Tuples from Rows results¶

The DataStax Java driver exposes a special TupleValue class to handle such columns. TupleValue exposes getters allowing to extract from the tuple all the data types supported by Cassandra:

Row row = session.execute("SELECT v FROM ks.collect_things WHERE pk = 1").one();

TupleValue tupleValue = row.getTupleValue("v");

int firstValueInTuple = tupleValue.getInt(0);

String secondValueInTuple = tupleValue.getString(1);

Float thirdValueInTuple = tupleValue.getFloat(2);

Using tuples as statement parameters¶

A prepared statement may contain a Tuple as a query parameter. In such cases, users will need to create or gather a TupleType first, in order to be able to create a TupleValue to bind:

PreparedStatement ps = session.prepare("INSERT INTO ks.collect_things (pk, ck1, ck2, v) VALUES (:pk, :ck1, :ck2, :v)");

TupleType tupleType = cluster.getMetadata().newTupleType(DataType.cint(), DataType.text(), DataType.cfloat());

BoundStatement bs = ps.bind();
bs.setInt("pk", 1);
bs.setString("ck1", "1");
bs.setString("ck2", "1");
bs.setTupleValue("v", tupleType.newValue(1, "hello", 2.3f));

session.execute(bs);

The method newValue(Object...) follows the same rules as new SimpleStatement(String, Object...); there can be ambiguities due to the fact that the driver will infer the data types from the values given in parameters of the method, whereas the data types required may differ (numeric literals are always interpreted as int).

To avoid such ambiguities, a TupleValue returned by newValue() also exposes specific setters for all the existing Cassandra data types:

TupleType tupleType = cluster.getMetadata().newTupleType(DataType.bigint(), DataType.text(), DataType.cfloat());

TupleValue value = tupleType.newValue().setLong(0, 2).setString(1, "hello").setDouble(2, 2.3f);

More use cases¶

Users can also define single-usage tuples in SELECT queries with the IN keyword (called a “multi-column IN restriction”), usually for tables with composite clustering keys. In this case, a tuple will be usable the same way it was for prepared statements’ parameters:

TupleType oneTimeUsageTuple = cluster.getMetadata().newTupleType(DataType.text(), DataType.text());

PreparedStatement ps = session.prepare("SELECT * FROM ks.collect_things WHERE pk = 1 and (ck1, ck2) IN (:t)");

BoundStatement bs = ps.bind();
bs.setTupleValue("t", oneTimeUsageTuple.newValue("1", "1"));

session.execute(bs);

More generally, the IN keyword in a SELECT query will be used to define a list of desired values of the filtered clustering keys, those would simply be bound as a list of TupleValue with the Java driver:

TupleType oneTimeUsageTuple = cluster.getMetadata().newTupleType(DataType.text(), DataType.text());

PreparedStatement ps = session.prepare("SELECT * FROM ks.collect_things WHERE pk = 1 AND (ck1, ck2) IN :l");

BoundStatement bs = ps.bind();
bs.setList("l", Arrays.asList(oneTimeUsageTuple.newValue("1", "1"), oneTimeUsageTuple.newValue("1", "2"), oneTimeUsageTuple.newValue("2", "1")));

session.execute(bs);
PREVIOUS
Batch statements
NEXT
User-defined types
  • 3.11.2.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Scylla Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • Address resolution
    • Asynchronous programming
    • Authentication
    • Compression
    • Control connection
    • Custom Codecs
    • Custom Payloads
    • Query idempotence
    • Load balancing
    • Logging
    • Metadata
    • Metrics
    • Native protocol
    • Object Mapper
      • Definition of mapped classes
      • Using custom codecs
      • Using the mapper
    • OSGi
    • Paging
    • Connection pooling
    • Query timestamps
    • Reconnection
    • Retries
    • Using the shaded JAR
    • Socket options
    • Speculative query execution
    • SSL
    • Statements
      • Simple statements
      • Prepared statements
      • Built statements
      • Batch statements
    • Using Tuples with the Java driver
    • User-defined types
  • Upgrade guide
    • Migrating from Astyanax
      • Configuration
      • Language change : from Thrift to CQL
      • Queries and Results
  • Frequently Asked Questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Using Tuples with the Java driver
    • Fetching Tuples from Rows results
    • Using tuples as statement parameters
      • More use cases
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2