Skip to content

Instantly share code, notes, and snippets.

@therako
Last active May 1, 2018 13:20
Show Gist options
  • Select an option

  • Save therako/c1966cfca165c20bf00c007d4d985f16 to your computer and use it in GitHub Desktop.

Select an option

Save therako/c1966cfca165c20bf00c007d4d985f16 to your computer and use it in GitHub Desktop.

Revisions

  1. therako revised this gist May 1, 2018. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions medium_ref_sstable_cql_scripts.scala
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,6 @@
    private def getCreateTableStatement(keyspace: String, tableName: String, df: DataFrame, keys: Array[String]) = {
    val primaryKey = keys.mkString(",")
    val fields = df.schema.map(field => field.name + " " + (field.dataType.simpleString.trim() match {
    case "array<array<string>>" => "LIST<frozen<LIST<text>>>"
    case "array<string>" => "list<text>"
    case "string" => "text"
    case _ => field.dataType.simpleString.trim()
    }))
    val fields = df.schema.map(field => field.name + " " + field.dataType.simpleString.trim())
    val fieldsDesc = fields.mkString(", ")
    "CREATE TABLE if not exists " + keyspace + "." + tableName + " (" + fieldsDesc + ", PRIMARY KEY (" + primaryKey + "))"
    }
  2. therako created this gist May 1, 2018.
    17 changes: 17 additions & 0 deletions medium_ref_sstable_cql_scripts.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    private def getCreateTableStatement(keyspace: String, tableName: String, df: DataFrame, keys: Array[String]) = {
    val primaryKey = keys.mkString(",")
    val fields = df.schema.map(field => field.name + " " + (field.dataType.simpleString.trim() match {
    case "array<array<string>>" => "LIST<frozen<LIST<text>>>"
    case "array<string>" => "list<text>"
    case "string" => "text"
    case _ => field.dataType.simpleString.trim()
    }))
    val fieldsDesc = fields.mkString(", ")
    "CREATE TABLE if not exists " + keyspace + "." + tableName + " (" + fieldsDesc + ", PRIMARY KEY (" + primaryKey + "))"
    }

    private def getInsertStatement(keyspace: String, tableName: String, df: DataFrame) = {
    val fieldsDesc = df.schema.fields.map(f => f.name).mkString(",")
    val valuesHolder = df.schema.fields.map(_ => "?").mkString(",")
    "insert into " + keyspace + "." + tableName + " (" + fieldsDesc + ") values (" + valuesHolder + ")"
    }