【问题标题】:How to preserve order of columns in cassandra如何在 cassandra 中保留列的顺序
【发布时间】:2015-12-28 08:47:17
【问题描述】:

我在 Cassandra 中有两张桌子:

CREATE TABLE table1 (
          name text PRIMARY KEY,
          grade text, 
          labid List<int>);

CREATE TABLE table2(
          name text PRIMARY KEY,
          deptid List<int>  
          grade text,);

例如:

val result: RDD[String, String, List[Int]] = myFunction();
result.saveToCassandra(keyspace, table1)

它工作正常。 但如果使用以下行:

result.saveToCassandra(keyspace, table2)

m 收到此类错误:com.datastax.spark.connector.types.TypeConversionException: 无法将 java.lang.String 类型的对象 test_data 转换为 List[AnyRef]

是否有使用 SomeColumns 满足两个表的解决方案[我们不知道将执行哪个表]。例如:

result.saveToCassandra(keyspace, table, SomeColumns(....))?

【问题讨论】:

    标签: scala cassandra datastax


    【解决方案1】:

    默认情况下,数据框架构只关心位置,而不关心列名,因此如果您的 c* 表具有不同的列顺序,您将得到不正确的写入。解决方案就像你说的那样,使用SomeColumns

    val columns = dataFrame.schema.map(_.name: ColumnRef)    
    dataFrame.rdd.saveToCassandra(keyspaceName, tableName, SomeColumns(columns: _*))
    

    现在数据框列将使用它们的名称而不是位置写入 c*。

    【讨论】:

      【解决方案2】:

      你的参数应该有不同的顺序,因为表有不同的列类型:

      val result: RDD[String, String, List[Int]] = myFunction();
      val reorder: RDD[String, List[Int], String] = result.map(r => r._1, r._3, r._2)
      reorder.saveToCassandra(keyspace, table2)
      

      【讨论】:

      • 是的,我知道您的解决方案。但是还有其他解决方案吗?
      • 有一些列会有用吗?
      猜你喜欢
      • 1970-01-01
      • 2014-09-22
      • 2013-03-17
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2015-05-12
      相关资源
      最近更新 更多