IDC:一季度全球传统PC出货量同比增长55.2%

  发布时间:2026-10-13   作者:玩站小弟   我要评论
IDC:一季度全球传统PC出货量同比增长55.2%。

上次介绍了HDFS的简单操作,今天进入HDFS中的Java和Python的API操作,后面可能介绍Scala的相关的。

在讲Java API之前介绍一下使用的IDE——IntelliJ IDEA ,我本人使用的是2020.3 x64的社区版本。

Java API

创建maven工程,关于Maven的配置,在IDEA中,Maven下载源必须配置成阿里云。

在对应的D:apache-maven-3.8.1-binapache-maven-3.8.1confsettings.xml需要设置阿里云的下载源。

下面创建maven工程,添加常见的依赖

添加hadoop-client依赖,版本最好和hadoop指定的一致,并添加junit单元测试依赖。

 

            org.apache.hadoop         hadoop-common         3.1.4               org.apache.hadoop         hadoop-hdfs         3.1.4             org.apache.hadoop       hadoop-client       3.1.4             junit       junit       4.11      HDFS文件上传

在这里编写测试类即可,新建一个java文件:main.java

这里的FileSyste一开始是本地的文件系统,需要初始化为HDFS的文件系统

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.junit.Test; import java.net.URI; public class main {      @Test     public void testPut() throws Exception {         //   获取FileSystem类的方法有很多种,这里只写一种(比较常用的是使URI)         Configuration configuration = new Configuration();         // user是Hadoop集群的账号,连接端口默认9000         FileSystem fileSystem = FileSystem.get(                 new URI("hdfs://192.168.147.128:9000"),                 configuration,                 "hadoop");         // 将f:/stopword.txt 上传到 /user/stopword.txt         fileSystem.copyFromLocalFile(                 new Path("f:/stopword.txt"), new Path("/user/stopword.txt"));         fileSystem.close();     } } 

在对应的HDFS中,就会看见我刚刚上传的机器学习相关的停用词。


HDFS文件下载

由于每次都需要初始化FileSystem,比较懒的我直接使用@Before每次加载。

HDFS文件下载的API接口是copyToLocalFile,具体代码如下。

@Test public void testDownload() throws Exception {     Configuration configuration = new Configuration();     FileSystem fileSystem = FileSystem.get(             new URI("hdfs://192.168.147.128:9000"),             configuration,             "hadoop");     fileSystem.copyToLocalFile(             false,             new Path("/user/stopword.txt"),             new Path("stop.txt"),             true);     fileSystem.close();     System.out.println("over"); }  Python API

下面主要介绍hdfs,参考:https://hdfscli.readthedocs.io/

我们通过命令pip install hdfs安装hdfs库,在使用hdfs前,使用命令hadoop fs -chmod -R 777 / 对当前目录及目录下所有的文件赋予可读可写可执行权限。

>>> from hdfs.client import Client >>> #2.X版本port 使用50070  3.x版本port 使用9870 >>> client = Client('http://192.168.147.128:9870')   >>> client.list('/')   #查看hdfs /下的目录 ['hadoop-3.1.4.tar.gz'] >>> client.makedirs('/test') >>> client.list('/') ['hadoop-3.1.4.tar.gz', 'test'] >>> client.delete("/test") True >>> client.download('/hadoop-3.1.4.tar.gz','C:\Users\YIUYE\Desktop') 'C:\Users\YIUYE\Desktop\hadoop-3.1.4.tar.gz' >>> client.upload('/','C:\Users\YIUYE\Desktop\demo.txt') >>> client.list('/') '/demo.txt' >>> client.list('/') ['demo.txt', 'hadoop-3.1.4.tar.gz'] >>> # 上传demo.txt 内容:Hello   hdfs >>> with client.read("/demo.txt") as reader: ...          print(reader.read()) b'Hello  hdfs ' 

相对于Java API,Python API连接实在简单。

 

  • Tag:

相关文章

  • 深度学习撞墙?LeCun与Marcus到底谁捅了马蜂窝

    今天的主角,是一对AI界相爱相杀的老冤家: Yann LeCun和Gary Marcus 在正式讲述这一次的「新仇」之前,我们先来回顾一下,两位大神的「旧恨」。 LeCun与Marcus之争 Face
    2026-12-01
  • AIOps如何转变IT管理

    随着全球努力应对冠状病毒疫情危机带来的影响,行业组织正在寻找方法来更好地管理竞争网络资源、不断增长的用户需求、复杂的故障排除挑战、数字化转型计划以及新技术等。组织一直要求IT部门少花钱多办事,如今,I
    2026-12-01
  • 标签: 软银 英伟达 孙正义 愿景基金

    云梦级11月11日云梦级消息,软银集团最新公布的季度财报显示,受益于科技股强势反弹,其持有的英伟达和英特尔等公司股权价值显著提升,意外实现净利润大幅跃升。这一表现进一步巩固了集团创始人孙正义持续推进人
    2026-12-01
  • 阿里云发布通义千问2.5,性能全面赶超GPT-4 Turbo

    5月9日消息,阿里云正式发布通义千问2.5,模型性能全面赶超GPT-4 Turbo,声称地表最强中文大模型。同时,通义千问最新开源的1100亿参数模型在多个基准测评收获最佳成绩,超越Meta的Llam
    2026-12-01
  • Anthropic发布Claude Sonnet 4.5:编码能力登顶榜首,实验性功能Imagine预演AI原生界面时代

    Anthropic在深夜突然发布Claude Sonnet4.5版本,这次更新不仅在技术指标上实现全面提升,更通过实验性功能Imagine with Claude展示了AI实时生成软件界面的创新方向。
    2026-12-01
  • 标签: 连连数字 章征宇 沈恩光 孙大利

    云梦级3月26日云梦级消息,连连数字(股票代码:“2598”)今日发布截至2025年12月31日的财报,财报显示,连连数字2025年营收为17.34亿元,较上年同期的13.15亿元增长31.9%。 连
    2026-12-01

最新评论