如何在Spark中处理这个问题

huangapple go评论52阅读模式
英文:

how to handle this in spark

问题

我正在使用spark-sql 2.4.x版本,datastax-spark-cassandra-connector与Cassandra-3.x版本一起使用,还有kafka。

我有一个场景,从kafka主题中获取一些财务数据。数据(基础数据集)包含companyId、year和prev_year字段信息。

如果列year等于prev_year,则我需要与不同的表即exchange_rates进行连接。

如果列year不等于prev_year,则我需要返回基础数据集本身。

如何在spark-sql中实现这个目标?

英文:

I am using spark-sql 2.4.x version , datastax-spark-cassandra-connector for Cassandra-3.x version. Along with kafka.

I have a scenario for some finance data coming from kafka topic. data (base dataset) contains companyId, year , prev_year fields information.

If columns year === prev_year then I need to join with different table i.e. exchange_rates.

If columns year =!= prev_year then I need to return the base dataset itself

How to do this in spark-sql ?

答案1

得分: 1

以下是您的翻译:

您可以参考以下方法处理您的情况

scala> Input_df.show
+---------+----+---------+----+
|companyId|year|prev_year|rate|
+---------+----+---------+----+
|        1|2016|     2017|  12|
|        1|2017|     2017|21.4|
|        2|2018|     2017|11.7|
|        2|2018|     2018|44.6|
|        3|2016|     2017|34.5|
|        4|2017|     2017|  56|
+---------+----+---------+----+

scala> exch_rates.show
+---------+----+
|companyId|rate|
+---------+----+
|        1|12.3|
|        2|12.5|
|        3|22.3|
|        4|34.6|
|        5|45.2|
+---------+----+

scala> val equaldf = Input_df.filter(col("year") === col("prev_year"))

scala> val notequaldf = Input_df.filter(col("year") =!= col("prev_year"))

scala> val joindf  = notequaldf.alias("n").drop("rate").join(exch_rates.alias("e"), List("companyId"), "left")

scala> val finalDF = equaldf.union(joindf)

scala> finalDF.show()
+---------+----+---------+----+
|companyId|year|prev_year|rate|
+---------+----+---------+----+
|        1|2017|     2017|21.4|
|        2|2018|     2018|44.6|
|        4|2017|     2017|  56|
|        1|2016|     2017|12.3|
|        2|2018|     2017|12.5|
|        3|2016|     2017|22.3|
+---------+----+---------+----+
英文:

You can refer below approach for your case.

scala> Input_df.show
+---------+----+---------+----+
|companyId|year|prev_year|rate|
+---------+----+---------+----+
|        1|2016|     2017|  12|
|        1|2017|     2017|21.4|
|        2|2018|     2017|11.7|
|        2|2018|     2018|44.6|
|        3|2016|     2017|34.5|
|        4|2017|     2017|  56|
+---------+----+---------+----+
scala> exch_rates.show
+---------+----+
|companyId|rate|
+---------+----+
|        1|12.3|
|        2|12.5|
|        3|22.3|
|        4|34.6|
|        5|45.2|
+---------+----+
scala> val equaldf = Input_df.filter(col("year") === col("prev_year"))
scala> val notequaldf = Input_df.filter(col("year") =!= col("prev_year"))
scala> val joindf  = notequaldf.alias("n").drop("rate").join(exch_rates.alias("e"), List("companyId"), "left")
scala> val finalDF = equaldf.union(joindf)
scala> finalDF.show()
+---------+----+---------+----+
|companyId|year|prev_year|rate|
+---------+----+---------+----+
|        1|2017|     2017|21.4|
|        2|2018|     2018|44.6|
|        4|2017|     2017|  56|
|        1|2016|     2017|12.3|
|        2|2018|     2017|12.5|
|        3|2016|     2017|22.3|
+---------+----+---------+----+

huangapple
  • 本文由 发表于 2020年1月3日 18:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定