python - 如何使用 python unittest 模拟 aws 库 GlueContext
问题描述
无法从 aws 胶水模拟 GlueContext
我有这些代码行
sc = SparkContext()
gluecontext = GlueContext(sc)
spark = gluecontext.spark_session
如何使用python unittest模拟gluecontext?
解决方案
一种方法是将 Glue 库和 Spark 下载到项目的根目录(或根据需要进行配置)
wget https://github.com/awslabs/aws-glue-libs/archive/glue-1.0.zip
wget https://aws-glue-etl-artifacts.s3.amazonaws.com/glue-1.0/spark-2.4.3-bin-hadoop2.8.tgz
unzip glue-1.0.zip -d $PROJECT_ROOT
tar -xf spark-2.4.3-bin-hadoop2.8.tgz -C $PROJECT_ROOT
export SPARK_HOME=$PROJECT_ROOT/spark-2.4.3-bin-spark-2.4.3-bin-hadoop2.8
然后简单地模拟gluecontext
from mock import patch
class Test(unittest.TestCase):
@patch('awsglue.context.GlueContext')
@patch('awsglue.utils.getResolvedOptions', side_effect=mock_get_resolved_options)
def test_method(self, mock_resolve_options, mock_glue_context):
<your code>
在本地提交测试
$PROJECT_ROOT/aws-glue-libs-glue-1.0/bin/gluepytest $PROJECT_ROOT/tests/
推荐阅读
- java - 使用 Quarkus 连接到 Azure Key Vault
- python - 旋转图像后如何跟踪像素位置?
- sql - SQL如何从所有客户中检索最后订购的2个列出的产品?
- python - 为什么 "lala" + int 需要转换为 str 而 "lala" * 2 不需要?
- excel - VBA 调整表大小会导致整个工作簿中的数据验证副本
- python-3.x - python中的FIFO缓冲区
- asp.net-core - 对象属性上缺少 ASP .NET Core 生成的 Swagger 描述
- java - Java DatagramChannel - 在一次“读取”调用中读取所有可用的数据报
- typescript - Typescript:如何引用数组定义的子类型?
- c# - 从 SQL Server 在 C# 中查询 JSON