- 创建maven工程:文件->新建->其他,弹出如下窗口:
- 搜索Maven Project,创建maven工程,勾选选择快速创建,选好工作空间,下一步
- 选择好maven坐标,点击finish完成maven项目创建
- 创建完如图:
编辑pom.xml文件,“project>”到”>”之间的定义说明maven版本,必须为4.0.0,<dependencies>之前的六行说明刚才创建的maven项目的信息。<dependencies></ependencies>之间为依赖,其中junit为测试用的依赖,hadoop-hdfs为工程中HDFS所需要的依赖,hadoop-core为Hadoop编写Hadoop必须的一些依赖,hadoop-client为在编写Hadoop客户端程序时常用的依赖,maven-compiler-plugin为利用maven打包时所用到的依赖。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.myhadoop</groupId>
<artifactId>myhadoop.hdfs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hdfs</name>
<description>操作Hadoop的文件系统</description>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
5、创建两个类,使类myhdfsAPI和操作类File。
package myhadoop.hdfs.FileOperations;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
public class File {
/*上传操作,将本地文本文件上传到分布式文件系统(HDFS)指定目录下。*/
public static void put(String remotePath,String localPath)throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);//创建一个分布式文件系统对象
Path src = new Path(localPath); //得到操作本地文件的路径对象
Path dst = new Path(remotePath); //得到操作分布式文件系统(HDFS)的路径对象
fs.copyFromLocalFile(src, dst); //上传本地文件到目录位置
fs.close(); //关闭分布式文件操作对象
}
/*读取操作,将分布式文件系统(HDFS)中的文件读出来。*/
public static void cat(String remotePath)throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf); //创建一个分布式文件系统对象
Path path =new Path(remotePath); //得到操作分布式文件系统(HDFS)的路 对象
if(fs.exists(path)) //判断目标位置是否存在
{
FSDataInputStream is =fs.open(path);//打开分布式文件操作对象
FileStatus status =fs.getFileStatus(path); //获取文件状态
//byte[] buffer =new byte[Integer.parseInt(String.valueOf(status.getLen()))];
byte[] buffer = new byte[Integer.parseInt(String.valueOf(status.getLen()))];
// byte[] buffer = new byte[Integer.parseInt(String.valueOf(status.getLen()))];
is.readFully(0, buffer); //读取文件流到buffer中
is.close(); //关闭流
fs.close(); //关闭文件操作对象
System.out.println(buffer.toString());
}
}
/*下载操作,将分布式文件系统(HDFS)中的文件下载到本地。*/
public static void get(String remotePath,String localPath)throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);//创建一个分布式文件系统对象
Path src = new Path(localPath); //得到操作本地文件的路径对象
Path dst = new Path(remotePath); //得到操作分布式文件系统(HDFS)的路径对象
fs.copyToLocalFile(dst,src); //下载分布式文件系统中的文件到本地
fs.close(); //关闭分布式文件操作对象
}
/*删除操作,删除分布式文件系统(HDFS)中的文件。*/
public static void rmr(String remotePath)throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf); //创建一个分布式文件系统对象
Path path = new Path(remotePath); //得到操作分布式文件系统(HDFS)的路径对象
fs.delete(path, true); //执行删除操作
fs.close(); //关闭分布式文件操作对象
}
/*目录列表操作,展示分布式文件系统(HDFS)中的目录结构。*/
public static void ls(String remotePath)throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf); //创建一个分布式文件系统对象
Path path = new Path(remotePath); //得到操作分布式文件系统(HDFS)的路径对象
FileStatus[] status = fs.listStatus(path);//的到文件状态数组
Path[] lisPaths = FileUtil.stat2Paths(status);
for (Path p : lisPaths) { //循环打印目录结构
System.out.println(p);
}
fs.close(); //关闭分布式文件操作对象
}
}
public class myhdfsAPI extends Configured implements Tool {
public static void main(String[] args) throws Exception {
int code = ToolRunner.run(new myhdfsAPI(), args);
System.exit(code);
}
@Override
public int run(String[] args) throws Exception {
Configuration config = getConf();
使用对应的方法:
// File.ls(args[0]); //
File.cat(args[0]); //
// File.put(args[0], args[1]);//
// File.get(args[0], args[1]);//
// File.rmr(args[0]); //
return 0;
}
}
6、打包,两种方法,第一种方法依次操作项目名右击,选择export,选择jar file->next->finish
第二种方法:在POM.XML中安装了aven- compiler-plugin,在pom.xml右击,选择run as->n build,在弹出的窗口填写编译目标,如图
7、在集群上运行示例