HDFS-文件写入API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.zhen.hdfs;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
 
/**
 * @author FengZhen
 * @date 2018年8月12日
 *
 */
public class FileSystemWriteAPI {
 
    public static void main(String[] args) {
        //create();
        mkdir();
    }
     
    /**
     *  FileSystem类有一系列新建文件的方法。
     *  1.最简单的方法是给准备建的文件指定一个Path对象,然后返回一个用于写入数据的输出流
     *   public FSDataOutputStream create(Path f) throws IOException {
            return create(f, true);
          }
        此方法有多个重载版本,允许我们指定是否需要强制覆盖现有的文件、文件备份数量、写入文件时所用的缓冲区大小、文件块大小以及文件权限。
        create方法能够为需要写入且当前不存在的文件创建父目录。尽管这样很方便,但有时不希望这样。如果希望父目录不存在就导致文件写入失败,则应该先调用exists方法检查父目录是否存在
        还有一个重载方法Progressable用于传递回调接口,如此一来,可以把数据写入datanode的进度通知给应用。
        public interface Progressable {
          public void progress();
        }
        2.另一种新建文件的方法是使用append方法在一个已有文件末尾追加数据
          public FSDataOutputStream append(Path f) throws IOException {
            return append(f, getConf().getInt("io.file.buffer.size", 4096), null);
          }
        这样的追加操作允许一个writer打开文件后在访问该文件的最后偏移量处追加数据。有了这个API,某些应用可以创建无边界文件,例如,应用可以在关闭日志文件之后继续追加日志。
        该追加操作是可选的,并非所有Hadoop文件系统都实现了该操作。例如,HDFS支持追加,但S3文件系统不支持
         
     */
     
    /**
     * FSDataOutPutStream对象
     * FileSystem实例的create方法返回FSDataOutPutStream对象,与FSDataInPutStream类相似,它也有一个查询文件当前位置的方法
     *  public long getPos() throws IOException {
          return position;                            // return cached position
        }
       但与FSDataInPutStream不同的是,FSDataOutPutStream类不允许在文件中定位。
       这是因为HDFS只允许对一个已打开的文件顺序写入,或在现有文件的末尾追加数据。换句话说,它不支持在除文件末尾之外的其它位置进行写入。
     */
    public static void create() {
        String localSrc = "/Users/FengZhen/Desktop/hadoop-file/core-site.xml";
        String dst = "hdfs://fz/user/hdfs/MapReduce/data/test_create.xml";
         
        Configuration conf = new Configuration();
        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(localSrc));
            FileSystem fileSystem = FileSystem.get(URI.create(dst), conf);
            OutputStream outputStream = fileSystem.create(new Path(dst), new Progressable() {
                public void progress() {
                    System.out.print(".");
                }
            });
            IOUtils.copyBytes(inputStream, outputStream, 4096, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    /**
     * 目录
     * FileSystem实例提供了创建目录的方法
     *   public boolean mkdirs(Path f) throws IOException {
           return mkdirs(f, FsPermission.getDirDefault());
         }
       通常,不需要显式创建一个目录,因为调用create方法写入文件时会自动创建父目录
     */
    public static void mkdir() {
        String uri = "hdfs://fz/user/hdfs/MapReduce/data/2017/08/11";
        Configuration conf = new Configuration();
        try {
            FileSystem fileSystem = FileSystem.get(new URI(uri), conf);
            fileSystem.mkdirs(new Path(uri));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}

  

posted on   嘣嘣嚓  阅读(874)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示