amazon-web-services - AWS Glue 动态过滤 - 使用另一个动态帧过滤一个动态帧
问题描述
我正在尝试根据驻留在另一个动态框架中的数据过滤动态过滤,我正在处理连接和关系示例,在此代码中,人员和成员资格动态框架由 id 连接,但我想根据存在的 id 过滤人员会员资格 DF,下面是我放置静态值的代码
import sys
from awsglue.transforms import Join
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
glueContext = GlueContext(SparkContext.getOrCreate())
# catalog: database and table names
db_name = "legislators"
tbl_persons = "persons_json"
tbl_membership = "memberships_json"
tbl_organization = "organizations_json"
# Create dynamic frames from the source tables
persons = glueContext.create_dynamic_frame.from_catalog(database=db_name, table_name=tbl_persons)
memberships = glueContext.create_dynamic_frame.from_catalog(database=db_name, table_name=tbl_membership)
orgs = glueContext.create_dynamic_frame.from_catalog(database=db_name, table_name=tbl_organization)
persons = persons.drop_fields(['links', 'identifiers','other_names', 'images','contact_details'])
# Keep the fields we need and rename some.
orgs = orgs.drop_fields(['other_names', 'identifiers','links'])
fileredPersons = Filter.apply(frame = persons,
f = lambda x: x["id"] in ["0005af3a-9471-4d1f-9299-737fff4b9b46", "00aca284-9323-4953-bb7a-1bf6f5eefe95"])
print "Filtered record count: ", fileredPersons.count()
下面是过滤逻辑
fileredPersons = Filter.apply(frame = persons,
f = lambda x: x["id"] in ["0005af3a-9471-4d1f-9299-737fff4b9b46", "00aca284-9323-4953-bb7a-1bf6f5eefe95"])
我想将会员 DF 中存在的 person_id 列传递到过滤器功能条件中,基本上过滤具有会员资格的人,任何帮助将不胜感激。
解决方案
您可以简单地执行内部连接,而不是像过滤
persons_filtered = persons.alias('persons').join(memberships, persons.id==memberships.id).select('persons.*')
这只会为您提供过滤后的值。如果您的会员 df 很小或某种查找,那么您甚至可以广播它以获得更快的结果
from pyspark.sql.functions import broadcast
persons_filtered = persons.alias('persons').join(broadcast(memberships), persons.id==memberships.id).select('persons.*')
希望能帮助到你。
推荐阅读
- javascript - 如何排列文本从 div 的底部开始?
- flutter - Flutter:小部件库错误捕获的异常
- r - 具有可变宽度的重叠条形图/直方图
- python - Discord.py Reddit Meme 命令
- javascript - 如何让 Tailwind UI 导航栏汉堡菜单在我的 React 应用程序中运行?
- javascript - Javascript 模板文字在声明为变量时有效,但在作为 json 模板传递时无效
- python - input() 语句的使用
- python - 优化字典中每个键之间的比较(Python)
- mysql - 为什么当我尝试添加外键时 MYSQL 出错?
- javascript - 在 JavaScript 中重新分配全局变量 - 有人可以向我解释为什么 currentAcc 保持未定义