dataframe:获取表a中存在但不在表b中的记录计数表b有2900万条记录(Pypark公司)

stszievb  于 2021-05-29  发布在  Spark
关注(0)|答案(1)|浏览(614)

表a——是一个只有1列和47000条记录的Dataframe,当distinct应用14000条记录时。
表b是一个Dataframe,只有1列,2900万条记录了所有不同的值。
我需要得到表a中的记录数,而不是表b中的记录数。但在pysparkshell(本地模式)中运行below查询时,不知何故我遇到了below错误。

Table_A         Table_B
123450          
123451
123452          123452
123453          123453
123454          123454
                123455
                123456
                123457

输出expected:- 2

DB_accnum=spark.sql("select org_acctnum from Table_A where 'some filter conditions'")

ACC_repository=spark.sql("select account_num from Table_B")

DB_accnum_d=DB_accnum.select('org_acctnum').distinct()
DB_accnum_d.persist()
broadcast(DB_accnum_d)
DB_accnum_d.count()

R_join= ACC_repository.join(DB_accnum_d,ACC_repository.account_num == 
DB_accnum.org_acctnum,how='rightouter')
R_join.count()

在这之后我到下面error:-

R_join.count()
20/06/14 22:03:15 WARN TaskMemoryManager: Failed to allocate a page (1073741824 bytes), try again.
20/06/14 22:03:16 WARN TaskMemoryManager: Failed to allocate a page (1073741824 bytes), try again.

内部连接工作正常。(尝试count(),在内部连接的df上显示(n=5)
在获取join count之后,我打算过滤表b中不存在的记录,然后获取新的\u df的计数,但在这两者之间得到了错误。
谁能告诉我这是正确的方法还是我做错了什么?

at0kjp5o

at0kjp5o1#

您可以通过运行减号查询或在dataframeapi中使用exceptall来实现这一点
只要确保列别名相同,并且两个dataframe包含相同的模式。

DB_accnum = spark.sql("select org_acctnum from Table_A where 'some filter conditions'")

ACC_repository = spark.sql("select account_num as Id from Table_B")

DB_accnum_d = DB_accnum.select(col('org_acctnum').alias('Id').distinct()

difference = DB_accnum_d.exceptAll(ACC_repository)

# difference will contain the account number present in DB_accnum_d and not present in ACC_repository

difference.count()
difference.show()

相关问题