1

Error:

cassandra.protocol.SyntaxException: \
  <Error from server: code=2000 [Syntax error in CQL query] \
  message="line 1:36 no viable alternative at input '(' \
  (CREATE TABLE  master_table(dict_keys[(]...)">

Code:

cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)

session=cluster.connect('firstkey')

ColName={"qty_dot_url": "int",
"qty_hyphen_url": "int",
"qty_underline_url": "int",
"qty_slash_url": "int"}

columns =  ColName.keys()
values = ColName.values()

session.execute('CREATE TABLE  master_table({ColName} {dataType}),PRIMARY KEY(qty_dot_url)'.format(ColName=columns, dataType=values))
    

How to resolve above mentioned error?

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23

2 Answers2

1

So I replaced the session.execute with a print, and it produced this:

CREATE TABLE  master_table(dict_keys(['qty_dot_url', 'qty_hyphen_url', 'qty_underline_url', 'qty_slash_url']) dict_values(['int', 'int', 'int', 'int'])),PRIMARY KEY(qty_dot_url)

That is not valid CQL. It needs to look like this:

CREATE TABLE  master_table(qty_dot_url int, qty_hyphen_url int,
  qty_underline_url int, qty_slash_url int, PRIMARY KEY(qty_dot_url))

I was able to create that by making these adjustments to your code:

createTableCQL = "CREATE TABLE master_table("

for key, value in ColName.items():
    createTableCQL += key + " " + value + ", "

createTableCQL += "PRIMARY KEY(qty_dot_url))"

You could then follow that with a session.execute(createTableCQL).

Notes:

  • The PRIMARY KEY definition must be inside the paren list.
  • Creating schema from inside application code is often problematic, and can create a schema disagreement in the cluster. It's almost always better to create tables outside of code.
Aaron
  • 55,518
  • 11
  • 116
  • 132
0

The syntax exception is a result of your Python code generating an invalid CQL which Aaron pointed out in his response.

To add to his answer, you need to add additional steps whenever you are programatically making schema changes. In particular, you need to make sure that you check for schema agreement (i.e. the schema change has been propagated to all nodes) before moving on to the next bit in your code.

You will need to modify your code to save the result from the schema change, for example:

resultset = session.execute(SimpleStatement("CREATE TABLE ..."))

then call this in your code:

resultset.response_future.is_schema_agreed

You'll need to loop through this check until True is returned. Depending on how long you want to wait (default max_schema_agreement_wait is 10 seconds), you'll need to implement some logic to do [something] when schema agreement is not achieved (because a node is down for example) -- this requires manual intervention from an operator to investigate the cluster.

As Aaron already said, performing schema changes programatically is very problematic and we discourage doing this unless you fully understand the pitfalls and know how to handle failures. Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23