气流中的pig_hook对python3不起作用

时间:2018-06-01 23:16:20

标签: python-3.5 airflow

使用Python 3.5.2,airflow 1.9.0

尝试设置一个猪钩,看看这里的文档: https://github.com/apache/incubator-airflow/blob/master/airflow/hooks/pig_hook.py

遵循第50-52行的例子

  File "python3.5/site-packages/airflow/hooks/pig_hook.py", line 53, in run_cli
f.write(pig)
  File "python3.5/tempfile.py", line 622, in func_wrapper
return func(*args, **kwargs)
  TypeError: a bytes-like object is required, not 'str'

给出以下错误:

    >>> ph = PigCliHook()
    >>> result = ph.run_cli("ls /;".encode('utf-8'))
    >>> ("hdfs://" in result)

如果更新为以:

运行
  File "python3.5/site-packages/airflow/hooks/pig_hook.py", line 74, in run_cli
stdout += line
  TypeError: Can't convert 'bytes' object to str implicitly

错误变为:

with NamedTemporaryFile(dir=tmp_dir,'w') as f:

稍后在同一个pig_hook.py文件中,它确实假定该字段的字符串类型,所以我不认为将输入作为字节对象传递是正确的。

我认为导致问题的对象是NamedTemporaryFile(来自pig_hook.py中的第52行),默认情况下在模式' w + b'中打开。如下文所述:

https://bugs.python.org/issue29245

但如果我将pig_hook.py中的第53行更改为:

with NamedTemporaryFile(dir=tmp_dir, mode='w') as f:

  File "python3.5/site-packages/airflow/hooks/pig_hook.py", line 53, in run_cli
f.write(pig)
  File "python3.5/tempfile.py", line 622, in func_wrapper
return func(*args, **kwargs)
  TypeError: a bytes-like object is required, not 'str'

它仍然期望一个字节数组导致相同的错误:

{{1}}

有谁知道如何解决这个问题?我似乎无法在使用字符串的模式下打开NamedTemporaryFile,其余代码假定为字符串。

1 个答案:

答案 0 :(得分:0)

调用后发现错误:

>>> ph = PigCliHook()
>>> result = ph.run_cli("ls /;".encode('utf-8'))
>>> ("hdfs://" in result)

File "python3.5/site-packages/airflow/hooks/pig_hook.py", line 74, in run_cli
stdout += line
TypeError: Can't convert 'bytes' object to str implicitly

不是来自我自己的输入,而是来自日志记录。所以我补充道:

line = line.decode('utf-8')

到pig_hook的第75行,现在看起来工作得很好。