FSDataOutputStream,这个类重载了很多write方法,用于写入很多类型的数据:比如字节数组,long,int,char等等。

像FSDataInputStream一样,要获得FSDataOutputStream的实例,必须通过FileSystem该类来和HDFS建立连接,然后通过路径返回FSDataOutputStream实例。

FileSystem返回FSDataOutputStream实例的方法有两组

  1.create(Path p)函数,创建一个空文件,然后可以向该文件顺序写入

  2.append(Path p)函数,打开一个已有文件,并最做文件末尾追加数据

FileSystemUtil

public class FileSystemUtil {

    private static FileSystem fileSystem;
       //代码中Kerberos认证根据自己环境替换即可
    public synchronized static FileSystem getFileSystem() throws IOException{
        if(fileSystem==null){
            Configuration conf=new Configuration();
            conf.set("fs.defaultFS", "hdfs://host12.master.cluster.enn.cn:8020");
            conf.set("dfs.client.block.write.replace-datanode-on-failure.policy" ,"NEVER" );
            conf.set("dfs.client.block.write.replace-datanode-on-failure.enable" ,"true" );
            KerberosClient.login(Constants.Kerberos_USER_NAME, Constants.Kerberos_KEYTAB_FILE);
            fileSystem=FileSystem.get(conf);
        }
        return fileSystem;
    }

    public synchronized static void shutdown(){
        if(fileSystem!=null){
            try {
                fileSystem.close();
                fileSystem=null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println(FileSystemUtil.getFileSystem());
    }
}

FSDataOutputStreamTest

public class FSDataOutputStreamTest{
    private static final Logger LOGGER = LoggerFactory.getLogger(Test.class);
    private static void hfdsAppendData() {
        String filePath = "/user/hive/warehouse/test.db/t_test/day=2017-11-29/hour=16/backup_ycgqh";
        FileSystem fileSystem = null;
        FSDataOutputStream fileOutputStream = null;
        Path hdfsPath = new Path(filePath);
        try {
            fileSystem=FileSystemUtil.getFileSystem();
            if (!fileSystem.exists(hdfsPath)) {
                fileOutputStream = fileSystem.create(hdfsPath,false);
            }else{
                fileOutputStream = fileSystem.append(hdfsPath);
            }
            fileOutputStream.writeUTF("");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                FileSystemUtil.shutdown();
            }
        }
    }
}

通过FSDataOutputStream向HDFS上写数据的更多相关文章

  1. HDFS的写数据过程分析

    HDFS的写数据过程分析 我们通过FileSystem类可以操控HDFS, 那我们就从这里开始分析写数据到HDFS的过程. 在我们向 HDFS 写文件的时候,调用的是 FileSystem.creat ...

  2. USB系列之四:向U盘上写数据

    在<USB系列之三>中,我们实现了一系列的SCSI命令,在这个系列中,我们要实现向U盘上写扇区的命令,所以,本文相对比较容易,更多地是给出一个实现的源程序. 在<USB系列之三> ...

  3. Linux启动kettle及linux和windows中kettle往hdfs中写数据(3)

    在xmanager中的xshell运行进入图形化界面 sh spoon.sh 新建一个job

  4. HDFS 读/写数据流程

    1. HDFS 写数据流程 客户端通过 Distributed FileSystem 模块向 NameNode 请求上传文件, NameNode 检查目标文件是否已存在,父目录是否存在: NameNo ...

  5. HDFS源码解析:教你用HDFS客户端写数据

    摘要:终于开始了这个很感兴趣但是一直觉得困难重重的源码解析工作,也算是一个好的开端. 本文分享自华为云社区<hdfs源码解析之客户端写数据>,作者: dayu_dls. 在我们客户端写数据 ...

  6. hbase 从hdfs上读取数据到hbase中

    <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifact ...

  7. HDFS数据流——写数据流程

    剖析HDFS文件写入 假设文件ss.avi共200m,其写入HDFS指定路径/user/atguigu/ss.avi流程如下: 1)客户端向namenode请求上传文件到指定路径,namenode通过 ...

  8. 把HDFS上的数据导入到Hive中

    1. 首先下载测试数据,数据也可以创建 http://files.grouplens.org/datasets/movielens/ml-latest-small.zip 2. 数据类型与字段名称 m ...

  9. 在standalone模式下运行yarn 0.9.0对HDFS上的数据进行计算

    1.通读http://spark.incubator.apache.org/docs/latest/spark-standalone.html 2.在每台机器上将spark安装到/opt/spark ...

随机推荐

  1. spring security There was an unexpected error (type=Forbidden, status=403).

    https://blog.csdn.net/qq_27093097/article/details/83190240 spring security There was an unexpected e ...

  2. PHP file函数

    一.判断函数 is_file($filename) //判断是否文件 is_link($filename) //判断是否为链接符号 is_dir($filename) //判断是否为路径 is_rea ...

  3. Zookeeper学习笔记(中)

    Zookeeper学习笔记(中) Zookeeper的基本原理和基本实现 深入了解ZK的基本原理 ZK的一致性: ZAB 协议: Zookeeper 原子消息广播协议 ZK通过选举保证 leader ...

  4. hive group by distinct区别以及性能比较

    Hive去重统计 相信使用Hive的人平时会经常用到去重统计之类的吧,但是好像平时很少关注这个去重的性能问题,但是当一个表的数据量非常大的时候,会发现一个简单的count(distinct order ...

  5. C# 基础:DataTable操作、发邮件

    本文出自:https://www.cnblogs.com/2186009311CFF/p/6865909.html DataTable操作 据参数删除为0的列:包括遍历.删除.取值 public st ...

  6. HOJ 2315 Time(模拟)

    Description Kim是一个掌控时间的大师.不同于一般人,他习惯使用秒来计算时间.如果你问他现在是几点,他会告诉你现在是今天的xxxx秒.Mik想要考考Kim.他想知道从某一天的00:00:0 ...

  7. 支持向量机(一)----总述(点到平面的距离,Lagrange函数,Lagrange对偶)

    概述:在对支持向量机的学习和使用过程中,遇到了许许多多的问题,通过查阅各种资料,也是逐一攻克了遇到的问题.感悟颇多,写此博文的目的是想以一个学习者的身份从一个刚接触支持向量机的角度去记录模型推到过程中 ...

  8. B. Equal Rectangles

    B. Equal Rectangles 给定4*N个数,是否能构成N个矩形 面积均相等 每次取两个大的,两个小的 #include<bits/stdc++.h> using namespa ...

  9. 版本基线自动化之Linux

    上一篇笔者叙述了如何在windows平台上进行自动化打包,这次采用linux平台 1.start.bat : 调用cleanall.bat脚本 ,从svn服务器中检出代码,并采用7-Zip工具进行压缩 ...

  10. window下启动redis服务

    ---恢复内容开始--- 在windows环境下启动redis服务,前提是你安装好了,启动如下: 一,进入redis的安装目录下,在地址栏输入“cmd”,回车 二,然后会进入cmd界面,直接运行命令r ...