如何将文件从本地笔记本电脑放到远程HDFS?

时间:2017-10-20 18:41:24

标签: python hadoop hdfs

我有Hadoop 2.8.1

配置hdfs-site.xml

<configuration>
# Add the following inside the configuration tag
<property>
        <name>dfs.data.dir</name>
        <value>/app/dfs/name/data</value>
        <final>true</final>
</property>
<property>
        <name>dfs.name.dir</name>
        <value>/app/dfs/name</value>
        <final>true</final>
</property>
<property>
        <name>dfs.replication</name>
        <value>1</value>
</property>
<property>
    <name>dfs.webhdfs.enabled</name>
    <value>true</value>
</property>
</configuration>

通过python找到了这段代码

from pywebhdfs.webhdfs import PyWebHdfsClient
from pprint import pprint

hdfs = PyWebHdfsClient(host='hadoop01',port='50070', user_name='hadoop')  # your Namenode IP & username here

my_data = "01010101010101010101010101010101000111 Example DataSet"
my_file = '/examples/myfile.txt'
hdfs.create_file(my_file, my_data.encode('utf-8'))

此变体有效。 但我想把已准备好的文件放到远程HDFS上。

试图写

with open("C:\\Downloads\\Demographic_Statistics_By_Zip_Code.csv") as file_data:
     print(file_data)

BUT文件没有PUT到HDFS。 只返回

<_io.TextIOWrapper name='C:\\Downloads\\Demographic_Statistics_By_Zip_Code.csv' mode='r' encoding='cp1251'>

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如何使用hdfs cli? 请参阅copyFromLocal或从此链接中输入命令:

https://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/FileSystemShell.html