Hadoop之旅(8)— HDFS API 实战操作

1、得到系统文件

/**
 * Get FileSystem (得到系统文件)
 *
 * @return
 * @throws Exception
 */
public static FileSystem getFileSystem() throws Exception {
    Configuration configuration = new Configuration();
    configuration.set("fd.defaultFS","hdfs://chenzy-1:9000");
    // get filesystem
    FileSystem fileSystem = FileSystem.get(configuration);

    System.out.println(fileSystem);
    return fileSystem;
}


2、创建文件

//指定相对路径relative path   路径:/user/Administrator/文件名
private static void relativePath() throws Exception{
    // get filesystem
    FileSystem fileSystem = getFileSystem();
    boolean b = fileSystem.createNewFile(new Path("relative.txt"));
    System.out.println(b);
    fileSystem.close();
}


3、追加文件内容

//追加内容
private static void appendContent() throws Exception{
    // get filesystem
    FileSystem fileSystem = getFileSystem();

    Path path = new Path("/chenzy/mapreduce/wordcount/input/test.txt");
    FSDataOutputStream append = fileSystem.append(path);
    append.write("this is append content ! 追加内容 !".getBytes());
    append.close();
    fileSystem.close();
}


//添加内容
private static void bufferedContent() throws Exception{
    // get filesystem
    FileSystem fileSystem = getFileSystem();

    Path path = new Path("/chenzy/mapreduce/wordcount/input/test.txt");
    FSDataOutputStream append = fileSystem.append(path);
    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(append));
    bufferedWriter.newLine();
    bufferedWriter.write("this is append content ! 内容 !");
    bufferedWriter.newLine();
    bufferedWriter.write("这是换行的内容!");
    bufferedWriter.newLine();

    bufferedWriter.close();
    fileSystem.close();
}


4、读取文件

//读取文件
private static void openRead() throws Exception{
    // get filesystem
    FileSystem fileSystem = getFileSystem();
    InputStream is = fileSystem.open(new Path("/chenzy/mapreduce/wordcount/input/test.txt"));
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));

    String line = null;
    while ((line = bufferedReader.readLine()) != null){
        System.out.println(line);
    }

    is.close();
    bufferedReader.close();
    fileSystem.close();
}



5、从本地拷贝到hdfs

//从本地拷贝到hdfs
private static void copyFromlocal() throws Exception{
    // get filesystem
    FileSystem fileSystem = getFileSystem();
    fileSystem.copyFromLocalFile(new Path("E:\\from\\new.txt"),new Path("/chenzy/mapreduce/wordcount/newMkdirs/test.txt"));
    fileSystem.close();
}

//hdfs拷贝到本地
private static void copyTolocal() throws Exception{
    // get filesystem
    FileSystem fileSystem = getFileSystem();
    fileSystem.copyToLocalFile(new Path("/chenzy/mapreduce/wordcount/newMkdirs/test.txt"),new Path("E:\\from\\new2.txt"));
    fileSystem.close();
}


更多的例子:查看GitHub



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值