Core Driver



Core driver

The core module handles cluster connectivity and request execution. It is published under thefollowing coordinates:

Core

(For more details on setting up your build tool, see the integration page.)

CqlSession is the main entry point of the driver. It holds the known state of the actual Cassandra cluster, and is what you use to execute queries. It is thread-safe, you should create a single instance (per target Cassandra cluster), and share it throughout your application; we use execute to send a query to Cassandra. CqlSession is the main entry point of the driver. It holds the known state of the actual Cassandra cluster, and is what you use to execute queries. It is thread-safe, you should create a single instance (per target Cassandra cluster), and share it throughout your application; we use execute to. To make the contents of the core driver package accessible to the PnP installer, open a Command Prompt window and use the expand command to expand the MSU file. Also expand the appropriate cabinet (.cab) file, which has a name that starts with 'Windows6.0-', that is contained in the MSU file.

Quick start

Here’s a short program that connects to Cassandra and executes a query:

  1. CqlSession is the main entry point of the driver. It holds the known state of the actualCassandra cluster, and is what you use to execute queries. It is thread-safe, you should create asingle instance (per target Cassandra cluster), and share it throughout your application;
  2. we use execute to send a query to Cassandra. This returns a ResultSet, which is an iterable of Row objects. On the next line, we extract the first row (which is the only one in this case);
  3. we extract the value of the first (and only) column from the row.

Always close the CqlSession once you’re done with it, in order to free underlying resources (TCP connections, thread pools…). In this simple example, we can use a try-with-resources block becauseCqlSession implements java.lang.AutoCloseable; in a real application, you’ll probably call oneof the close methods (close, closeAsync, forceCloseAsync) explicitly.

This example uses the synchronous API. Most methods have asynchronous equivalents (look for *Asyncvariants that return a CompletionStage).

Setting up the driver

CqlSession#builder() provides a fluent API to create an instance programmatically. Most of thecustomization is done through the driver configuration (refer to thecorresponding section of this manual for full details).

We recommend that you take a look at the reference configuration for thelist of available options, and cross-reference with the sub-sections in this manual for moreexplanations.

By default, a session isn’t tied to any specific keyspace. You’ll need to prefix table names in yourqueries:

You can also specify a keyspace at construction time, either through theconfiguration:

Or with the builder:

That keyspace will be used as the default when table names are not qualified:

You might be tempted to open a separate session for each keyspace used in your application; however,connection pools are created at the session level, so each new session will consume additionalsystem resources:

If you issue a USE statement, it will change the default keyspace on that session:

Be very careful though: switching the keyspace at runtime is inherently thread-unsafe, so if thesession is shared by multiple threads (and is usually is), it could easily cause unexpected queryfailures.

Finally, if you’re connecting to Cassandra 4 or above, you can specify the keyspace independentlyfor each request:

Running queries

You run queries with the session’s execute* methods:

As shown here, the simplest form is to pass a query string directly. You can also pass aStatement instance.

Processing rows

Executing a query produces a ResultSet, which is an iterable of Row. The basic way to processall rows is to use Java’s for-each loop:

This will return all results without limit (even though the driver might use multiple queries inthe background). To handle large result sets, you might want to use a LIMIT clause in your CQLquery, or use one of the techniques described in the paging documentation.

When you know that there is only one row (or are only interested in the first one), the driverprovides a convenience method:

Reading columns

Row provides getters to extract column values; they can be either positional or named:

CqlIdentifier is a string wrapper that deals with case-sensitivity. If you don’t want to create aninstance for each getter call, the driver also provides convenience methods that take a raw string:

See AccessibleByName for an explanation of the conversion rules.

CQL to Java type mapping
CQL3 data typeGetter nameJava typeSee also
asciigetStringjava.lang.String
bigintgetLonglong
blobgetBytesjava.nio.ByteBuffer
booleangetBooleanboolean
countergetLonglong
dategetLocalDatejava.time.LocalDateTemporal types
decimalgetBigDecimaljava.math.BigDecimal
doublegetDoubledouble
durationgetCqlDurationCqlDurationTemporal types
floatgetFloatfloat
inetgetInetAddressjava.net.InetAddress
intgetIntint
listgetListjava.util.List
mapgetMapjava.util.Map
setgetSetjava.util.Set
smallintgetShortshort
textgetStringjava.lang.String
timegetLocalTimejava.time.LocalTimeTemporal types
timestampgetInstantjava.time.InstantTemporal types
timeuuidgetUuidjava.util.UUID
tinyintgetBytebyte
tuplegetTupleValueTupleValueTuples
user-defined typesgetUDTValueUDTValueUser-defined types
uuidgetUuidjava.util.UUID
varchargetStringjava.lang.String
varintgetVarintjava.math.BigInteger
Core Driver

Sometimes the driver has to infer a CQL type from a Java type (for example when handling the values of simple statements); for those that have multiple CQL equivalents, it makesthe following choices:

  • java.lang.String: text
  • long: bigint
  • java.util.UUID: uuid

In addition to these default mappings, you can register your own types withcustom codecs.

Primitive types

For performance reasons, the driver uses primitive Java types wherever possible (boolean,int…); the CQL value NULL is encoded as the type’s default value (false, 0…), which canbe ambiguous. To distinguish NULL from actual values, use isNull:

Corel Drivers For Windows 10

Collection types

To ensure type safety, collection getters are generic. You need to provide type parameters matchingyour CQL type when calling the methods:

Core Drivers Of Info Age

Core Driver

For nested collections, element types are generic and cannot be expressed as Java Class instances.Use GenericType instead:

Core Driver Update

Since generic types are anonymous inner classes, it’s recommended to store them as constants in autility class instead of re-creating them each time.

Row metadata

Core Drivers

ResultSet and Row expose an API to explore the column metadata at runtime: