How to create a keyspace and table in cassandra using R?

300 Views Asked by At

I am currently working on sparklyr and I am trying to create a new keyspace in Cassandra. Is it possible at all? If so, how?

I was trying below expression:

sql(sqlContext, "CREATE KEYSPACE key1 WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1 }")

But in result I have obtained error:

  Error in invokeJava(isStatic = FALSE, objId$id, methodName, ...) : 
  java.lang.RuntimeException: [1.1] failure: ``with'' expected but identifier CREATE found
1

There are 1 best solutions below

0
On BEST ANSWER

There is no SQL Command in Spark SQL for creating new Cassandra Tables or Keyspaces. Spark Sql is only able to create or destroy metadata relating to Cassandra Tables which already exist. To create new tables you need to use the custom DataFrame Apis added in Scala/Java.

val renamed = df.withColumnRenamed("col1", "newcolumnname")
renamed.createCassandraTable(
    "test", 
    "renamed", 
    partitionKeyColumns = Some(Seq("user")), 
    clusteringKeyColumns = Some(Seq("newcolumnname")))

Dataframe Docs

This basically means you would have to do some kind of cross language invocation of Java from R. I don't know of any simple way to do this and I would recommend just invoking the any of the Cassandra drivers from a supported language. For example just using Python and the python driver.