简体   繁体   English

在 Yarn Cluster 模式下执行的 Spark Scala 代码中读取本地/linux 文件

[英]Read local/linux files in Spark Scala code executing in Yarn Cluster Mode

How to access and read local file data in Spark executing in Yarn Cluster Mode.如何在 Yarn Cluster 模式下访问和读取 Spark 中执行的本地文件数据。

local/linux file: /home/test_dir/test_file.csv

spark-submit --class "" --master yarn --deploy_mode cluster --files /home/test_dir/test_file.csv test.jar 

Spark code to read csv:读取 csv 的 Spark 代码:

val test_data = spark.read.option("inferSchema", "true").option("header", "true).csv("/home/test_dir/test_file.csv")
val test_file_data = spark.read.option("inferSchema", "true").option("header", "true).csv("file:///home/test_dir/test_file.csv")

The above sample spark-submit is failing with local file not-found error (/home/test_dir/test_file.csv)上述示例 spark-submit 失败并出现本地文件未找到错误 (/home/test_dir/test_file.csv)

Spark by defaults check for file in hdfs:// but my file is in local and should not be copied into hfds and should read only from local file system. Spark 默认检查 hdfs:// 中的文件,但我的文件在本地,不应复制到 hfds 中,应仅从本地文件系统读取。

Any suggestions to resolve this error?有什么建议可以解决这个错误吗?

Using file:// prefix will pull files from the YARN nodemanager filesystem, not the system from where you submitted the code.使用file://前缀将从 YARN 节点管理器文件系统中提取文件,而不是从您提交代码的系统中提取文件。

To access your --files use csv("#test_file.csv")要访问您的--files使用csv("#test_file.csv")

should not be copied into hdfs不应复制到 hdfs

Using --files will copy the files into a temporary location that's mounted by the YARN executor and you can see them from the YARN UI使用--files会将文件复制到由 YARN 执行程序挂载的临时位置,您可以从 YARN UI 中看到它们

Below solution worked for me:以下解决方案对我有用:

local/linux file: /home/test_dir/test_file.csv

spark-submit --class "" --master yarn --deploy_mode cluster --files /home/test_dir/test_file.csv test.jar

To access file passed in spark-submit:要访问在 spark-submit 中传递的文件:

import scala.io.Source
val lines = Source.fromPath("test_file.csv").getLines.toString

Instead of specifying complete path, specify only file name that we want to read.不要指定完整路径,而只指定我们要读取的文件名。 As spark already takes copy of file across nodes, we can access data of file with only file name.由于 spark 已经跨节点复制文件,我们可以仅使用文件名访问文件数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SPARK YARN群集模式下使用Scala代码时,“用户未初始化Spark上下文”错误 - “User did not initialize spark context” Error when using Scala code in SPARK YARN Cluster mode 无法在Spark Kubernetes集群模式下读取本地文件 - Unable to read local files in spark kubernetes cluster mode 使用Spark in Cluster模式将文件写入本地系统 - Writing files to local system with Spark in Cluster mode 为什么这个 Spark 代码在本地模式下工作,而不在集群模式下工作? - Why this Spark code works in local mode but not in cluster mode? 在 spark 中使用 scala 将预测结果保存到 HDFS 表中在纱线集群模式下非常慢 - Use scala in spark to save prediction result into HDFS table is VERY slow on yarn-cluster mode 纱线群集模式下Spark作业的ClassNotFoundException - ClassNotFoundException for Spark job on Yarn-cluster mode Spark提交在纱线集群模式下截断参数 - Spark submit truncates arguments in yarn cluster mode 处于集群 (YARN) 模式时 Spark 上的 Kerberos 问题 - Kerberos issue on Spark when in cluster (YARN) mode 如何将配置文件添加到在 YARN-CLUSTER 模式下运行的 Spark 作业? - How can I add configuration files to a Spark job running in YARN-CLUSTER mode? 在 spark 代码中使用配置文件管理 conf.setMaster() 以自动设置本地或纱线集群 - In spark code manage conf.setMaster() using a config file to autoset local or yarn-cluster
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM