这两天 资本家突然对Ozone感兴趣 让使用Java实现上传下载
我就找各种资料 找.....找 找了一个月
Documentation for Apache Hadoop Ozone
上面这个连接是 Ozone官网的 里面有命令行的指令 和javaAPI的实现 但是只要这个是实现不了的 因为官方没有给你提供 要引入POM和 类似于YML的配置 我就各种找人问 终于有个大大大神 他也没用过 但是架不住人家智商高 现学 。。。。。记得设置解除文件大小上传限制 不然会出错
可以去我另一篇文章看
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-ozone-client</artifactId>
<version>1.1.0</version>
</dependency>
我是在本地搭建的docker 版本的Ozone 直接拉取的官方镜像
不废话 上代码
思路 首先你要有 一个 Ozone 当然我是拉取的官方docker的 没有废啥力气
然后需要先写一个 上传到WEB服务器的 文件上传接口 (这个你们可以去csdn或者别的论坛搜一下都会有)
//要存储的路径如果你要在本地测试就用你本地的盘路径 如果是liunx就用liunx的
private String fileSavePath="E:\\";
public String upload(MultipartFile uploadFile, HttpServletRequest req,String 创建卷,String 创建桶,String 创建快) throws IOException {
String filePath = "";
String format = sdf.format(new Date());
File folder = new File(fileSavePath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() +
oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
uploadFile.transferTo(new File(folder, newName));
filePath = fileSavePath+ format + newName;
} catch (IOException e) {
e.printStackTrace();
return "上传失败! ";
}
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() +
oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
uploadFile.transferTo(new File(folder, newName));
filePath = fileSavePath+ format + newName;
} catch (IOException e) {
e.printStackTrace();
return "上传失败! ";
}
OzoneConfig ozoneConfig = new OzoneConfig();
ozoneConfig.Ozonedu(filePath,创建卷,创建桶,创建快);
return filePath;
}
接下来是官方给的javaAPI 其实这个上传的代码是可以直接拿来用的 我主要是是卡住在 可移动配置
不知道这个东西是什么 记住一定要是9862接口哦 这个是Ozone指定的 具体我也不知道为什么
可移动配置
new一个 OzoneConfiguration 就可以啦 不要问为什么 因为我也只是会用。。。
// 从文件中读取数据,需要用户提供此函数
byte [] videoData = readFile("intro.mp4");
// 创建客户端对象
OzoneClient ozClient = OzoneClientFactory.getRpcClient(服务器ip,9862接口,可移动配置);
// 通过客户端对象获取 ObjectStore 的引用
ObjectStore objectStore = ozClient.getObjectStore();
// 创建用于存储数据的 assets 卷
// 此处创建的卷采用默认参数
objectStore.createVolume("assets");
// 验证卷是否已创建
OzoneVolume assets = objectStore.getVolume("assets");
// 创建名为 videos 的桶
assets.createBucket("videos");
OzoneBucket video = assets.getBucket("videos");
// 从文件中读取数据,需要用户提供此函数 放你上传文件的路径哦 也就是上面上传到WEB服务器的filePath
byte [] videoData = readFile(path);
// 创建输出流并写数据
OzoneOutputStream videoStream = video.createKey("intro.mp4", 1048576);
videoStream.write(videoData);
// 写操作完成之后关闭输出流
videoStream.close();
// 从文件中读取数据,需要用户提供此函数
byte [] videoData = readFile("intro.mp4");
这个地方是需要你们自己去写一个文件转为 字节流的转换方法哦 但是为了方便你们 我就直接贴在下面了
public static byte[] readFile(String path)
{
try {
FileInputStream in =new FileInputStream(new File(path));
byte[] data=new byte[in.available()];
in.read(data);
in.close();
return data;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
到此上传就可以了
可以利用 官方给的命令行去查看你相应的 卷桶快 是否创建 如果创建那就证明是可以的
上传 = 文件上传到WEB -----转字节流------存储到Ozone
下载的话我就不贴了 和上传一样的 一样的配置Ozone就可以
下载 = 读取Ozone ------字节流转文件 -------将文件再通过下载接口下载到本地 (字节流转文件后可以指定到虚拟机某个路径下 你可以写WEB服务器的下载接口 根据路径下载就可以)
上传和下载的时候 卷 桶 不可重复
新人写文章 多多指教 绝对原创哦 因为我当时做的时候搜遍了各大论坛 都没有java集成Ozone
未经本人通知禁止搬运哦