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

问题描述:

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

通过这个,我们可以将insert数据从一个表格移动到另一个表格Oracle。我如何在Cassandra中写这个查询?如何使用cassandra中的另一个表选择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 

我怎样才能insert数据为new_tablenew_table.ID = Max(Old_table.ID)+Old_table.ID和其他数据作为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也得到解决。

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

基本的事情要做。

  1. 从oldTable中获取数据。

  2. 获取从数据帧

  3. 最大ID使用旧的数据帧创建新的数据帧。注意.withColumn应具有相同的列名id

例如代码使用阶:

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() ..等

修改根据您的需要的代码。