目录
一、创建Maven工程
1.引入库
<!-- https://mvnrepository.com/artifact/org.alluxio/alluxio-core-common -->
<dependency>
<groupId>org.alluxio</groupId>
<artifactId>alluxio-core-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.alluxio/alluxio-core-client -->
<dependency>
<groupId>org.alluxio</groupId>
<artifactId>alluxio-core-client</artifactId>
<version>2.6.0</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.alluxio/alluxio-core-client-fs -->
<dependency>
<groupId>org.alluxio</groupId>
<artifactId>alluxio-core-client-fs</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
二、文件操作
1.连接到自己的Alluxio环境
在Maven项目中创建java类FileTest,添加所需要的属性,在类中添加init()方法从而连接到自己的Alluxio服务器
package com.test.alluxioTest;
import alluxio.AlluxioURI;
import alluxio.client.file.FileInStream;
import alluxio.client.file.FileOutStream;
import alluxio.client.file.FileSystem;
import alluxio.conf.AlluxioConfiguration;
import alluxio.conf.AlluxioProperties;
import alluxio.conf.InstancedConfiguration;
import alluxio.conf.PropertyKey;
import alluxio.exception.AlluxioException;
import alluxio.grpc.CreateFilePOptions;
import alluxio.grpc.WritePType;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
public class FileTest {
AlluxioProperties properties;
AlluxioConfiguration configuration;
FileSystem fileSystem;
CreateFilePOptions options;
@Before
public void init() {
//创建属性类
properties = new AlluxioProperties();
//将master节点设置成当前你alluxio服务器的IP地址或hostname
properties.set(PropertyKey.MASTER_HOSTNAME, "alluxio01");
//操作alluxio集群的用户名,一般为运行alluxio-start.sh命令的用户
properties.set(PropertyKey.SECURITY_LOGIN_USERNAME, "hope");
//生成配置类
configuration = new InstancedConfiguration(properties);
fileSystem = FileSystem.Factory.create(configuration);
options = CreateFilePOptions.newBuilder() //创建CreateFilePOptions类型的构造器,该类型可以配置一些alluxio文件操作的属性
.setWriteType(WritePType.ASYNC_THROUGH) //这里配置了写入文件时数据写入alluxio的worker之后,异步的将数据写入底层文件系统
.buildPartial();//最后使用创建CreateFilePOptions类型,使用该方法时.未配置的项会采用默认设置
}
}
2.创建新文件
在类FileTest中添加函数实现操作
@Test
public void createTest() throws IOException, AlluxioException {
AlluxioURI path = new AlluxioURI("/files/test.txt");
//创建一个之前不存在的文件,并生成输出流
FileOutStream fileOutStream = fileSystem.createFile(path,options);
fileOutStream.close();
}
3.删除文件
在类FileTest中添加函数实现操作
@Test
public void deleteTest() throws IOException, AlluxioException {
AlluxioURI path = new AlluxioURI("/files/test.txt");
//创建一个之前不存在的文件,并生成输出流
fileSystem.delete(path);
fileSystem.close();
}
4.读取文件
在类FileTest中添加函数实现操作
@Test
public void readTest() throws IOException, AlluxioException {
AlluxioURI path = new AlluxioURI("/files/LICENSE");
//创建一个之前不存在的文件,并生成输出流
FileInStream fileInStream = fileSystem.openFile(path);
//生成输出流之后就可以使用java的文件IO对文件进行读写
InputStreamReader inputStreamReader = new InputStreamReader(fileInStream,"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
bufferedReader.lines().forEach(System.out::println);
fileInStream.close();
}
5.写入文件
在类FileTest中添加函数实现操作
@Test
public void writeTest() throws IOException, AlluxioException {
AlluxioURI path = new AlluxioURI("/files/test.txt");
//创建一个之前不存在的文件,并生成输出流
FileOutStream fileOutStream = fileSystem.createFile(path,options);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write("hello world");
fileOutStream.close();
}
三、总结
记得操作之后要把输入流和输出流关掉