Describe schema using the Java driver

274 views
Skip to first unread message

HM

unread,
Apr 10, 2017, 10:16:37 PM4/10/17
to DataStax Java Driver for Apache Cassandra User Mailing List

I need to save a keyspace schema.

The command that I would use, using the cqlsh interface would be:

"describe keyspace demo";

How can I do the same thing using the Java driver?

Jacques-Henri Berthemet

unread,
Apr 11, 2017, 3:22:57 AM4/11/17
to java-dri...@lists.datastax.com

You can use this on your session:

 

session.getCluster().getMetadata().getKeyspace(keyspaceName).getTables();

 

Then you can use exportAsString() to export the CQL command or other methods to explore the table structure.

--

Jacques-Henri Berthemet

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.

Alexandre Dutra

unread,
Apr 11, 2017, 3:58:20 AM4/11/17
to java-dri...@lists.datastax.com
Just to elaborate on Jacques-Henri's answer, you have two interesting methods in class KeyspaceMetadata:
  1. asCQLQuery(): returns the CREATE KEYSPACE statement that would re-create the keyspace;
  2. exportAsString()returns a human-readable string containing ALL the CQL queries representing this keyspace and its user types, tables, functions, aggregates, as well as secondary indexes and materialized views for every table. This would be the closest equivalent to cqlsh DESCRIBE KEYSPACE command.
Hope that helps,
Alexandre Dutra
--
Alexandre Dutra
Driver & Tools Engineer @ DataStax

Alexandre Dutra

unread,
Apr 11, 2017, 4:00:05 AM4/11/17
to java-dri...@lists.datastax.com
One last thing :) beware that exportAsString() has a known bug regarding nested UDTs: 

Seyed Hossein Mortazavi

unread,
Apr 12, 2017, 8:02:41 PM4/12/17
to java-dri...@lists.datastax.com
This really helped, thank you!

On Tue, Apr 11, 2017 at 3:59 AM, Alexandre Dutra <alexand...@datastax.com> wrote:
One last thing :) beware that exportAsString() has a known bug regarding nested UDTs: 
On Tue, Apr 11, 2017 at 9:58 AM Alexandre Dutra <alexand...@datastax.com> wrote:
Just to elaborate on Jacques-Henri's answer, you have two interesting methods in class KeyspaceMetadata:
  1. asCQLQuery(): returns the CREATE KEYSPACE statement that would re-create the keyspace;
  2. exportAsString()returns a human-readable string containing ALL the CQL queries representing this keyspace and its user types, tables, functions, aggregates, as well as secondary indexes and materialized views for every table. This would be the closest equivalent to cqlsh DESCRIBE KEYSPACE command.
Hope that helps,
Alexandre Dutra

On Tue, Apr 11, 2017 at 9:22 AM Jacques-Henri Berthemet <jacques-henri.berthemet@genesys.com> wrote:

You can use this on your session:

 

session.getCluster().getMetadata().getKeyspace(keyspaceName).getTables();

 

Then you can use exportAsString() to export the CQL command or other methods to explore the table structure.

--

Jacques-Henri Berthemet

 

From: java-driver-user@lists.datastax.com [mailto:java-driver-user@lists.datastax.com] On Behalf Of HM
Sent: mardi 11 avril 2017 04:17
To: DataStax Java Driver for Apache Cassandra User Mailing List <java-driver-user@lists.datastax.com>
Subject: Describe schema using the Java driver

 

 

I need to save a keyspace schema.

 

The command that I would use, using the cqlsh interface would be:

 

 

"describe keyspace demo";


How can I do the same thing using the Java driver?

 

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.

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

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
--
Alexandre Dutra
Driver & Tools Engineer @ DataStax
--
Alexandre Dutra
Driver & Tools Engineer @ DataStax

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.



--
Seyed Hossein Mortazavi

Phd Student
Computer Science
University of Toronto
Reply all
Reply to author
Forward
0 new messages