如何在火花中处理这个 [英] how to handle this in spark

查看:39
本文介绍了如何在火花中处理这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 spark-sql 2.4.x 版本,Cassandra-3.x 版本使用的是 datastax-spark-cassandra-connector.与 kafka 一起.

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

我有一个来自 kafka 主题的财务数据的场景.data(基础数据集)包含 companyId, year , prev_year 字段信息.

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

如果列 year === prev_year 那么我需要加入不同的表,即 exchange_rates.

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

如果列 year =!= prev_year 那么我需要返回基础数据集本身

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

如何在 spark-sql 中做到这一点?

How to do this in spark-sql ?

推荐答案

您可以针对您的情况参考以下方法.

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|
+---------+----+---------+----+

这篇关于如何在火花中处理这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆