cassandra:输入端没有可行的替代方案

jfewjypa  于 2021-06-10  发布在  Cassandra
关注(0)|答案(1)|浏览(431)

我是cassandra数据库的新手,我正在尝试将sparkDataframe保存到cassandra数据库。
在创建表时,我得到了一个异常。 "SyntaxException: no viable alternative at input" .

val sparkContext = spark.sparkContext

    //Set the Log file level
    sparkContext.setLogLevel("WARN")

    //Connect Spark to Cassandra and execute CQL statements from Spark applications
    val connector = CassandraConnector(sparkContext.getConf)
    connector.withSessionDo(session =>
    {
      session.execute("DROP KEYSPACE IF EXISTS my_keyspace")
      session.execute("CREATE KEYSPACE my_keyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1}")
      session.execute("USE my_keyspace")
      session.execute("CREATE TABLE mytable('Inbound_Order_No' varchar,'Material' varchar,'Container_net_weight' double,'Shipping_Line' varchar,'Container_No' varchar,'Month' int,'Day' int,'Year' int,'Job_Run_Date' timestamp, PRIMARY KEY(Inbound_Order_No,Container_No))")

      df.write
        .format("org.apache.spark.sql.cassandra")
        .mode("overwrite")
        .option("confirm.truncate", "true")
        .option("spark.cassandra.connection.host", "localhost")
        .option("spark.cassandra.connection.port", "9042")
        .option("keyspace", "my_keyspace")
        .option("table", "mytable")
        .save()
    }
    )

我无法追踪错误,因此寻求帮助。
拜托note:i am 在windows系统中完成这项工作,一切都在本地设置。我也分享了我的Spark代码,如果你发现任何其他错误,那么请与我分享。

r1zhe5dt

r1zhe5dt1#

session.execute("CREATE TABLE mytable(\"Inbound_Order_No\" varchar,\"Material\" varchar,\"Container_net_weight\" double,\"Shipping_Line\" varchar,\"Container_No\" varchar,\"Month\" int,\"Day\" int,\"Year\" int,\"Job_Run_Date\" timestamp, PRIMARY KEY(\"Inbound_Order_No\",\"Container_No\"))")

双引号用于区分大小写的列,而不是单引号。

session.execute("CREATE TABLE mytable(Inbound_Order_No varchar,Material varchar,Container_net_weight double,Shipping_Line varchar,Container_No varchar,Month int,Day int,Year int,Job_Run_Date timestamp, PRIMARY KEY(Inbound_Order_No,Container_No))")

如果希望列名用小写,请使用上面的查询。。默认情况下,cassandra将创建小写的列名(如果不是用双引号括起来的话)
根据在cqlsh中运行的comment命令中的请求:

CREATE TABLE mytable("Inbound_Order_No" varchar,"Material" varchar,"Container_net_weight" double,"Shipping_Line" varchar,"Container_No" varchar,"Month" int,"Day" int,"Year" int,"Job_Run_Date" timestamp, PRIMARY KEY("Inbound_Order_No","Container_No"))

相关问题