python连接hdfs篇一

1)windows和虚拟机需互通(ping通即可)

1、关闭windows防火墙、虚拟机的防火墙

windows版:
在这里插入图片描述

centos端:

// 彻底关闭防火墙
service iptables stop
chkconfig iptables off
chkconfig --list | grep iptables

在这里插入图片描述

2、修改centos 的ip、网关设置

// 修改eht0的IP
vi /etc/sysconfig/network-scripts/ifcfg-eth0
//把里面的原有内容删除,添加内容
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.168.50.65
NETMASK=255.255.255.0
GATEWAY=192.168.50.2
DNS1=114.114.114.114
DNS2=8.8.8.8
网关和IP都在同一50//重启网络
service network restart
service network status
ifconfig
//如果修改IP报错,需要关闭NetworkManager
chkconfig NetworkManager off
service NetworkManager stop

3、修改虚拟机网络适配器,自动获取改为默认

在这里插入图片描述

4、windows端ping虚拟机ip成功
如果还不能ping通检查,检查虚拟机中hdfs问题

5、start-all.sh 启动hdfs
jps检查Namenode、dataNode、SeconaryNamenode节点是否启动成功

在这里插入图片描述

6.centos端ping windows 成功
在这里插入图片描述

2)python连接操作hdfs

  1. Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

解决办法如下:
在d盘新建一个目录,比如d:\hadoop2.6.5
然后在该目录下新建文件夹bin
把winutils.exe放到bin文件夹中
然后配置环境变量,名称为HADOOP_HOME
环境变量的值为 d:\hadoop2.6.5
在这里插入图片描述

2.requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘hmaster’, port=50075): Max retries exceeded with url: /webhdfs/v1/test.txt?op=OPEN&namenoderpcaddress=hMaster:9000&offset=0 (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x00000000035BAB38>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed’,))

说明程序没有将主机名映射到正确的ip

解决方法:在运行python程序的主机的hosts文件中加上主机名和ip的映射,对于我所使用的windows系统,hosts文件的路径是C://Windows/System32/drivers/etc/hosts,在文件末尾加上

ip 主机名
以本文的情况为例,则是

在这里插入图片描述

3)python操作hdfs代码

# -*- coding: utf-8 -*-
# python连接hdfs,修改host文件,把ip和host写入
import pyhdfs

# 文件上传
def put_to_hdfs(client,local_path,hdfs_path):

    try:
        client.copy_from_local(local_path,hdfs_path)
        print("success")
    except IOError:
        print("Error: 没有找到文件或读取文件失败")
    else:
        print("内容写入文件成功")

# 创建目录
def mk(client,hdfs_path) :
    try:
        client.mkdirs(hdfs_path)
        print("success")
    except IOError:
        print("Error: 创建失败",IOError)
    else:
        print("创建目录成功")

# 显示当前文件目录
def showlist(client,hdfs_path):
    try:
        return client.listdir(hdfs_path)
    except IOError:
        print("Error: ", IOError)

# 下载文件
def downfile(client,hdfs_path,localpath):
    try:
        client.copy_to_local(hdfs_path,localpath)
        print("success")
    except IOError:
        print("Error: ", IOError)
    else:
        print("下载成功")



if __name__ == '__main__':
    #hdfs端口9000,图形界面端口50070
    client = pyhdfs.HdfsClient(hosts="192.168.50.65,9000",user_name="root")
    # 打开一个远程节点上的文件,返回一个HTTPResponse对象

    print (client.get_home_directory())
    # 返回可用的namenode节点
    print (client.get_active_namenode())

    #创建/bbb/s目录
    mk(client,"/bbb/s")
    #上传word.txt到hdfs中mr/word8.txt"
    put_to_hdfs(client,"d:/word.txt","/mr/word8.txt")
    #显示/mr下目录
    c=showlist(client,'/mr')
    print(c)
    #下载hdfs中/mr/word2.txt到d:/woord.txt
    downfile(client,'/mr/word2.txt','d:/woord.txt')

4)关闭hdfs

//停止
stop-all.sh

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值