Re: How to Parse raw CQL text?

2018-02-26 Thread Jon Haddad
Yes ideally.  I’ve been spending a bit of time in the parser the last week.  
There’s a lot of internals which are still using old terminology and are pretty 
damn confusing.  I’m doing a little investigation into exposing some of the 
information while also modernizing it.  


> On Feb 26, 2018, at 10:02 AM, Hannu Kröger  wrote:
> 
> If this is needed functionality, shouldn’t that be available as a public 
> method or something? Maybe write a patch etc. ?
> 
> Ariel Weisberg > kirjoitti 
> 26.2.2018 kello 18.47:
> 
>> Hi,
>> 
>> I took a similar approach and it worked fine. I was able to build a tool 
>> that parsed production query logs.
>> 
>> I used a helper method that would just grab a private field out of an object 
>> by name using reflection.
>> 
>> Ariel
>> 
>> On Sun, Feb 25, 2018, at 11:58 PM, Jonathan Haddad wrote:
>>> 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 >> > 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, field4 map, 
>>> 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 >> > 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 >> > 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.si...@anant.us 
>>> 
>>> Anant Corporation
>>> 
>>> On Feb 5, 2018, 4:06 PM -0500, Kant Kodali >> >, 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 . I get the 
 following error.  And I thought maybe someone in this mailing list will be 
 more familiar with internals.  
 
 Exception in thread "main" 
 

Re: How to Parse raw CQL text?

2018-02-26 Thread Hannu Kröger
If this is needed functionality, shouldn’t that be available as a public method 
or something? Maybe write a patch etc. ?

> Ariel Weisberg  kirjoitti 26.2.2018 kello 18.47:
> 
> Hi,
> 
> I took a similar approach and it worked fine. I was able to build a tool that 
> parsed production query logs.
> 
> I used a helper method that would just grab a private field out of an object 
> by name using reflection.
> 
> Ariel
> 
>> On Sun, Feb 25, 2018, at 11:58 PM, Jonathan Haddad wrote:
>> 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  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, field4 map, 
>> 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  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  
>> 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.si...@anant.us
>> 
>> Anant Corporation
>> 
>> On Feb 5, 2018, 4:06 PM -0500, Kant Kodali , 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. 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, field4 map, 
>>> primary key (field1) );";
>>> 
>>> ANTLRStringStream stringStream = new ANTLRStringStream(stmt);
>>> 
>>> CqlLexer cqlLexer = new 

Re: How to Parse raw CQL text?

2018-02-26 Thread Kant Kodali
wouldn't it make sense to expose the parser at some point?

On Mon, Feb 26, 2018 at 9:47 AM, Ariel Weisberg  wrote:

> Hi,
>
> I took a similar approach and it worked fine. I was able to build a tool
> that parsed production query logs.
>
> I used a helper method that would just grab a private field out of an
> object by name using reflection.
>
> Ariel
>
> On Sun, Feb 25, 2018, at 11:58 PM, Jonathan Haddad wrote:
>
> 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  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, field4 map, 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  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 
> 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.si...@anant.us
>
> Anant Corporation
>
> On Feb 5, 2018, 4:06 PM -0500, Kant Kodali , 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 . 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, field4 map, 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() == 
> 

Re: How to Parse raw CQL text?

2018-02-26 Thread Ariel Weisberg
Hi,

I took a similar approach and it worked fine. I was able to build a tool
that parsed production query logs.
I used a helper method that would just grab a private field out of an
object by name using reflection.
Ariel

On Sun, Feb 25, 2018, at 11:58 PM, Jonathan Haddad wrote:
> 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  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, field4 map, 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
>>  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
>>>  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.si...@anant.us
 
  Anant Corporation
 
 On Feb 5, 2018, 4:06 PM -0500, Kant Kodali ,
 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[1]. 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$RawS-
> tatement.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, field4 map, primary key (field1)
> );"*;
>> ANTLRStringStream stringStream = *new
> 

Re: How to Parse raw CQL text?

2018-02-25 Thread Jonathan Haddad
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  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, field4 map, 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  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 > > 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.si...@anant.us
>>>
>>> Anant Corporation
>>>
>>> On Feb 5, 2018, 4:06 PM -0500, Kant Kodali , 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 . 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, field4 map, 
>>> 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;
>>>   

Re: How to Parse raw CQL text?

2018-02-05 Thread Kant Kodali
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,
field4 map, 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  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 
> 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.si...@anant.us
>>
>> Anant Corporation
>>
>> On Feb 5, 2018, 4:06 PM -0500, Kant Kodali , 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 . 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$Ra
>> wStatement.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, field4 map, 
>> 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!
>>
>>
>


Re: How to Parse raw CQL text?

2018-02-05 Thread Kant Kodali
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 
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.si...@anant.us
>
> Anant Corporation
>
> On Feb 5, 2018, 4:06 PM -0500, Kant Kodali , 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 . 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$Ra
> wStatement.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, field4 map, 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!
>
>


Re: How to Parse raw CQL text?

2018-02-05 Thread Rahul Singh
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.si...@anant.us

Anant Corporation

On Feb 5, 2018, 4:06 PM -0500, Kant Kodali , 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. 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, field4 map, 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!