修复pyhdfs无法获取hadoop文件的问题

1.pyhdfs命令

import pyhdfs
fs=pyhdfs.HdfsClient(hosts="172.10.10.2")
f=fs.open("/1.txt")

2.报错

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    f=fs.open("/test/1.txt")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pyhdfs.py", line 475, in open
    path, 'OPEN', expected_status=httplib.TEMPORARY_REDIRECT, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pyhdfs.py", line 391, in _get
    return self._request('get', *args, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pyhdfs.py", line 377, in _request
    _check_response(response, expected_status)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pyhdfs.py", line 809, in _check_response
    raise cls(status_code=response.status_code, **remote_exception)
pyhdfs.HdfsFileNotFoundException: File /test/1.txt not found.
>>> f=fs.open("/1.txt")
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1083, in request
    self._send_request(method, url, body, headers)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1128, in _send_request
    self.endheaders(body)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1079, in endheaders
    self._send_output(message_body)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 911, in _send_output
    self.send(msg)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 854, in send
    self.connect()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connection.py", line 166, in connect
    conn = self._new_conn()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connection.py", line 150, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000000042CB438>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='slave2', port=50075): Max retries exceeded with url: /webhdfs/v1/1.txt?op=OPEN&user.name=Administrator&namenoderpcaddress=172.10.10.2:9000&offset=0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000000042CB438>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    f=fs.open("/1.txt")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pyhdfs.py", line 477, in open
    metadata_response.headers['location'], stream=True, **self._requests_kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\adapters.py", line 508, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='slave2', port=50075): Max retries exceeded with url: /webhdfs/v1/1.txt?op=OPEN&user.name=Administrator&namenoderpcaddress=172.10.10.2:9000&offset=0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000000042CB438>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))

3.问题分析

在最后一行报错可以看到

HTTPConnectionPool(host='slave2', port=50075)

“slave2”,是我hadoop第二台子机的hostname,这说明已经连到了hadoop,只是没翻译为ip地址。

4.问题解决

把python所在机器的hosts加上hadoop的别名= =


参考文档:hadoop中常见的问题

阅读更多
个人分类: Python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭