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 Upgrade guide Migrating from Astyanax Queries and Results

Queries and Results¶

There are many resources such as this post or this post to learn how to transform previous Thrift operations to CQL queries.

The Java driver executes CQL queries through the Session. The queries can either be simple CQL Strings or represented in the form of Statements. The driver offers 4 kinds of statements, SimpleStatement, Prepared/BoundStatement, BuiltStatement, and BatchStatement. All necessary information can be found here about the nature of the different Statements.

As explained in the running section, results of a CQL query will be in the form of Rows from Tables, composed of fixed set of columns, each with a type and a name. The driver exposes the set of Rows returned from a query as a ResultSet, thus containing Rows on which getXXX() can be called. Here are simple examples of translation from Astyanax to Java driver in querying and retrieving query results.

Single column¶

Astyanax:

ColumnFamily<String, String> CF_STANDARD1 = new ColumnFamily<String, String>("cf1", StringSerializer.get(), StringSerializer.get(). StringSerializer.get());

Column<String> result = keyspace.prepareQuery(CF_STANDARD1)
    .getKey("1")
    .getColumn("3")
    .execute().getResult();
String value = result.getStringValue();

Java driver:

Row row = session.execute("SELECT value FROM table1 WHERE key = '1' AND column1 = '3'").one();
String value = row.getString("value");

All columns¶

Astyanax:

ColumnList<String> columns;
int pagesize = 10;
RowQuery<String, String> query = keyspace
       .prepareQuery(CF_STANDARD1)
       .getKey("1")
       .autoPaginate(true)
       .withColumnRange(new RangeBuilder().setLimit(pagesize).build());

while (!(columns = query.execute().getResult()).isEmpty()) {
   for (Column<String> c : columns) {
       String value = c.getStringValue();
   }
}

Java driver:

ResultSet rs = session.execute("SELECT value FROM table1 WHERE key = '1'");
for (Row row : rs) {
   String value = row.getString("value");
}

Column range¶

Astyanax:

ColumnList<String> result;
result = keyspace.prepareQuery(CF_STANDARD1)
       .getKey("1")
       .withColumnRange(new RangeBuilder().setStart("3").setEnd("5").setMaxSize(100).build())
       .execute().getResult();

Iterator<Column<String>> it = result.iterator();
while (it.hasNext()) {
   Column<String> col = it.next();
   String value = col.getStringValue();
}

Java driver:

ResultSet rs = session.execute("SELECT value FROM table1 WHERE key = '1'" +
       " AND column1 > '3'" +
       " AND column1 < '5'");
for (Row row : rs) {
   String value = row.getString("value");
}

Async¶

The Java driver provides native support for asynchronous programming since it is built on top of an asynchronous protocol, please see this page for best practices regarding asynchronous programming with the Java driver.

PREVIOUS
Language change : from Thrift to CQL
NEXT
Frequently Asked Questions
  • 3.11.0.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

  • Queries and Results
    • Single column
    • All columns
    • Column range
    • Async
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