python3操作hdfs

    技术2023-08-13  79

    我们要先安装一下

    pip install hdfs from hdfs.client import Client client = Client("http://xxx:50070/",root="/") lists = client.list(hdfs_path="/",status=True) print("hdfs中的目录为:", lists) print(client.status(hdfs_path="/py_test_demo",strict=True)) print("根目录下的文件数量为:", client.checksum(hdfs_path="/py_test_demo/tototo9.json"))

    上传

    def callback(filename, size): print(filename, "完成了一个chunk上传", "当前大小:", size) if size == -1: print("文件上传完成") client.upload(hdfs_path="/py_test_demo", local_path="./tototo9.json", chunk_size=2 << 19, progress=callback, cleanup=True)

    这里遇到一个权限问题,报错信息:

    hdfs.util.HdfsError: Permission denied: user=dr.who, access=WRITE, inode="/py_test_demo":root:supergroup:drwxr-xr-x

    我们这里的版本用的是免费版的 我们去修改 /usr/local/hadoop-3.1.3/etc/hadoop 下的hdfs-site.xml 中设置权限为false

    然后这个问题就ok了。

    接下来又有一个问题,在win本地运行时候,隔一段时间就报错网络异常

    socket.gaierror: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Failed to establish a new connection: [Errno 11001] getaddrinfo failed urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001B2DBDAEDD8>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

    这个错就是win没有识别到hadoop地址信息,所以我们需要修改一下win本地host文件,使其映射到hadoop集群上。

    host地址:

    C:\Windows\System32\drivers\etc

    我们把集群信息加上,加到文件最下面就可以了

    xxx.xxx xmhl xxx.xxx myhbase xxx.xxx xmhl30

    Processed: 0.010, SQL: 9