Python对HDFS的操作(一)

HDFS

hdfs的定义:

  Hadoop的分布式文件系统(HDFS)被设计成适合运行通用硬件上的分布式文件系统,它和现有的分布式文件系统有很多的共同点。但同时,它和其它的分布式文件系统的区别也是很明显的,hdfs是一个高容错性的系统,适合部署在廉价的机器上。HDFS能提供高吞吐量的数据访问,非常适合大规模数据集上使用。HDFS放宽了一部分POSIX(https://baike.baidu.com/item/POSIX/3792413?fr=aladdin)约束,来实现流式读取文件系统数据的目的。HDFS在最开始是作为Apache Nutch搜索引擎项目的基础架构而开发的。HDFS现在是Apache Hadoop Core项目的一部分,这个项目的地址是:http://hadoop.apache.org/core/

安装:

  因为这里介绍的是python版本的使用,所以需要安装相应的包:

 pip install hdfs

 基础使用方法:

  Client---创建集群链接:

from hdfs import *
client=Client("https://hdfsip:50070")

  参数说明:

    class  hdfs.client.Client(urlroot=Noneproxy=Nonetimeout=Nonesession=None)

      url: ip:端口

        root:指定hdfs根目录

      proxy:制定登陆用户的身份

      timout: 设置超时时间

      session:(官方解释,没弄明白具体意思,也暂时没有用到这个参数,等用到之后再进行补充)

  dir---查看Client所有支持的方法:

>>> dir(client)
>>>['__class__''__delattr__''__dict__''__dir__''__doc__''__eq__''__format__''__ge__''__getattribute__''__gt__',
  '__hash__' '__init__' '__le__' '__lt__' '__module__' '__ne__' '__new__' '__reduce__' '__reduce_ex__' '__registry__' ,
    '__repr__' '__setattr__' '__sizeof__' '__str__' '__subclasshook__' '__weakref__' '_append' '_create' '_delete' ,
    '_get_content_summary' '_get_file_checksum' '_get_file_status' '_get_home_directory' '_list_status' '_mkdirs' '_open' ,
    '_proxy' '_rename' '_request' '_session' '_set_owner' '_set_permission' '_set_replication' '_set_times' '_timeout' ,
  'checksum' 'content' 'delete' 'download' 'from_options' 'list' 'makedirs' 'parts' 'read' 'rename' 'resolve' 'root' ,
    'set_owner' 'set_permission' 'set_replication' 'set_times' 'status' 'upload' ,
    'url' 'walk' 'write' ]

  status---获取指定路径的具体信息:

>>> client.status("/")

{'accessTime': 0, 'pathSuffix': '', 'group': 'supergroup', 'type': 'DIRECTORY', 'owner': 'root', 'childrenNum': 4, 'blockSize': 0,

 'fileId': 16385, 'length': 0, 'replication': 0, 'storagePolicy': 0, 'modificationTime': 1473023149031, 'permission': '777'}

  参数说明:

    status(hdfs_path,strict=True)

    hdfs_path:就是hdfs的路径

    strict:当设置为True时,hdfs的路径不存在时,返回异常信息

       当设置为False时,hdfs的路径不存在时,返回None

  list---获取指定路径的子目录信息:

client.list("/")
["test01","test02","test03"]

       参数说明:

    list(hdfs_path,status=False)

    hdfs_path: hdfs的路径

    status:为True时,同时反hi子目录的状态信息,默认为False

  makedirs--创建目录

client.makedirs("test06")

  参数说明:

    makedirs(hdfs_path,permission=None)

    hdfs_path: 要创建目录

    permission:对创建的文件夹设置权限

  rename—重命名

>>> client.rename("/test","/new_name")
>>> client.list("/")
['file', 'gyt', 'hbase', 'new_name', 'tmp']

   参数说明:

    rename(hdfs_path, local_path)

 delete—删除

>>> client.list("/")
['file', 'gyt', 'hbase', 'new_name', 'tmp']
>>> client.delete("/new_name")
True
>>> client.list("/")
['file', 'gyt', 'hbase', 'tmp']
  参数说明:
    delete(hdfs_path,recursive=False)

    recursive:删除文件和其子目录,设置为False如果不存在,则会抛出异常,默认为False

 upload——上传数据

>>> client.list("/")
[u'hbase', u'test']
>>> client.upload("/test","/opt/bigdata/hadoop/NOTICE.txt")
'/test/NOTICE.txt'
>>> client.list("/")
[u'hbase', u'test']
>>> client.list("/test")
[u'NOTICE.txt']

  参数说明:

    upload(hdfs_path,local_path,overwrite=False,n_threads=1,temp_dir=None,

    chunk_size=65536,progress=None,cleanup=True,**kwargs)

    overwrite:是否是覆盖性上传文件

    n_threads:启动的线程数目

    temp_dir:当overwrite=true时,远程文件一旦存在,则会在上传完之后进行交换

    chunk_size:文件上传的大小区间

    progress:回调函数来跟踪进度,为每一chunk_size字节。它将传递两个参数,文件上传的路径和传输的字节数。一旦完成,-1将作为第二个参数

    cleanup:如果在上传任何文件时发生错误,则删除该文件

  download——下载

>>> client.download("/test/NOTICE.txt","/home")
'/home/NOTICE.txt'
>>> import os
>>> os.system("ls /home")
lost+found  NOTICE.txt  thinkgamer
0

  参数说明:

    download(hdfs_path,local_path,overwrite=False,n_threads=1,temp_dir=None,**kwargs)

    和上传的参数一样

  read——读取文件

>>> with client.read("/test/NOTICE.txt") as reader:
...     print reader.read()
...
This product includes software developed by The Apache Software
Foundation (https://www.apache.org/).
 
>>>

参数说明:

  read(*args,**kwds)

  hdfs_path:hdfs路径

  offset:设置开始的字节位置

  length:读取的长度(字节为单位)

  buffer_size:用于传输数据的字节的缓冲区的大小。默认值设置在HDFS配置。

  encoding:制定编码

  chunk_size:如果设置为正数,上下文管理器将返回一个发生器产生的每一chunk_size字节而不是一个类似文件的对象

  delimiter:如果设置,上下文管理器将返回一个发生器产生每次遇到分隔符。此参数要求指定的编码。

  progress:回调函数来跟踪进度,为每一chunk_size字节(不可用,如果块大小不是指定)。它将传递两个参数,文件上传的路径和传输的字节数。称为一次与- 1作为第二个参数。

我在测试的时候遇见这个错误:
hdfs.util.HdfsError: Permission denied: user=dr.who, access=WRITE, inode="/test":root:supergroup:drwxr-xr-x

 找到的解决办法是:

  解决办法是:在配置文件hdfs-site.xml中加入
<property>
  <name>dfs.permissions</name>
  <value>false</value>
</property>

 重启集群

有点长了,webhdfs在下一篇中写

猜你喜欢

转载自www.cnblogs.com/songdongdong6/p/9846837.html
今日推荐