使用java API操作Alluxio文件系统

目录

一、创建Maven工程

1.引入库

二、文件操作

1.连接到自己的Alluxio环境

2.创建新文件

3.删除文件

4.读取文件

5.写入文件

三、总结


一、创建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();

    }

三、总结

记得操作之后要把输入流和输出流关掉

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值