7-部署本地开发环境2-上传与下载(hdfs)
1. 新建一个java类MkdirDemo
MkdirDemo 类实现 HDFS 上目录和文件的创建、删除操作,并提供遍历目录的功能。


点击“Enter”
编写以下代码:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* HDFS 目录与文件的创建、删除操作示例
*/
public class MkdirDemo {
// 获取 HDFS 文件系统对象
public static FileSystem getFileSystem() throws IOException, URISyntaxException {
Configuration conf = new Configuration();
URI uri = new URI("hdfs://192.168.47.201:8020");
return FileSystem.get(uri, conf);
}
// 遍历指定目录并打印其下所有文件/目录信息
public static void list(String dir) throws Exception {
FileSystem fileSystem = getFileSystem();
FileStatus[] listStatus = fileSystem.listStatus(new Path(dir));
for (FileStatus fileStatus : listStatus) {
boolean isDir = fileStatus.isDirectory();
String name = fileStatus.getPath().toString();
System.out.println(isDir + " " + name);
}
fileSystem.close(); // 关闭资源
}
// 创建目录(支持递归创建多级目录)
public static void mkdir(String path) throws Exception {
FileSystem fileSystem = getFileSystem();
boolean result = fileSystem.mkdirs(new Path(path));
System.out.println("创建目录 " + path + " : " + (result ? "成功" : "失败"));
list("/"); // 创建后展示根目录内容
fileSystem.close();
}
// 创建空文件
public static void create(String path) throws Exception {
FileSystem fileSystem = getFileSystem();
fileSystem.create(new Path(path)).close(); // 创建文件后立即关闭输出流
System.out.println("创建文件 " + path + " 成功");
list("/");
fileSystem.close();
}
// 删除目录或文件(recursive = true 表示递归删除)
public static void delete(String path) throws Exception {
FileSystem fileSystem = getFileSystem();
boolean result = fileSystem.delete(new Path(path), true);
System.out.println("删除 " + path + " : " + (result ? "成功" : "失败"));
list("/");
fileSystem.close();
}
public static void main(String[] args) throws Exception {
// 创建目录
mkdir("/bigdata");
// 创建文件
create("/demo.txt");
// 删除目录(若需要)
// delete("/bigdata");
}
}

代码功能:在hdfs目录下新建了一个bigdata子目录,创建了一个demo.txt文件
在浏览器输入:192.168.47.201:9870

2.新建一个java类UploadDemo01
UploadDemo01:实现本地文件与 HDFS 之间的上传和下载操作(使用数据流方式)
(1)在项目中新建一个 hello.txt 文件

编辑文件写上:这是一个测试文件!

按ctrl+s保存
新建UploadDemo01 类


点击“Enter”
编辑以下代码:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
public class UploadDemo01 {
public static FileSystem getFileSystem() throws IOException, URISyntaxException {
Configuration conf = new Configuration();
URI uri = new URI("hdfs://192.168.47.201:8020");
return FileSystem.get(uri, conf);
}
// 上传:本地 -> HDFS
public static void uploadFile(String source, String dest) throws IOException, URISyntaxException {
FileSystem fs = getFileSystem();
try (FileInputStream in = new FileInputStream(source);
FSDataOutputStream out = fs.create(new Path(dest))) {
IOUtils.copyBytes(in, out, 4096, true);
System.out.println("上传成功:" + source + " -> " + dest);
} catch (FileNotFoundException e) {
System.err.println("本地文件不存在:" + source);
} finally {
fs.close();
}
}
// 下载:HDFS -> 本地(使用流复制,无需 winutils)
public static void downloadFile(String hdfsPath, String localPath) throws IOException, URISyntaxException {
FileSystem fs = getFileSystem();
try (FSDataInputStream in = fs.open(new Path(hdfsPath));
FileOutputStream out = new FileOutputStream(localPath)) {
IOUtils.copyBytes(in, out, 4096, true);
System.out.println("下载成功:" + hdfsPath + " -> " + localPath);
} catch (FileNotFoundException e) {
System.err.println("HDFS 文件不存在:" + hdfsPath);
} finally {
fs.close();
}
}
public static void main(String[] args) throws Exception {
// 上传文件(确保本地存在 hello.txt)
uploadFile("hello.txt", "/hello.txt");
// 下载文件
downloadFile("/hello.txt", "hello1.txt");
}
}
执行成功

3.新建一个java类UploadDemo02
新建UploadDemo02 类


点击“Enter”
新建一个test2.txt文件


编辑内容:这是使用copyFromLocalFile 和copyToLocalFile 方法实现文件的上传与下载操作。

编辑以下代码:
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
public class UploadDemo02 {
// 创建方法 getFileSystem,实现获取文件系统对象的操作
public static FileSystem getFileSystem() throws IOException, URISyntaxException {
Configuration conf = new Configuration(); // 创建 conf 对象
URI uri = new URI("hdfs://192.168.47.201:8020"); // 创建 uri 对象
final FileSystem fileSystem = FileSystem.get(uri, conf); // 创建 fileSystem 对象
return fileSystem;
}
// 上传文件/目录:将本地路径 pathLocal 的文件/目录上传到 HDFS 路径 pathDistal
public static void uploadfile(String pathLocal, String pathDistal) throws Exception {
final FileSystem fileSystem = getFileSystem();
fileSystem.copyFromLocalFile(new Path(pathLocal), new Path(pathDistal));
}
// 下载文件/目录:将 HDFS 路径 pathDistal 的文件/目录下载到本地路径 pathLocal
public static void downloadFile(String pathDistal, String pathLocal) throws Exception {
final FileSystem fileSystem = getFileSystem();
fileSystem.copyToLocalFile(new Path(pathDistal), new Path(pathLocal));
}
public static void main(String[] args) throws Exception {
System.setProperty("hadoop.home.dir", "D:\\SystemsAndTools\\hadoop-3.1.2");
// 上传:将本地 test2.txt 上传到 HDFS 根目录
uploadfile("test2.txt", "/");
// 下载:将 HDFS 根目录下的 hello.txt 下载到本地 hello3.txt
downloadFile("/hello.txt", "hello2.txt");
}
}

以上代码执行后无问题,那么恭喜你,已经学会了部署本地开发环境了!!!
&spm=1001.2101.3001.5002&articleId=162544491&d=1&t=3&u=929098b47b7741c385283a9564e321b1)
1208

被折叠的 条评论
为什么被折叠?



