idea內 hdfs API 操作

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.hdfs.server.common.JspHelper;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.mortbay.util.Scanner;

import java.io.*;
import java.net.URI;
public class Test1 {
    private static Configuration conf = new Configuration();

    public static void main(String[] args) throws Exception{
        // addFiles();
        // addFile();
        // appendSomeThings();
        readFile();
        // addSomeThings();
   }

    //創建一個文件夾
    private static void addFiles() throws Exception{
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.150:8020"),conf);
        boolean mkdirs = fs.mkdirs(new Path("/abba"));
        if(mkdirs){
            System.out.println("ok");
        }else {
            System.out.println("?");
        }
    }

    //在上一個創建的文件夾內創建一個文件
    private static void addFile() throws Exception{
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.150:8020"),conf);
        boolean b = fs.createNewFile(new Path("/abba/a.txt"));
        if(b){
            System.out.println("ok");
        }else {
            System.out.println("?");
        }
    }

    //文件添加內容
    private static void addSomeThings() throws Exception {

        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.150:8020"),conf);
        FSDataOutputStream append = fs.append(new Path("/abba/a.txt"));
        BufferedWriter bf =new BufferedWriter(new OutputStreamWriter(append));

        bf.write("  這一行沒換行");

        //換行
        bf.newLine();
        bf.write("這一行換行");

        bf.close();
        fs.close();
    }

    //文件內容追加
    private static void appendSomeThings() throws Exception {

        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.150:8020"),conf);
        FSDataOutputStream append = fs.append(new Path("/abba/a.txt"));
        append.write("hello world !".getBytes());
        append.close();
        fs.close();
    }

    //讀取文件內容
    private static void readFile() throws Exception {

        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.150:8020"),conf);
        InputStream is = fs.open(new Path("/abba/a.txt"));
        BufferedReader bf = new BufferedReader(new InputStreamReader(is));

        String s = null;
        while ((s = bf.readLine()) != null){
            System.out.println(s);
        }

        is.close();
        bf.close();
        fs.close();

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章