Hadoop 的版本是 3.1.1

1. 启动 Hadoop 服务

$ start-all.sh

2. 新建 IDEA 的 Maven 项目

2.1 选中 Maven,Project SDK 选择为 1.8,再点击 Next

2.2 填写好 GroupId,ArtifactId 后,点击 Next

2.3 点击 Finish

3. 修改 Target bytecode version

打开 Setting,选中 Build, Execution, Deployment -> Compiler -> java,将 Target bytecode version 改为 1.8 或 8。

确认这几个配置下的 jdk 版本都为 1.8

4. 导入需要的 jar 包

4.1 选中 Dependencies 后点击下方的 + 号,选择「JARs or directories」

4.2 进入 Hadoop 目录下的 share/hadoop/ 中,把这几个包都导进去

4.2 在 pom.xml 中添加如下依赖

    <dependencies><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!--&lt;!&ndash; https://mvnrepository.com/artifact/commons-logging/commons-logging &ndash;&gt;--><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!--&lt;!&ndash; https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common &ndash;&gt;--><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.1.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-core</artifactId><version>1.2.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.1.1</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.1.1</version></dependency></dependencies>

5. 编写 Hadoop 项目的 Java 代码

5.1 新建 Java 类「Test.java」

5.2 编写代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;public class Test {// 在 HDFS 中新建一个 test 文件夹public static void main(String[] args) {FileSystem fileSystem = null;try {fileSystem = FileSystem.get(new URI("hdfs://localhost:9000/"),new Configuration(),"binguner");fileSystem.mkdirs(new Path("/test"));fileSystem.close();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (URISyntaxException e) {e.printStackTrace();}}}

5.3 运行 Java 程序

6. 运行结果

6.1 运行前的 HDFS 目录下没有 test 文件夹

6.2 运行后的 HDFS 目录下多了 test 文件夹

7. FileSystem 常用接口

  • 7.1 mkdirs
public boolean mkdirs(Path f) throws IOException {return this.mkdirs(f, FsPermission.getDirDefault());
}

参数是新的文件夹的路径,可以在文件夹里嵌套文件夹进行创建。

  • 7.2 create
    public FSDataOutputStream create(Path f) throws IOException {return this.create(f, true);}public FSDataOutputStream create(Path f, boolean overwrite) throws IOException {return this.create(f, overwrite, this.getConf().getInt("io.file.buffer.size", 4096), this.getDefaultReplication(f), this.getDefaultBlockSize(f));}public FSDataOutputStream create(Path f, Progressable progress) throws IOException {return this.create(f, true, this.getConf().getInt("io.file.buffer.size", 4096), this.getDefaultReplication(f), this.getDefaultBlockSize(f), progress);}public FSDataOutputStream create(Path f, short replication) throws IOException {return this.create(f, true, this.getConf().getInt("io.file.buffer.size", 4096), replication, this.getDefaultBlockSize(f));}public FSDataOutputStream create(Path f, short replication, Progressable progress) throws IOException {return this.create(f, true, this.getConf().getInt("io.file.buffer.size", 4096), replication, this.getDefaultBlockSize(f), progress);}public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize) throws IOException {return this.create(f, overwrite, bufferSize, this.getDefaultReplication(f), this.getDefaultBlockSize(f));}public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, Progressable progress) throws IOException {return this.create(f, overwrite, bufferSize, this.getDefaultReplication(f), this.getDefaultBlockSize(f), progress);}public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, short replication, long blockSize) throws IOException {return this.create(f, overwrite, bufferSize, replication, blockSize, (Progressable)null);}public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {return this.create(f, FsCreateModes.applyUMask(FsPermission.getFileDefault(), FsPermission.getUMask(this.getConf())), overwrite, bufferSize, replication, blockSize, progress);}public abstract FSDataOutputStream create(Path var1, FsPermission var2, boolean var3, int var4, short var5, long var6, Progressable var8) throws IOException;public FSDataOutputStream create(Path f, FsPermission permission, EnumSet<CreateFlag> flags, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {return this.create(f, permission, flags, bufferSize, replication, blockSize, progress, (ChecksumOpt)null);}public FSDataOutputStream create(Path f, FsPermission permission, EnumSet<CreateFlag> flags, int bufferSize, short replication, long blockSize, Progressable progress, ChecksumOpt checksumOpt) throws IOException {return this.create(f, permission, flags.contains(CreateFlag.OVERWRITE), bufferSize, replication, blockSize, progress);}

create 有多个重载函数,它的参数可以指定是否覆盖已有的文件、文件备份数量、写入文件缓冲区大小、文件块大小以及文件权限。它的返回值是一个 FSDataOutputStream,通过返回的 FSDataOutputStream 对象可以对文件进行写入。

  • 7.3 copyFromLocal
    public void copyFromLocalFile(Path src, Path dst) throws IOException {this.copyFromLocalFile(false, src, dst);}public void copyFromLocalFile(boolean delSrc, Path src, Path dst) throws IOException {this.copyFromLocalFile(delSrc, true, src, dst);}public void copyFromLocalFile(boolean delSrc, boolean overwrite, Path[] srcs, Path dst) throws IOException {Configuration conf = this.getConf();FileUtil.copy(getLocal(conf), srcs, this, dst, delSrc, overwrite, conf);}

将本地文件拷贝到文件系统,参数可以指定上传本地文件的路径,上传的多个路径组成的 Path 数组,存放目标对路径,可以指定是否删除本地本地的文件或者覆盖 hdfs 上已经创建的文件。

  • 7.4 copyToLocalFile
    public void copyToLocalFile(Path src, Path dst) throws IOException {this.copyToLocalFile(false, src, dst);}public void copyToLocalFile(boolean delSrc, Path src, Path dst) throws IOException {this.copyToLocalFile(delSrc, src, dst, false);}

将目标文件复制到本地指定路径,delSrc 参数指定移动文件后是否要删除源文件。

  • 7.6 moveToLocalFile
    public void moveToLocalFile(Path src, Path dst) throws IOException {this.copyToLocalFile(true, src, dst);}

将目标文件移动到指定路径,函数内部调用的是 copyToLocalFile

  • 7.6 exists
    public boolean exists(Path f) throws IOException {try {return this.getFileStatus(f) != null;} catch (FileNotFoundException var3) {return false;}}

输入一个路径,检查 HDFS 上是否存在这个路径,存在返回 true,不存在返回 false

  • 7.7 delete
    public abstract boolean delete(Path var1, boolean var2) throws IOException;

第一个参数是要删除的路径,第二个参数为 true 时,如果目标文件夹内有文件,会强制删除。

欢迎关注本文作者:

公众号搜索「Binguner」

关注并回复「干货」,获取我整理的千G Android、iOS、JavaWeb、大数据、人工智能等学习资源。

idea使用svn拉取项目代码_使用 IDEA 搭建 Hadoop3.1.1 项目相关推荐

  1. vscode中如何拉取git代码_工具 | 手把手教你在VSCode中使用Git

    在一个目录下clone项目: git clone XXXXXX.git 使用VScode 打开项目 右击通过Code打开. 使用vscode提交代码 1.打开下面视图,添加一行文字: ## 测试提交 ...

  2. svn拉取文件合并_四、Jenkins+SVN拉取文件下发

    一.安装SVN [root@docker02 ~]# yum -y install subversion [root@docker02 ~]# mkdir -pv /data/svn [root@do ...

  3. idea 拉取gitee代码_手把手撸一个 IDEA 插件

    点击上方"IT牧场",选择"设为星标" 技术干货每日送达! 作者:乱来梦游神 来源 :https://urlify.cn/Eja6zu 前段时间看到公众号一篇关 ...

  4. hadoopsdk使用_使用 IDEA 搭建 Hadoop3.1.1 项目

    Hadoop 的版本是 3.1.1 1. 启动 Hadoop 服务 $ start-all.sh 2. 新建 IDEA 的 Maven 项目 2.1 选中 Maven,Project SDK 选择为 ...

  5. SVN拉取和Excel冲突合并

    综述 SVN拉取原先是版本管理的一个功能,但遇到Excel文件的冲突就成为了一个阻碍工作的痛点.而程序化的Excel文件冲突合并就提上了开发日程. 本文将要阐述实现一键解决SVN拉取和Excel冲突合 ...

  6. gitee项目能用SVN拉取吗_使用Git开发维护Github开源项目的一些经验

    Github无法访问/访问慢的经验 github经常会因为一些原因抽风,无法访问,这时候,可以使用gitee同步github项目,然后从gitee拉项目.在gitee导入项目以后,项目名称后面有个同步 ...

  7. git gui怎么拉取项目代码_这些Git命令都不会,还是不要去面试了

    前言 以下,项目中经常使用的Git命令,汇总到这里以便与你能快速的学习和掌握Git命令,在文章最后有惊喜哟,一定要看到最后啊! 使用的 Git版本:git version 2.24.0 命令 git ...

  8. idea使用svn拉取源码,创建分支,合并分支教程

    svn拉取源码,创建分支,合并分支教程 最近对svn的分支使用比较感兴趣了,花时间研究了一下,分享给大家: 此文章记录了idea使用svn的过程:包括拉取源码.建立分支.切换分支.合并分支等常用操作. ...

  9. SVN拉取、提交文件

    前提: 已完成安装SVN软件以及配置相关账号和权限. SVN拉取项目 拿到SVN地址,新建文件夹后,在文件夹空白处鼠标右键,选择SVN Checkout,windows 11系统右键在显示更多选项中可 ...

最新文章

  1. keil 函数 默认 外部 内部 博客_5.9 C++内部函数与外部函数
  2. 解密微信小程序加密信息
  3. js声明变量的三种方式
  4. python摄像头推流_树莓派使用python-librtmp实现rtmp推流h264的方法
  5. mapreduce分组统计_Mongodb的分组统计MapReduce
  6. Hadoop入门(十八)Mapreduce的倒排索引程序
  7. 设计模式之创建者模式
  8. sendmail for linux
  9. Spring MVC的表单标签库详解
  10. clipboard.js在弹出框中无法复制的问题
  11. centos7 mysql读写监控,《Centos7——zabbix监控mysql的常规操作》
  12. 平板波导 matlab,非对称平板波导色散曲线求解(附matlab程序).doc
  13. 【C语言】开平方公式,根号下x的函数使用:sqrt()
  14. ping/tracert/telnet
  15. 10款提高工作效率的工具软件,你值得拥有!
  16. 【Python表白爱心合集】——“故事很长,我长话短说,我喜欢你,很久了”(♡ʟᴏᴠᴇ ᴜ ᴛʜʀᴇᴇ ᴛʜᴏᴜsᴀɴᴅ♡)
  17. 银行那些事儿--银行会计
  18. 【GAN:Dense block:VIIF】
  19. 2021-10-26 2021年B站1024安全攻防题第一题(加密解密)
  20. 节拍脉冲发生器的设计

热门文章

  1. The type 'System.Object' is defined in an assembly that is not referenced
  2. noip2016 蚯蚓
  3. Jmeter入门6 参数化—CSV Data Set Config 通过文件导入数据
  4. Flash Builder 使用
  5. log2:USB ,有线网, 安卓设备作外接WiFi
  6. Programming Ruby学习笔记一
  7. 八皇后问题--C语言学习笔记
  8. Quotations
  9. centos下MySQL Workbench连接时崩溃的解决方法
  10. VS2008 在IE8中 调试 ActiveX控件 无法进入断点的解决方法 设置VS2008和IE8 调试ATL MFC ActiveX控件