目录
创建一个项目
在pom.xml中添加以下命令,用maven导入以下jar包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.1.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies>
在java目录下创建软件包,里面添加类HdfsApiClient
FileSystem fileSystem = null;
//初始化hdfs文件系统
public void init(){
//创建文件配置对象
Configuration conf = new Configuration();
}
Configuration conf = new Configuration() 是读取本地hadoop的配置文件
//创建文件系统对象
fileSystem = FileSystem.get(new URI("hdfs://bigdata03:8020"), conf, "root");
记得抛出异常
public void init() throws URISyntaxException, IOException, InterruptedException
创建文件系统对象
添加before注解
//初始化hdfs文件系统
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
//创建文件配置对象
Configuration conf = new Configuration();
//创建文件系统对象
fileSystem = FileSystem.get(new URI("hdfs://bigdata03:8020"), conf, "root");
System.out.println("hdfs文件系统初始化成功");
}
关闭文件系统对象
//关闭文件系统对象
@After
public void close() throws IOException {
if(fileSystem !=null){
fileSystem.close();
System.out.println("hdfs文件系统已经关闭");
}
}
创建hdfs目录
//创建hdfs目录
//除了头尾的具体的api操作,打上Test注解
@Test
public void createPath() throws IOException {
boolean result = fileSystem.mkdirs(new Path("/hdfs_api"));
if (result){
System.out.println("创建目录成功");
}
else{
System.out.println("创建目录失败");
}
}
启动我们的虚拟机bigdata03(有NameNode的节点)
打开NameNode的Web服务
运行createPath()
此时的web端显示已经创建成功
删除hdfs目录
//删除hdfs目录
@Test
public void deletePath() throws IOException {
Path deletePath = new Path("/hdfs_api");
//判断hdfs上是否存在该目录
//目录存在就删除
if(fileSystem.exists(deletePath)){
//第二个参数表示是否递归,如果目录不为空也能删除
boolean result = fileSystem.delete(deletePath, true);
System.out.println(result==true?"删除目录成功":"删除目录失败");
}
else{
System.out.println("要删除的目录hdfs中不存在");
}
}
在hdfs上创建一个文件 写入指定的内容
//在hdfs上创建一个文件,并写入指定内容
@Test
public void createHdfsFile() throws IOException {
//获取数据输出流对象
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/api_file.txt"));
//定义要输出的文件的内容
String line = "hello bigdata";
//将指定内容写入文件
fsDataOutputStream.write(line.getBytes());
//对输出流对象进行刷新
fsDataOutputStream.flush();
//关闭输出流对象
fsDataOutputStream.close();
}
对hdfs上的文件修改路径和名称
//对hdfs上的文件修改路径和名称
@Test
public void moveHdfsFile() throws IOException {
//文件原路径
Path src = new Path("/api_file.txt");
//文件新路径
Path dst = new Path("/hdfs_api/api_file_new.txt");
fileSystem.rename(src,dst);
}
读取hdfs上的文件内容
//读取hdfs上的文件内容
@Test
public void readHdfsFile() throws IOException {
//获取数据输入流对象
FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/hdfs_api/api_file_new.txt"));
//通过IO工具类读取文件中的数据
IOUtils.copyBytes(fsDataInputStream,System.out,2048,false);
System.out.println("\n");
}
从本地上传文件到hdfs上
//从本地上传文件到hdfs上
@Test
public void uploadFile() throws IOException {
//文件的本地路径
Path src = new Path("D:\\javacode\\word.txt");
//文件上传到hdfs的路径
Path dst= new Path("/hdfs_api");
fileSystem.copyFromLocalFile(src,dst);
}
从hdfs上下载文件到本地
//从hdfs上下载文件到本地
@Test
public void downloadFile() throws IOException {
//在hdfs上的文件
Path src= new Path("/hdfs_api/api_file_new.txt");
//文件的下载路径
Path dst= new Path("D:\\javacode\\api_file_new.txt");
//文件下载后是否删除hdfs上的原文件
boolean delSrc = false;
//true:下载的文件不会存在crc校验文件
//false:下载的文件存在crc校验文件
boolean useRawLocalFileSystem = true;
fileSystem.copyToLocalFile(delSrc,src,dst,useRawLocalFileSystem);
}
查看hdfs上的文件信息
//查看hdfs上的文件信息
@Test
public void queryHdfsFileInfo() throws IOException {
//查询的起始路径
Path path= new Path("/");
//是否递归
boolean recursive = true;
//获取迭代器
RemoteIterator<LocatedFileStatus> listIterator = fileSystem.listFiles(path, recursive);
//进行遍历
//判断迭代器是否还有需要迭代的元素
while(listIterator.hasNext()){
//获取迭代器中的需要迭代的元素
LocatedFileStatus fileStatus = listIterator.next();
//获取文件的路径
Path filePath = fileStatus.getPath();
System.out.println("文件的路径是"+filePath);
//获取文件的权限
FsPermission permission = fileStatus.getPermission();
System.out.println("文件的权限是"+permission);
//获取文件的所属用户
String owner = fileStatus.getOwner();
System.out.println("文件的所属用户是"+owner);
//获取文件的所属用户的用户组
String group = fileStatus.getGroup();
System.out.println("文件的所属用户的用户组是"+group);
//获取文件的副本数
short replication = fileStatus.getReplication();
System.out.println("文件的副本数是"+replication);
//获取文件的块大小
long blockSize = fileStatus.getBlockSize();
System.out.println("文件的块大小是"+blockSize/1024/1024+"MB");
System.out.println("--------------------------");
}
}