【问题标题】:Ingest "t" and "f" as boolean to Cassandra将“t”和“f”作为布尔值输入到 Cassandra
【发布时间】:2021-01-18 14:37:18
【问题描述】:

我使用 pyspark 将 csv 作为数据帧加载,然后将其保存到 Cassandra。其中一列在 Cassandra 的模式中定义为布尔值,但我在 csv 中的实际数据是字符串tf。我有没有机会让 Cassandra 将 tf 识别为布尔值?否则我必须添加一个数据转换步骤。

【问题讨论】:

    标签: apache-spark cassandra boolean spark-cassandra-connector


    【解决方案1】:

    Spark Cassandra 连接器uses String.toBoolean call 将字符串转换为布尔值。但它只接受truefalse,如果与其他字符串一起使用,则会引发异常。所以你需要编写小数据转换代码,如下所示:

    scala> val df = Seq((1, "t"), (2, "f"), (3, "t")).toDF("id", "b")
    df: org.apache.spark.sql.DataFrame = [id: int, b: string]
    
    scala> val df2 = df.withColumn("b", $"b" === "t")
    df2: org.apache.spark.sql.DataFrame = [id: int, b: boolean]
    
    scala> df2.show()
    +---+-----+
    | id|    b|
    +---+-----+
    |  1| true|
    |  2|false|
    |  3| true|
    +---+-----+
    

    【讨论】:

      猜你喜欢
      • 2013-03-22
      • 1970-01-01
      • 2018-06-09
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      相关资源
      最近更新 更多