【问题标题】:Pass parameters/arguments to HDInsight/Spark Activity in Azure Data Factory将参数/参数传递给 Azure 数据工厂中的 HDInsight/Spark Activity
【发布时间】:2022-08-04 17:46:11
【问题描述】:
我有一个从 Azure 数据工厂中的 Spark Activity 启动并运行 PySpark 3.1 的按需 HDInsight 群集。为了测试我的代码,我通常从创建的 HDInsight 群集页面启动 Jupyter Notebook。
现在,我想将一些参数传递给该 Spark 活动,并从 Jupyter 笔记本代码中检索这些参数。我尝试过两种方法,但没有一种方法对我有用:
方法 A。作为参数,然后尝试使用sys.argv[] 检索它们。
方法 B。作为 Spark 配置,然后尝试使用 sc.getConf().getAll() 检索它们。
我怀疑要么:
- 我没有正确指定参数
- 或使用错误的方式在 Jupyter Notebook 代码中检索它们
- 或参数仅对“文件路径”字段中指定的 Python
*.py 脚本有效,但对 Jupyter 笔记本无效。
任何有关如何将参数传递到 Azure 数据工厂中的 HDInsight Spark 活动的指针将不胜感激。
标签:
pyspark
azure-data-factory
azure-hdinsight
【解决方案1】:
问题在于entryFilePath。在 HDInsight 群集的 Spark 活动中,您必须将 entryFilePath 作为.jar 文件或者.py 文件.当我们遵循这一点时,我们可以成功地传递可以使用sys.argv 的参数。
from pyspark import SparkContext
from pyspark.sql import *
import sys
sc = SparkContext()
sqlContext = HiveContext(sc)
# Create an RDD from sample data which is already available
hvacText = sc.textFile("wasbs:///HdiSamples/HdiSamples/SensorSampleData/hvac/HVAC.csv")
# Create a schema for our data
Entry = Row('Date', 'Time', 'TargetTemp', 'ActualTemp', 'BuildingID')
# Parse the data and create a schema
hvacParts = hvacText.map(lambda s: s.split(',')).filter(lambda s: s[0] != 'Date')
hvac = hvacParts.map(lambda p: Entry(str(p[0]), str(p[1]), int(p[2]), int(p[3]), int(p[6])))
# Infer the schema and create a table
hvacTable = sqlContext.createDataFrame(hvac)
hvacTable.registerTempTable('hvactemptable')
dfw = DataFrameWriter(hvacTable)
#using agrument from pipeline to create table.
dfw.saveAsTable(sys.argv[1])
- 当管道被触发时,它会成功运行并创建所需的表。我们可以使用以下查询在 HDInsight 群集的
Jupyter notebook 中查询此表:
select * from new_hvac
笔记:
因此,请确保您将参数传递给 python 脚本(.py 文件)而不是 python 笔记本。