首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用webhdfs列出HDFS目录内容?

如何使用webhdfs列出HDFS目录内容?
EN

Stack Overflow用户
提问于 2016-06-23 10:51:31
回答 1查看 6.3K关注 0票数 3

是否可以使用webhdfs检查HDFS中目录的内容?

这可以像hdfs dfs -ls通常所做的那样工作,但是可以使用webhdfs

如何使用Python2.6列出webhdfs目录?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-23 11:24:37

您可以使用LISTSTATUS动词。文档位于列出目录,在WebHDFS REST文档上可以找到以下代码:

对于curl,如下所示:

代码语言:javascript
复制
curl -i  "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=LISTSTATUS"

响应是一个FileStatuses JSON对象:

代码语言:javascript
复制
{
  "name"      : "FileStatuses",
  "properties":
  {
    "FileStatuses":
    {
      "type"      : "object",
      "properties":
      {
        "FileStatus":
        {
          "description": "An array of FileStatus",
          "type"       : "array",
          "items"      : fileStatusProperties
        }
      }
    }
  }
}

fileStatusProperties (用于items字段)具有以下JSON模式:

代码语言:javascript
复制
var fileStatusProperties =
{
  "type"      : "object",
  "properties":
  {
    "accessTime":
    {
      "description": "The access time.",
      "type"       : "integer",
      "required"   : true
    },
    "blockSize":
    {
      "description": "The block size of a file.",
      "type"       : "integer",
      "required"   : true
    },
    "group":
    {
      "description": "The group owner.",
      "type"       : "string",
      "required"   : true
    },
    "length":
    {
      "description": "The number of bytes in a file.",
      "type"       : "integer",
      "required"   : true
    },
    "modificationTime":
    {
      "description": "The modification time.",
      "type"       : "integer",
      "required"   : true
    },
    "owner":
    {
      "description": "The user who is the owner.",
      "type"       : "string",
      "required"   : true
    },
    "pathSuffix":
    {
      "description": "The path suffix.",
      "type"       : "string",
      "required"   : true
    },
    "permission":
    {
      "description": "The permission represented as a octal string.",
      "type"       : "string",
      "required"   : true
    },
    "replication":
    {
      "description": "The number of replication of a file.",
      "type"       : "integer",
      "required"   : true
    },
   "type":
    {
      "description": "The type of the path object.",
      "enum"       : ["FILE", "DIRECTORY"],
      "required"   : true
    }
  }
};

您可以使用pywebhdfs处理Python中的文件名,如下所示:

代码语言:javascript
复制
import json
from pprint import pprint
from pywebhdfs.webhdfs import PyWebHdfsClient

hdfs = PyWebHdfsClient(host='host',port='50070', user_name='hdfs')  # Use your own host/port/user_name config

data = hdfs.list_dir("dir/dir")  # Use your preferred directory, without the leading "/"

file_statuses = data["FileStatuses"]
pprint file_statuses   # Display the dict

for item in file_statuses["FileStatus"]:
    print item["pathSuffix"]   # Display the item filename

与对每个对象进行print不同,您实际上可以根据需要使用这些项。file_statuses的结果只是一个Python dict,所以它可以像任何其他dict一样使用,只要您使用正确的键。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37989520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档