1. 程式人生 > >修復pyhdfs無法獲取hadoop檔案的問題

修復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中常見的問題