【问题标题】:How to set consistency level ONE and ifNotExists in spark cassandra DataFrame如何在 spark cassandra DataFrame 中设置一致性级别 ONE 和 ifNotExists
【发布时间】:2017-07-29 12:54:27
【问题描述】:

我在 cassandra 3.9 中有一个柜台

create table counter_table ( id text, hour_no int, platform text, type text, title text,
count_time counter,
PRIMARY KEY (id, hour_no,  platform, type , title));

我的 spark (2.1.0) scala (2.11) 代码是

import com.datastax.driver.core.{ConsistencyLevel, DataType}
import com.datastax.spark.connector.writer.WriteConf
val writeConf = WriteConf(consistencyLevel = ConsistencyLevel.ONE, ifNotExists = true)

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "false").option("inferSchema", "true").load("csv_file_path")

val newNames = Seq("id" , "hour_no" , "platform" , "type" , "title" , "count_time")
val dfRenamed = df.toDF(newNames: _*)

dfRenamed.write.format("org.apache.spark.sql.cassandra").
     mode(SaveMode.Append).options(Map( "table" -> "counter_table", "keyspace" -> "key1", 
    "output.consistency.level" -> "LOCAL_ONE", "output.ifNotExists" -> "true" )).save()    

火花代码给出一致性错误

Caused by: com.datastax.driver.core.exceptions.WriteFailureException: 
Cassandra failure during write query at consistency LOCAL_QUORUM (2 responses were required but only 1 replica responded, 1 failed)

我们如何在 DataFrame 中指定 ONE 的一致性

【问题讨论】:

    标签: scala apache-spark cassandra spark-dataframe spark-cassandra-connector


    【解决方案1】:

    您的两个参数都缺少开头

    所有参数都应以 spark.cassandra 为前缀。

    但是你还有第二个问题。

    不可能以SERIAL 以外的任何一致性级别执行IF NOT EXISTS 查询,因为它使用 PAXOS。这意味着你不应该做ONE

    更新:我现在知道使用 Paxos CL 可以做一些非常危险的事情,因此可以为部分事务强制使用不同的 CL,但你不应该这样做,因为你基本上会破坏你想要的所有保证首先检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2017-06-14
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多