[英]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
數據從一個表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
如何使用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連接器完成。
基本的事情。
從oldTable獲取數據。
從數據框中獲取最大ID
使用舊數據框創建新數據框。 注意.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.