【问题标题】:How to Insert into cassandra table using select from another table in cassandra?如何使用从 cassandra 中的另一个表中选择插入到 cassandra 表中?
【发布时间】:2017-10-28 02:01:54
【问题描述】:
insert into sys.new_table select id + (select max(id) from sys.Old_table),name from sys.Old_table;  

通过这个我们可以将insert的数据从一个表转移到Oracle中的另一个表。我如何在Cassandra 中编写此查询?

Old_table
    ID,Case Number,Date
    8534426,HV210935,03/19/2012 12:00:00 PM
    8534427,HV210768,12/16/2011 04:30:00 AM

我如何使用new_table.ID = Max(Old_table.ID)+Old_table.IDinsert 数据转换为new_table 以及Old_table 上的其他数据使用Cassandra?我可以在mysql 中使用上述语法进行插入。

new_table
    ID,Case Number,Date
    8534428,HV210935,03/19/2012 12:00:00 PM
    8534429,HV210768,12/16/2011 04:30:00 AM

如果这也可以使用Spark 解决,请建议我。

【问题讨论】:

    标签: mysql apache-spark cassandra cassandra-2.0 cassandra-2.2


    【解决方案1】:

    这可以使用 spark-cassandra 连接器来完成。

    基本要做的事情。

    1. 从 oldTable 中获取数据。

    2. 从数据帧中获取最大 id

    3. 使用旧数据框创建新数据框。注意.withColumn 应该有相同的列名id

    使用 scala 的示例代码:

    val oldTable = sc.read.formt("org.apache.spark.sql.cassandr")
                     .options(Map("keyspace"->"sys","table"->"Old_table"))
                     .load()
    
    val maxId = oldTable.select(max("id")).collect()(0).getAs[Int](0)
    
    val newTable = oldTable.withColumn("id",lit(maxId).plus(col("id")))
    
    newTable.write.format("org.apache.spark.sql.cassandr")
            .options(Map("keyspace"->"sys","table"->"new_table"))
            .save()
    

    这只是一个示例代码,其中 sc 是 SQLContext/HiveContext。

    根据您的数据大小,您可以在 oldTable.. 等上使用 .cache()

    根据您的要求修改代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2013-05-25
      • 2014-02-17
      相关资源
      最近更新 更多