gpt4 book ai didi

python - Catch 子句不适用于 hive_context.read.json 函数

转载 作者:行者123 更新时间:2023-12-02 16:57:19 24 4
gpt4 key购买 nike

我有这个 PySpark 脚本:

from pyspark.sql import HiveContext
from pyspark.sql import SQLContext
from pyspark import SparkContext

sc = SparkContext.getOrCreate()
hive_context = HiveContext(sc)

sc.addFile("hdfs:///user/cloudera/2904/src/LogFunction.py")
import LogFunction

try:
df = hive_context.read.json("/user/cloudera/Projet/pareeam.json")
except IOError:
LogFunction.WarnLog("Nope")
....
....

这是我的 LogFunction.py:

import logging
from logging.handlers import RotatingFileHandler
from pyspark.sql import HiveContext
from pyspark.sql import SQLContext
from pyspark import SparkContext
sc = SparkContext.getOrCreate()
hive_context = HiveContext(sc)

df = hive_context.read.json("/user/cloudera/2904/param.json")
Path = df[df.column.isin("LogRep")].collect()[0][1]
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')

debug_handler = RotatingFileHandler(Path+ '/LogDebug.log', 'a', 1000000, 1)
debug_handler.setLevel(logging.DEBUG)
debug_handler.setFormatter(formatter)
logger.addHandler(debug_handler)
info_handler = RotatingFileHandler(Path+ '/LogInfo.log', 'a', 1000000, 1)
info_handler.setLevel(logging.INFO)
info_handler.setFormatter(formatter)
logger.addHandler(info_handler)
warning_handler = RotatingFileHandler(Path+ '/LogWarning.log', 'a', 1000000, 1)
warning_handler.setLevel(logging.WARNING)
warning_handler.setFormatter(formatter)
logger.addHandler(warning_handler)

error_handler = RotatingFileHandler(Path+ '/LogError.log', 'a', 1000000, 1)
error_handler.setLevel(logging.ERROR)
error_handler.setFormatter(formatter)
logger.addHandler(error_handler)

def WarnLog(a):
logger.warning(a)

无论如何,我确信我的日志记录正在运行,因为我已经尝试过其他错误(//除以零异常)。

但是这个例子似乎不是这样。在执行脚本时,我得到一个

py4j.protocol.Py4JJavaError: An error occurred while calling o31.json.
: java.io.FileNotFoundException: File hdfs://quickstart.cloudera:8020/user/cloudera/Projet/pareeam.json does not exist.

在终端上,但我的警告文件中没有任何内容。任何帮助为什么它没有发现错误?谢谢

最佳答案

您捕获了一个错误的异常。您的代码捕获 IOErrorDataFrameReader.json 抛出 py4j.protocol.Py4JJavaError(内部)和 pyspark.sql.utils.AnalysisException

将您的代码更改为

from pyspark.sql.utils import AnalysisException

try:
df = hive_context.read.json("/does/not/exist")
except AnalysisException:
LogFunction.WarnLog("Nope")

关于python - Catch 子句不适用于 hive_context.read.json 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com