cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Haddad <...@jonhaddad.com>
Subject Re: How to Parse raw CQL text?
Date Mon, 26 Feb 2018 04:58:24 GMT
I had to do something similar recently.  Take a look at
org.apache.cassandra.cql3.QueryProcessor.parseStatement().  I've got some
sample code here [1] as well as a blog post [2] that explains how to access
the private variables, since there's no access provided.  It wasn't really
designed to be used as a library, so YMMV with future changes.

[1]
https://github.com/rustyrazorblade/rustyrazorblade-examples/blob/master/privatevaraccess/src/main/kotlin/com/rustyrazorblade/privatevaraccess/CreateTableParser.kt
[2]
http://rustyrazorblade.com/post/2018/2018-02-25-accessing-private-variables-in-jvm/

On Mon, Feb 5, 2018 at 2:27 PM Kant Kodali <kant@peernova.com> wrote:

> I just did some trial and error. Looks like this would work
>
> public class Test {
>
>     public static void main(String[] args) throws Exception {
>
>         String stmt = "create table if not exists test_keyspace.my_table (field1 text,
field2 int, field3 set<ascii>, field4 map<ascii, text>, primary key (field1) );";
>         ANTLRStringStream stringStream = new ANTLRStringStream(stmt);
>         CqlLexer cqlLexer = new CqlLexer(stringStream);
>         CommonTokenStream token = new CommonTokenStream(cqlLexer);
>         CqlParser parser = new CqlParser(token);
>
>         ParsedStatement query = parser.cqlStatement();
>
>
>         if (query.getClass().getDeclaringClass() == CreateTableStatement.class) {
>             CreateTableStatement.RawStatement cts = (CreateTableStatement.RawStatement)
query;
>
>             CFMetaData
>                 .compile(stmt, cts.keyspace())
>
>
>                 .getColumnMetadata()
>                 .values()
>                 .stream()
>                 .forEach(cd -> System.out.println(cd));
>
>         }
>
>    }
>
> }
>
>
> On Mon, Feb 5, 2018 at 2:13 PM, Kant Kodali <kant@peernova.com> wrote:
>
>> Hi Anant,
>>
>> I just have CQL create table statement as a string I want to extract all
>> the parts like, tableName, KeySpaceName, regular Columns,  partitionKey,
>> ClusteringKey, Clustering Order and so on. Thats really  it!
>>
>> Thanks!
>>
>> On Mon, Feb 5, 2018 at 1:50 PM, Rahul Singh <rahul.xavier.singh@gmail.com
>> > wrote:
>>
>>> I think I understand what you are trying to do … but what is your goal?
>>> What do you mean “use it for different” queries… Maybe you want to do an
>>> event and have an event processor? Seems like you are trying to basically
>>> by pass that pattern and parse a query and split it into several actions?
>>>
>>> Did you look into this unit test folder?
>>>
>>>
>>> https://github.com/apache/cassandra/blob/trunk/test/unit/org/apache/cassandra/cql3/CQLTester.java
>>>
>>> --
>>> Rahul Singh
>>> rahul.singh@anant.us
>>>
>>> Anant Corporation
>>>
>>> On Feb 5, 2018, 4:06 PM -0500, Kant Kodali <kant@peernova.com>, wrote:
>>>
>>> Hi All,
>>>
>>> I have a need where I get a raw CQL create table statement as a String
>>> and I need to parse the keyspace, tablename, columns and so on..so I can
>>> use it for various queries and send it to C*. I used the example below
>>> from this link <https://github.com/tacoo/cassandra-antlr-sample>. I get
>>> the following error.  And I thought maybe someone in this mailing list will
>>> be more familiar with internals.
>>>
>>> Exception in thread "main"
>>> org.apache.cassandra.exceptions.ConfigurationException: Keyspace
>>> test_keyspace doesn't exist
>>> at
>>> org.apache.cassandra.cql3.statements.CreateTableStatement$RawStatement.prepare(CreateTableStatement.java:200)
>>> at com.hello.world.Test.main(Test.java:23)
>>>
>>>
>>> Here is my code.
>>>
>>> package com.hello.world;
>>>
>>> import org.antlr.runtime.ANTLRStringStream;
>>> import org.antlr.runtime.CommonTokenStream;
>>> import org.apache.cassandra.cql3.CqlLexer;
>>> import org.apache.cassandra.cql3.CqlParser;
>>> import org.apache.cassandra.cql3.statements.CreateTableStatement;
>>> import org.apache.cassandra.cql3.statements.ParsedStatement;
>>>
>>> public class Test {
>>>
>>>     public static void main(String[] args) throws Exception {
>>>         String stmt = "create table if not exists test_keyspace.my_table (field1
text, field2 int, field3 set<ascii>, field4 map<ascii, text>, primary key (field1)
);";
>>>         ANTLRStringStream stringStream = new ANTLRStringStream(stmt);
>>>         CqlLexer cqlLexer = new CqlLexer(stringStream);
>>>         CommonTokenStream token = new CommonTokenStream(cqlLexer);
>>>         CqlParser parser = new CqlParser(token);
>>>         ParsedStatement query = parser.query();
>>>         if (query.getClass().getDeclaringClass() == CreateTableStatement.class)
{
>>>             CreateTableStatement.RawStatement cts = (CreateTableStatement.RawStatement)
query;
>>>             System.out.println(cts.keyspace());
>>>             System.out.println(cts.columnFamily());
>>>             ParsedStatement.Prepared prepared = cts.prepare();
>>>             CreateTableStatement cts2 = (CreateTableStatement) prepared.statement;
>>>             cts2.getCFMetaData()
>>>                     .getColumnMetadata()
>>>                     .values()
>>>                     .stream()
>>>                     .forEach(cd -> System.out.println(cd));
>>>         }
>>>     }
>>> }
>>>
>>> Thanks!
>>>
>>>
>>
>

Mime
View raw message