CompanyJune 11, 2015

DataStax Java Driver: 2.2.0-rc1 released!

Alexandre Dutra
Alexandre Dutra
DataStax Java Driver: 2.2.0-rc1 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");
DateWithoutTime d = row.getDate("d");
long t = row.getTime("t");
cqlsh:test> CREATE TABLE ints(i int PRIMARY KEY);
cqlsh:test> INSERT INTO ints(i) VALUES (1);
cqlsh:test> INSERT INTO ints(i) VALUES (2);
cqlsh:test> INSERT INTO ints(i) VALUES (3);
cqlsh:test> CREATE FUNCTION inc(i int)
            RETURNS NULL ON NULL INPUT
            RETURNS int LANGUAGE java
            AS 'return i+1;';
cqlsh:test> select inc(i) from ints;
 test.inc(i)
-------------
           2
           3
           4
cqlsh:test> CREATE FUNCTION plus(s int, v int)
            RETURNS NULL ON NULL INPUT
            RETURNS int LANGUAGE java AS 'return s+v;';
cqlsh:test> CREATE AGGREGATE sum(int) SFUNC plus STYPE int INITCOND 0;
cqlsh:test> SELECT test.sum(i) FROM ints;
 test.sum(i)
-------------
           6
session.execute("SELECT test.sum(i) FROM ints");
// With the query builder:
import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
session.execute(select().fcall("test.sum", raw("i")).from("ints"));
AggregateMetadata sum = cluster.getMetadata()
    .getKeyspace("test")
    .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());
session.execute("CREATE TABLE IF NOT EXISTS unbound(k int PRIMARY KEY, v int)");
PreparedStatement pst = session.prepare("INSERT INTO unbound (k, v) VALUES (?, ?)");
// Normal execution: all values bound
session.execute(pst.bind(1, 1));
int v = session.execute("SELECT v FROM unbound WHERE k = 1")
    .one().getInt("v");
assert v == 1;
// New in 2.2: v is left unbound
session.execute(pst.bind(1));
v = session.execute("SELECT v FROM unbound WHERE k = 1")
    .one().getInt("v");
assert v == 1;
PreparedStatement pst = session.prepare("SELECT v FROM unbound WHERE k = ?");
session.execute(pst.bind());
// InvalidQueryException: Invalid unset value for column k
session.execute("CREATE TABLE IF NOT EXISTS example(k int primary key, v text)");
BatchStatement batch = new BatchStatement();
batch.add(new SimpleStatement("INSERT INTO example (k, v) VALUES (1, ?)",
    Strings.repeat("1", 5 * 1024)));
ResultSet rs = session.execute(batch);
List<String> warnings = rs.getExecutionInfo().getWarnings();
assert warnings.size() == 1;
// Batch of prepared statements for [test.example] is of size 5137,
// exceeding specified threshold of 5120 by 17.
Discover more
ReleasesJava
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.