Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 使用python创建或修改日期的Hdfs文件或文件夹_Python 2.7_Hdfs - Fatal编程技术网

Python 2.7 使用python创建或修改日期的Hdfs文件或文件夹

Python 2.7 使用python创建或修改日期的Hdfs文件或文件夹,python-2.7,hdfs,Python 2.7,Hdfs,为了查找在hdfs中创建文件或文件夹的天数,我使用了以下python代码 import datetime from datetime import datetime def get_days(directory, file) current_datetime = datetime.now() command = "hadoop fs -ls " + directory + file + " | tr -s ' ' | cut -d' ' -f6-7 | grep '^[0-9

为了查找在hdfs中创建文件或文件夹的天数,我使用了以下python代码

import datetime
from datetime import datetime

def get_days(directory, file)

    current_datetime = datetime.now()
    command = "hadoop fs -ls " + directory + file + " | tr -s ' ' | cut -d' ' -f6-7 | grep '^[0-9]' "
    status, output = shell_command(command)
        if status == 0:
            file_date = datetime.strptime(output, '%Y-%m-%d %H:%M')
            date_period = current_datetime - file_date
    return date_period.days
它抛出了以下错误

File "/usr/lib64/python2.7/_strptime.py", line 325, in _strptime (data_string, format)) 
ValueError: time data 'log4j:WARN No appenders could be found for logger (org.apache.hadoop.security.UserGroupInformation).\nlog4j:WARN Please initialize the log4j system properly.\nlog4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.\n2018-04-11 14:08' does not match format '%Y-%m-%d %H:%M'

我能想到的唯一原因是输出是一个推断类型Unicode的变量。有没有其他方法可以在python中找到Hdfs文件的修改日期或时间?请告知。也欢迎对上述代码进行任何更改。提前谢谢

以下代码适用于我:

import datetime
import commands
from datetime import datetime

def get_days(directory, file):    
    current_datetime = datetime.now()
    command = "hadoop fs -ls " + directory + file + " | tr -s ' ' | cut -d' ' -f6-7 | grep '^[0-9]' "
    (status, output) = commands.getstatusoutput(command)
        if status == 0:
            file_date = datetime.strptime(output, '%Y-%m-%d %H:%M')
            date_period = current_datetime - file_date
    return date_period.days

尝试WebHDFSAPI,您还可以读取集群的fs映像以获取元数据。您可以找到块或文件的所有详细信息及其修改日期。fs图像是二进制格式的,因此我们可以使用hdfs oiv命令(hdfs oiv–i/fsimage/fsimage\u00000000 xyz-->脱机图像查看器)访问它。您还可以使用命令“hdfs dfs-stat”获取修改的日期和一些其他元数据。谢谢您的回答。我将尝试fs image和-stat。我遇到的问题是,上面的代码在单元测试期间运行良好,但当我将其放入每天运行的工作流中时,几天后它开始出现上述错误。我找不到任何原因,所以我正在尝试一种方法来避免将控制台输出传递给“strptime”。嗨,Rahul,Hadoop fs-stat将日期和时间作为控制台输出,但我同样怀疑strptime函数在使用它时在部署后会抛出错误。你能打印“输出”吗并查看您是否获得了预期的输出,即正确的格式。从错误中,我们可以确定格式与预期不符。对我来说,它工作得很好。我还使用了“commands”包,而不是您的“shell\u command”方法