繁体   English   中英

如何使用cassandra中另一个表中的选择插入cassandra表中?

[英]How to Insert into cassandra table using select from another table in cassandra?

insert into sys.new_table select id + (select max(id) from sys.Old_table),name from sys.Old_table;  

这样,我们就可以insert数据从一个表insertOracle另一个表。 如何在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

如何使用Cassandra insert数据插入new_table new_table.ID = Max(Old_table.ID)+Old_table.ID和其他数据,如Old_table 我可以在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解决此问题,请提出建议。

这可以使用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()等。

根据您的要求修改代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM