当前位置: 首页 > article >正文

Python 操作大数据使用 Hadoop

参考:https://blog.csdn.net/wuShiJingZuo/article/details/135620018

from hdfs import InsecureClient
hdfs_client = InsecureClient("http://localhost:9000", user="xiaokkk")
# 测试连接
print(hdfs_client.status("/"))
{'accessTime': 0, 'blockSize': 0, 'childrenNum': 9, 'fileId': 16385, 'group': 'supergroup', 'length': 0, 'modificationTime': 1725326811876, 'owner': 'xiaokkk', 'pathSuffix': '', 'permission': '755', 'replication': 0, 'snapshotEnabled': True, 'storagePolicy': 0, 'type': 'DIRECTORY'}
# 查看目录
files_and_dirs = hdfs_client.list("/")
print(files_and_dirs)
['apps', 'backup', 'data', 'ks', 'logs', 'tmp', 'user']
# 上传本地文件到HDFS
local_file_path = "/Users/xiaokkk/Desktop/local_projects/hadoop/input.txt"
hdfs_file_path = "/data/input.txt"

hdfs_client.upload(hdfs_file_path, local_file_path)
print(f"File {local_file_path} uploaded to {hdfs_file_path}")
File /Users/xiaokkk/Desktop/local_projects/hadoop/input.txt uploaded to /data/input.txt
# 下载HDFS文件到本地
hdfs_client.download(hdfs_file_path, "/Users/xiaokkk/Desktop/local_projects/hadoop/test_download.txt")
# 删除HDFS文件
client.delete('/path/to/your/file', recursive=False)  # 如果文件是一个目录并且包含文件,则需要设置
# 连接到HDFS
from hdfs import InsecureClient
client = InsecureClient("http://localhost:9000", user="xiaokkk")
# 指定HDFS上的文件路径
hdfs_path = '/data/input.txt'
# 打开HDFS上的文件
with client.read(hdfs_path, encoding='utf-8') as reader:
    # 读取文件内容
    content = reader.read()
    print(content)

# # 对文件内容进行处理,例如统计行数
# line_count = content.count('\n')
# print(f"文件 {hdfs_path} 包含 {line_count + 1} 行")
# 
# # 如果你想处理每一行,可以这样做
# for line in content.splitlines():
#     # 处理每一行
#     print(line)
Hello world, this is a simple example of a text file.
This file will be used to demonstrate how the word count works.
Every word will be counted, and thpython word_count.py input.txt another_input.txt > output.txte results will show the frequency of each word.

http://www.kler.cn/news/294118.html

相关文章:

  • Unity3D在2D游戏中获取触屏物体的方法
  • 做运营,发布时间很重要
  • Android Studio更新代码可正常运行但IDE报红
  • 【保姆级教程】使用 PyTorch 自定义卷积神经网络(CNN) 实现图像分类、训练验证、预测全流程【附数据集与源码】
  • 【量化部署】AWQ in MLSys 2024
  • 如何查找 Docker 容器的 IP 地址 ?
  • #include <iostream>介绍
  • Linux日志-journal日志
  • 【uni-app】通过 HBuilderX 创建 uni-app vue3项目
  • 2024年架构设计师论文-“模型驱动架构设计方法及其应用”
  • 【MATLAB】矩阵的合并
  • 编程技术开发105本经典书籍推荐分享
  • iOS——线程安全、线程同步与线程通信
  • cmd 常用命令总结
  • ES中对于时间聚合的一些总结
  • 集合及映射
  • Django项目搭建
  • Ext JS主要特点有哪些?
  • [米联客-XILINX-H3_CZ08_7100] FPGA程序设计基础实验连载-37 LVDS信号环路测试
  • 爬取图片保存为pdf
  • 使用Java通过SSH协议在两个远程服务器之间传输文件
  • 联众优车持续加大汽车金融服务投入与创新,赋能汽车消费新生态
  • 2024年会声会影哪个版本最新版
  • 【生日视频制作】奥迪A8提车交车仪式AE模板修改文字软件生成器教程特效素材【AE模板】
  • 【MATLAB源码-第160期】基于matlab的胡桃夹子优化算法(NOA)无人机三维路径规划,输出做短路径图和适应度曲线
  • Linux操作系统:Docker版本更新
  • yarn create vite时报错error Error: EPERM: operation not permitted, mkdir
  • css radius
  • 剪映剪辑影视视频字幕声音批量自动对齐教程
  • 系统找不到指定的文件怎么解决?