将 Jar 添加到独立的 pyspark [英] Add Jar to standalone pyspark

查看:57
本文介绍了将 Jar 添加到独立的 pyspark的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在启动一个 pyspark 程序:

I'm launching a pyspark program:

$ export SPARK_HOME=
$ export PYTHONPATH=$SPARK_HOME/python:$SPARK_HOME/python/lib/py4j-0.9-src.zip
$ python

和py代码:

from pyspark import SparkContext, SparkConf

SparkConf().setAppName("Example").setMaster("local[2]")
sc = SparkContext(conf=conf)

如何添加 jar 依赖项,例如 Databricks csv jar?使用命令行,我可以像这样添加包:

How do I add jar dependencies such as the Databricks csv jar? Using the command line, I can add the package like this:

$ pyspark/spark-submit --packages com.databricks:spark-csv_2.10:1.3.0 

但我没有使用任何这些.该程序是不使用 spark-submit 的更大工作流的一部分我应该能够运行我的 ./foo.py 程序,它应该可以正常工作.

But I'm not using any of these. The program is part of a larger workflow that is not using spark-submit I should be able to run my ./foo.py program and it should just work.

  • 我知道您可以为 extraClassPath 设置 spark 属性,但是您必须将 JAR 文件复制到每个节点?
  • 尝试过 conf.set("spark.jars", "jar1,jar2") ,但在 py4j CNF 异常中也不起作用

推荐答案

2021-01-19 更新

这里有很多方法(设置 ENV 变量,添加到 $SPARK_HOME/conf/spark-defaults.conf 等...)其他答案已经涵盖了这些.我想为那些特别想从 Python 脚本Jupyter Notebook 中执行此操作的人添加一个答案.

There are many approaches here (setting ENV vars, adding to $SPARK_HOME/conf/spark-defaults.conf, etc...) other answers already cover these. I wanted to add an answer for those specifically wanting to do this from within a Python Script or Jupyter Notebook.

当你创建 Spark 会话时,你可以添加一个 .config() 来拉入特定的 Jar 文件(在我的例子中我想要加载 Kafka 包):

When you create the Spark session you can add a .config() that pulls in the specific Jar file (in my case I wanted the Kafka package loaded):

spark = SparkSession.builder.appName('my_awesome')\
    .config('spark.jars.packages', 'org.apache.spark:spark-sql-kafka-0-10_2.12:3.0.1')\
    .getOrCreate()

使用这行代码,我不需要做任何其他事情(没有 ENV 或 conf 文件更改).

Using this line of code I didn't need to do anything else (no ENVs or conf file changes).

  • 注意 1:JAR 文件将动态下载,您无需手动下载.
  • 注意 2:确保版本与您想要的匹配,因此在上面的示例中,我的 Spark 版本是 3.0.1,所以我在末尾添加了 :3.0.1.
  • Note 1: The JAR file will dynamically download, you don't need to manually download it.
  • Note 2: Make sure the versions match what you want, so in the example above my Spark version is 3.0.1 so I have :3.0.1 at the end.

这篇关于将 Jar 添加到独立的 pyspark的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆