use of com.datastax.driver.core.UDTValue in project cassandra by apache.
the class UFJavaTest method testJavaUserTypeWithUse.
@Test
public void testJavaUserTypeWithUse() throws Throwable {
String type = createType("CREATE TYPE %s (txt text, i int)");
createTable("CREATE TABLE %s (key int primary key, udt frozen<" + KEYSPACE + '.' + type + ">)");
execute("INSERT INTO %s (key, udt) VALUES (1, {txt: 'one', i:1})");
for (ProtocolVersion version : PROTOCOL_VERSIONS) {
executeNet(version, "USE " + KEYSPACE);
executeNet(version, "CREATE FUNCTION f_use1( udt " + type + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS " + type + " " + "LANGUAGE java " + "AS $$return " + " udt;$$;");
try {
List<Row> rowsNet = executeNet(version, "SELECT f_use1(udt) FROM %s WHERE key = 1").all();
Assert.assertEquals(1, rowsNet.size());
UDTValue udtVal = rowsNet.get(0).getUDTValue(0);
Assert.assertEquals("one", udtVal.getString("txt"));
Assert.assertEquals(1, udtVal.getInt("i"));
} finally {
executeNet(version, "DROP FUNCTION f_use1");
}
}
}
use of com.datastax.driver.core.UDTValue in project cassandra by apache.
the class UFJavaTest method testJavaUserType.
@Test
public void testJavaUserType() throws Throwable {
String type = KEYSPACE + '.' + createType("CREATE TYPE %s (txt text, i int)");
createTable("CREATE TABLE %s (key int primary key, udt frozen<" + type + ">)");
String fUdt0 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS " + type + " " + "LANGUAGE java " + "AS $$return " + " udt;$$;");
String fUdt1 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + ") " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE java " + "AS $$return " + " udt.getString(\"txt\");$$;");
String fUdt2 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + ") " + "CALLED ON NULL INPUT " + "RETURNS int " + "LANGUAGE java " + "AS $$return " + " Integer.valueOf(udt.getInt(\"i\"));$$;");
execute("INSERT INTO %s (key, udt) VALUES (1, {txt: 'one', i:1})");
UntypedResultSet rows = execute("SELECT " + fUdt0 + "(udt) FROM %s WHERE key = 1");
Assert.assertEquals(1, rows.size());
assertRows(execute("SELECT " + fUdt1 + "(udt) FROM %s WHERE key = 1"), row("one"));
assertRows(execute("SELECT " + fUdt2 + "(udt) FROM %s WHERE key = 1"), row(1));
for (ProtocolVersion version : PROTOCOL_VERSIONS) {
List<Row> rowsNet = executeNet(version, "SELECT " + fUdt0 + "(udt) FROM %s WHERE key = 1").all();
Assert.assertEquals(1, rowsNet.size());
UDTValue udtVal = rowsNet.get(0).getUDTValue(0);
Assert.assertEquals("one", udtVal.getString("txt"));
Assert.assertEquals(1, udtVal.getInt("i"));
assertRowsNet(version, executeNet(version, "SELECT " + fUdt1 + "(udt) FROM %s WHERE key = 1"), row("one"));
assertRowsNet(version, executeNet(version, "SELECT " + fUdt2 + "(udt) FROM %s WHERE key = 1"), row(1));
}
}
use of com.datastax.driver.core.UDTValue in project cassandra by apache.
the class CQLSSTableWriterTest method testWritesWithUdts.
@Test
@SuppressWarnings("unchecked")
public void testWritesWithUdts() throws Exception {
final String KS = "cql_keyspace3";
final String TABLE = "table3";
final String schema = "CREATE TABLE " + KS + "." + TABLE + " (" + " k int," + " v1 list<frozen<tuple2>>," + " v2 frozen<tuple3>," + " PRIMARY KEY (k)" + ")";
File tempdir = Files.createTempDir();
File dataDir = new File(tempdir.getAbsolutePath() + File.separator + KS + File.separator + TABLE);
assert dataDir.mkdirs();
CQLSSTableWriter writer = CQLSSTableWriter.builder().inDirectory(dataDir).withType("CREATE TYPE " + KS + ".tuple2 (a int, b int)").withType("CREATE TYPE " + KS + ".tuple3 (a int, b int, c int)").forTable(schema).using("INSERT INTO " + KS + "." + TABLE + " (k, v1, v2) " + "VALUES (?, ?, ?)").build();
UserType tuple2Type = writer.getUDType("tuple2");
UserType tuple3Type = writer.getUDType("tuple3");
for (int i = 0; i < 100; i++) {
writer.addRow(i, ImmutableList.builder().add(tuple2Type.newValue().setInt("a", i * 10).setInt("b", i * 20)).add(tuple2Type.newValue().setInt("a", i * 30).setInt("b", i * 40)).build(), tuple3Type.newValue().setInt("a", i * 100).setInt("b", i * 200).setInt("c", i * 300));
}
writer.close();
loadSSTables(dataDir, KS);
UntypedResultSet resultSet = QueryProcessor.executeInternal("SELECT * FROM " + KS + "." + TABLE);
TypeCodec collectionCodec = UDHelper.codecFor(DataType.CollectionType.frozenList(tuple2Type));
TypeCodec tuple3Codec = UDHelper.codecFor(tuple3Type);
assertEquals(resultSet.size(), 100);
int cnt = 0;
for (UntypedResultSet.Row row : resultSet) {
assertEquals(cnt, row.getInt("k"));
List<UDTValue> values = (List<UDTValue>) collectionCodec.deserialize(row.getBytes("v1"), ProtocolVersion.NEWEST_SUPPORTED);
assertEquals(values.get(0).getInt("a"), cnt * 10);
assertEquals(values.get(0).getInt("b"), cnt * 20);
assertEquals(values.get(1).getInt("a"), cnt * 30);
assertEquals(values.get(1).getInt("b"), cnt * 40);
UDTValue v2 = (UDTValue) tuple3Codec.deserialize(row.getBytes("v2"), ProtocolVersion.NEWEST_SUPPORTED);
assertEquals(v2.getInt("a"), cnt * 100);
assertEquals(v2.getInt("b"), cnt * 200);
assertEquals(v2.getInt("c"), cnt * 300);
cnt++;
}
}
use of com.datastax.driver.core.UDTValue in project cassandra by apache.
the class CQLSSTableWriterTest method testWritesWithDependentUdts.
@Test
@SuppressWarnings("unchecked")
public void testWritesWithDependentUdts() throws Exception {
final String KS = "cql_keyspace4";
final String TABLE = "table4";
final String schema = "CREATE TABLE " + KS + "." + TABLE + " (" + " k int," + " v1 frozen<nested_tuple>," + " PRIMARY KEY (k)" + ")";
File tempdir = Files.createTempDir();
File dataDir = new File(tempdir.getAbsolutePath() + File.separator + KS + File.separator + TABLE);
assert dataDir.mkdirs();
CQLSSTableWriter writer = CQLSSTableWriter.builder().inDirectory(dataDir).withType("CREATE TYPE " + KS + ".nested_tuple (c int, tpl frozen<tuple2>)").withType("CREATE TYPE " + KS + ".tuple2 (a int, b int)").forTable(schema).using("INSERT INTO " + KS + "." + TABLE + " (k, v1) " + "VALUES (?, ?)").build();
UserType tuple2Type = writer.getUDType("tuple2");
UserType nestedTuple = writer.getUDType("nested_tuple");
TypeCodec tuple2Codec = UDHelper.codecFor(tuple2Type);
TypeCodec nestedTupleCodec = UDHelper.codecFor(nestedTuple);
for (int i = 0; i < 100; i++) {
writer.addRow(i, nestedTuple.newValue().setInt("c", i * 100).set("tpl", tuple2Type.newValue().setInt("a", i * 200).setInt("b", i * 300), tuple2Codec));
}
writer.close();
loadSSTables(dataDir, KS);
UntypedResultSet resultSet = QueryProcessor.executeInternal("SELECT * FROM " + KS + "." + TABLE);
assertEquals(resultSet.size(), 100);
int cnt = 0;
for (UntypedResultSet.Row row : resultSet) {
assertEquals(cnt, row.getInt("k"));
UDTValue nestedTpl = (UDTValue) nestedTupleCodec.deserialize(row.getBytes("v1"), ProtocolVersion.NEWEST_SUPPORTED);
assertEquals(nestedTpl.getInt("c"), cnt * 100);
UDTValue tpl = nestedTpl.getUDTValue("tpl");
assertEquals(tpl.getInt("a"), cnt * 200);
assertEquals(tpl.getInt("b"), cnt * 300);
cnt++;
}
}
Aggregations