Determining which UDT a table is using at the field level

12 views
Skip to first unread message

Jeremy Truelove

unread,
Aug 27, 2015, 5:10:40 PM8/27/15
to DataStax Java Driver for Apache Cassandra User Mailing List

Is there a programatic way to determine which UDTs a table is referring to by looking at one of the system tables? If you setup the example CQL below and look at the result of the select query you'll notice that in the 'validator' field it's not possible to really determine which UDT is being referenced for which field. There's a # after the keyspace which appears to uniquely identify the UDT actually being used but I'm not sure where to get that mapping from. I haven't been able to locate it on any system tables.



CREATE KEYSPACE IF NOT EXISTS "test"

  WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };


  CREATE TYPE if not exists test.udt_type (

      street text,

      city text,

      zip int

  );

  

    CREATE TYPE if not exists test.udt_type2 (

      street text,

      city text,

      zip int

  );


create table if not exists test.two_udts(

  id uuid,

  type1 frozen<udt_type>,

  type2 frozen<udt_type2>,

  type3 frozen<udt_type2>,

  PRIMARY KEY(id)

  );


-- shows you that there are 2 UDTs on this table but not which one is mapped to which field

  select * from system.schema_columns where keyspace_name = 'test';


-- shows you the UDTs that are there

select * from system.schema_usertypes where keyspace_name = 'test';

Andrew Tolbert

unread,
Aug 27, 2015, 5:59:29 PM8/27/15
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Jeremy,

It is definitely not trivial to parse this, it's done in the driver using CassandraTypeParser.parseOne.   The type name is the second parameter in UserType but it's the hexbytes for the name:

org.apache.cassandra.db.marshal.UserType(test,7564745f74797065,737472656574:org.apache.cassandra.db.marshal.UTF8Type,63697479:org.apache.cassandra.db.marshal.UTF8Type,7a6970:org.apache.cassandra.db.marshal.Int32Type)

The rest of the data is a series of hexBytes_name:type associated with the type.

It'd probably be best to let the driver do this for you, i.e the following shows you that the columns associated with the table belong to the expected types:

            KeyspaceMetadata ksm = cluster.getMetadata().getKeyspace("test");
            TableMetadata tm = ksm.getTable("two_udts");
            UserType udtType = ksm.getUserType("udt_type");
            UserType udtType2 = ksm.getUserType("udt_type2");
            assert udtType == tm.getColumn("type1").getType();
            assert udtType2 == tm.getColumn("type2").getType();
            assert udtType2 == tm.getColumn("type3").getType();

Thanks,
Andy

Andrew Tolbert

unread,
Aug 27, 2015, 6:04:39 PM8/27/15
to DataStax Java Driver for Apache Cassandra User Mailing List
Ah, I should have mentioned.  To get the type name from the column, you can cast it to UserType and then use getTypeName(), i.e.:

((UserType)tm.getColumn("type3").getType()).getTypeName())

Would yield 'udt_type2' in this case.

Thanks!
Andy

On Thursday, August 27, 2015 at 4:10:40 PM UTC-5, Jeremy Truelove wrote:

Jeremy Truelove

unread,
Aug 27, 2015, 6:07:41 PM8/27/15
to java-dri...@lists.datastax.com
Yeah I figured that string was the type just wasn't sure how to back it out to something useful. Much appreciated. I'm building some code generation based on scheme reading so it looks like i can do a lot of this with just the driver probably vs parsing and building it out myself.

To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.

Reply all
Reply to author
Forward
0 new messages