CompanyJanuary 26, 2016

DataStax Java Driver: 3.0.0 released!

Alexandre Dutra
Alexandre Dutra
DataStax Java Driver: 3.0.0 released!
session.execute("CREATE TABLE IF NOT EXISTS small_ints(s smallint PRIMARY KEY, t tinyint)");
PreparedStatement pst = session.prepare("INSERT INTO small_ints (s, t) VALUES (:s, :t)");
session.execute(pst.bind(Short.MIN_VALUE, Byte.MAX_VALUE));
Row row = session.execute("SELECT * FROM small_ints").one();
short s = row.getShort("s");
byte t = row.getByte("t");
session.execute(pst.bind(1, 1));
// InvalidTypeException: Invalid type for value 0 of CQL type smallint,
// expecting class java.lang.Short but class java.lang.Integer provided
session.execute(pst.bind((short)1, (byte)1));
session.execute("CREATE TABLE IF NOT EXISTS dates(ts timestamp PRIMARY KEY, d date, t time)");
session.execute("INSERT INTO dates (ts, d, t) VALUES ('2015-01-28 11:47:58', '2015-01-28', '11:47:58')");
Row row = session.execute("SELECT * FROM dates").one();
Date ts = row.getTimestamp("ts");
LocalDate d = row.getDate("d");
long t = row.getTime("t");
BoundStatement bound = ps1.bind().setString("foo", "bar");
// Unset by name
bound.unset("foo");
// Unset by index
bound.unset(0);
MaterializedViewMetadata mv = cluster.getMetadata()
    .getKeyspace("test").getMaterializedView("my_view");
TableMetadata table = cluster.getMetadata()
    .getKeyspace("test").getTable("my_table")
MaterializedViewMetadata mv = table.getView("my_view");
// You can also query all views in that table:
System.out.printf("Table %s has the following views: %s%n", table.getName(), table.getViews());
USE test;
CREATE FUNCTION plus(x int, y int)
            RETURNS NULL ON NULL INPUT
            RETURNS int LANGUAGE java AS 'return x + y;';
CREATE AGGREGATE sum(int)
            SFUNC plus
            STYPE int
            INITCOND 0;
FunctionMetadata plus = cluster.getMetadata()
    .getKeyspace(keyspace)
    .getFunction("plus", DataType.cint(), DataType.cint());
System.out.printf("Function %s has signature %s and body '%s'%n",
    plus.getSimpleName(), plus.getSignature(), plus.getBody());
AggregateMetadata sum = cluster.getMetadata()
    .getKeyspace(keyspace)
    .getAggregate("sum", DataType.cint());
System.out.printf("%s is an aggregate that computes a result of type %s%n",
    sum.getSimpleName(), sum.getReturnType());
FunctionMetadata plus = sum.getStateFunc();
System.out.printf("%s is a function that operates on %s%n",
    plus.getSimpleName(), plus.getArguments());
TableMetadata table = cluster.getMetadata()
        .getKeyspace("test")
        .getTable("my_table");
IndexMetadata index = table.getIndex("my_index");
System.out.printf("Table %s has index %s targeting %s%n", table.getName(), index.getName(), index.getTarget());
IndexMetadata index = table.getIndex("my_index");
ColumnMetadata indexedColumn = table.getColumn(index.getTarget());
System.out.printf("Index %s operates on column %s%n", index.getName(), indexedColumn);
ResultSet rs = session.execute(...);
List<String> warnings = rs.getExecutionInfo().getWarnings();
// Roll your own codec
TypeCodec<MyPojo> myJsonCodec = ...;
// register it so the driver can use it
cluster.getConfiguration().getCodecRegistry().register(myJsonCodec);
// query some JSON data
Row row = session.execute("SELECT json FROM t WHERE pk = 'I CAN HAZ JSON'").one();
// Let the driver convert it for you...
MyPojo myPojo = row.get("json", MyPojo.class);
<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-extras</artifactId>
  <version>3.0.0</version>
</dependency>
Cluster cluster = Cluster.builder()
        .addContactPoints("127.0.0.1")
        // by default, statements will be considered non-idempotent
        .withQueryOptions(new QueryOptions().setDefaultIdempotence(false))
        // make your retry policy idempotence-aware
        .withRetryPolicy(new IdempotenceAwareRetryPolicy(DefaultRetryPolicy.INSTANCE))
        .build();
Session session = cluster.connect();
// by default, statements like this one will not be retried
session.execute("INSERT INTO table (pk, c1) VALUES (42, 'foo')");
// but this one will
session.execute(new SimpleStatement("SELECT c1 FROM table WHERE pk = 42").setIdempotent(true));
// Note the use of named parameters in the query
String query = "SELECT * FROM measures WHERE sensor_id=:sensor_id AND day=:day";
Map<String, Object> params = new HashMap<String, Object>();
params.put("sensor_id", 42);
params.put("day", "2016-01-28");
SimpleStatement statement = new SimpleStatement(query, params);
Discover more
Releases
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.