Hadoop分布式文件系统操作(FsDataInputStream和FsDataOutputStream)

前言:

        上一篇文章使用IDEA成功连接上了HAdoop集群,本章将使用Configuration、FileSystem、Path、FSDataInputStream、FSDataOutputStream对HDFS(分布式文件系统)进行读写操作。

实现:

        查看文件是否存在

        初始化一个filename,将其赋值为hadoop集群文件系统下的一个文件的路径。 实例化configuration类与filesystem类。 调用filesystem类的get方法赋值给fs实例。 实例化一个path类,并赋初值为filename 调用filesystem类的exists方法查看word.txt是否存在

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.BasicConfigurator;

import java.io.IOException;


import java.io.IOException;
public class fileTest{
    public static void main(String[] args) {
        //自动快速地使用缺省Log4j环境。
//        BasicConfigurator.configure();
        try {
            String filename = "hdfs://192.168.139.100:9000/test/input/word.txt";
            Configuration conf = new Configuration();
            FileSystem fs = null;
            fs = FileSystem.get(conf);
            if (fs.exists(new Path(filename))){
                System.out.println("the file is exist");
            }else{
                System.out.println("the file is not exist");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
        读取文件内容

        初始化一个filename,将其赋值为hadoop集群文件系统下的一个文件的路径。 实例化configuration类与filesystem类。 实例化一个path类,并赋初值为filename 调用filesystem类的get方法赋值给filesystem实例。 调用filesystem类的exists方法查看word.txt是否存在 调用filesystem类的open方法将文件以流的形式读取并赋值给FSDataIntputStream的实例 将FSDataInputStream的实例传入IOUtils类中查看文件内容

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.IOException;

public class FSfileinput {
    public static void main(String[] args) {
//      初始化FileSystem类
        FileSystem fs = null;
//      初始化configuration类
        Configuration conf = new Configuration();
//      赋值文件变量
        String filePath = "hdfs://192.168.139.100:9000/test/input/words.txt";
//      实例化Path类
        Path path = new Path(filePath);
//      初始化FSDataInputStream类
        FSDataInputStream in = null;
        try {
            fs = FileSystem.get(conf);
            if (fs.exists(path)){
                in = fs.open(path);
                IOUtils.copyBytes(in, System.out, 2048, false);
                IOUtils.closeStream(in);
                fs.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
        追加或覆盖写

        初始化一个filename,将其赋值为hadoop集群文件系统下的一个文件的路径。 实例化configuration类与filesystem类。 实例化一个path类,并赋初值为filename 调用filesystem类的get方法赋值给filesystem实例。 调用filesystem类的exists方法查看word.txt是否存在 调用filesystem类的create方法将文件以流的形式读取并赋值给FSDataOutputStream的实例 使用FSDataOutputStream的实例覆盖或追加

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;

public class FSfileoutput {
    public static void main(String[] args) throws IOException {
        //      初始化文件系统
        FileSystem fileSystem = null;
        //      文件路径
        String filepath = "hdfs://192.168.139.100:9000/test/input/words.txt";
        //      初始化fsdataoutputstream
        FSDataOutputStream fsDataOutputStream = null;
        //      初始化path
        Path path = new Path(filepath);
        //      初始化configuration
        Configuration conf = new Configuration();
        try {
            fileSystem = FileSystem.get(conf);
            if (!fileSystem.exists(path)){
        //       当文件不存在的时候 创建文件
                fsDataOutputStream = fileSystem.create(path, false);
            }else {
        //       当文件存在的时候 追加
                fsDataOutputStream = fileSystem.append(path);
            }
            fsDataOutputStream.writeUTF("你好");
//            fsDataOutputStream.write("不好".getBytes());
            fsDataOutputStream.writeBytes("\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fsDataOutputStream!=null){
                try {
                    fsDataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fileSystem.close();
            }
        }
    }
}

问题:

        1. 文件系统处于安全模式:Cannot create file/test/input/words.txt. Name node is in safe mode.

hadoop dfsadmin -safemode leave # 移除安全模式

        2. 文件系统目录中创建文件,被拒绝。接触安全模式后使用hdfs授权w(write权限)。

hdfs dfs -chmod a+w /test/input

猜你喜欢

转载自blog.csdn.net/2201_75875170/article/details/133832066
今日推荐