在生产环境中部署时如何读取项目中的 db.properties 文件或任何其他 conf 文件......在 scala Spark Yarn HDFS 中......
我收到此错误...
24/05/09 16:34:32 INFO Client:
client token: N/A
diagnostics: User class threw exception: java.io.FileNotFoundException: File file:/app/hadoop/yarn/local/usercache/user/appcache/application_1715046519048_2131/container_e37_1715046519048_2131_02_000001/user/db.properties does not exist
at org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:641)
at org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:930)
at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:631)
首先我保存在本地,它不起作用,然后我保存在 hdfs 目录中
这是我的 scala 代码:-
sparkContext.addFile("hdfs://user/db.properties")
val propertiesFile = SparkFiles.get("hdfs://user/db.properties")
val properties = new Properties()
properties.load(new FileInputStream(propertiesFile))
这是我用来运行 Spark 应用程序的脚本。我的脚本中有必要的详细信息,但这是文件的详细信息。
[spark-submit.sh]
--files hdfs://user/db.properties \
而且我将 db.properties 保留在 src 文件夹之外,如下所示:-
project-root/
├── db.properties
│
│
│
└── src/
└── main/
└── scala/
└── mysqlTohive.scala
在生产中,我将这样的文件保存在不同的路径中:
-
path-home/user/spark/ [where below files are there]
-->mysqlTohive.scala
-->db.properties
听起来该文件实际上不是您告诉程序查看的位置。如果您有权访问 HDFS 实用程序,例如,如果您可以通过 SSH 连接到集群,我将使用 try to
bin/hdfs dfs -ls <path>
来探索路径。否则,您可以尝试列出 Spark 程序中的文件:
val fs = FileSystem.get(sc.hadoopConfiguration)
fs.listStatus(new Path("hdfs://"))
如果您实际上不在HDFS中保留文件的副本,但它在执行
spark-submit
的地方可以在本地使用(例如,实际上引导到集群中的驱动程序节点上),那么您可以简单地使用本地、绝对或相对路径(即没有 hdfs://
前缀)
properties.load(new java.io.FileInputStream(SparkFiles.get("db.properties"))) // or /path-home/user/spark/ etc.