[英]Pyspark : Multiple join condition with cast type as string
在 Spark SQL 中,我需要将 as_of_date 转换为字符串,并在连接后对 3 个表和 select 表 1、2 和 3 中的所有行和列进行多重内连接。 示例表架构如下所示
Tablename : Table_01 alias t1
Column | Datatype
as_of_date | String
Tablename | String
Credit_Card | String
Tablename : Table_02 alias t2
Column | Datatype
as_of_date | INT
Customer_name | String
tablename | string
Tablename : Table_03 alias t3
Column | Datatype
as_of_date | String
tablename | String
address | String
加入用例:
t1.as_of_date = t2.as_of_date AND t1.tablename = t2.tablename
t2.as_of_date = t3.as_of_date AND t2.tablename = t3.tablename
表已经在 hive 中创建,我正在对这些表进行 spark 转换,并将 table_02 中的 as_of_date 转换为字符串。
我想到了两种方法,但我不确定哪种方法最好
方法一:
df = spark.sql("select t1.*,t2.*,t3.* from table_1 t1 where cast(t1.as_of_date as string) inner join table_t2 t2 on t1.as_of_date = t2.as_of_date AND t1.tablename = t2.tablename inner join table_03 t3 on t2.as_of_date = t3.as_of_date and t2.tablename = t3.tablename")
方法二:
df_t1 = spark.sql("select * from table_01");
df_t2 = spark.sql("select * from table_02");
df_t3 = spark.sql("select * from table_03");
## Cast as_of_date as String if dtype as of date is int
if dict(df_t2.dtypes)["as_of_date"] == 'int':
df_t1["as_of_date"].cast(cast(StringType())
## Join Condition
df = df_t1.alias('t1').join(df_t2.alias('t2'),on="t1.tablename=t2.tablename AND t1.as_of_date = t2.as_of_date", how='inner').join(df_t3.alias('t3'),on="t2.as_of_date = t3.as_of_date AND t2.tablename = t3.tablename",how='inner').select('t1.*,t2.*,t3.*')
我觉得使用方法 2 是冗长的,我需要一些关于我应该使用 go 的方法的建议,以便于维护和使用的脚本
我建议如下直接使用 Spark SQL。 无论数据类型如何,您都可以将所有表中的每个 as_of_date 列转换为字符串。 您想将 integer 转换为字符串,但是如果您也将字符串转换为字符串,则没有害处。
df = spark.sql("""
select t1.*, t2.*, t3.*
from t1
join t2 on string(t1.as_of_date) = string(t2.as_of_date) AND t1.tablename = t2.tablename
join t3 on string(t2.as_of_date) = string(t3.as_of_date) AND t2.tablename = t3.tablename
""")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.