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. 使用glew和glad 新建窗口

    一.添加头文件 首先,将头文件加到项目的.cpp文件中 #include <glad/glad.h> 2 #include <GLFW/glfw3.h> 注: 包含glad的头 ...

  2. shell之文本过滤(grep)

    shell之文本过滤(grep) 分类: linux shell脚本学习2012-09-14 14:17 588人阅读 评论(0) 收藏 举报 shell正则表达式扩展工具存储 grep(全局正则表达 ...

  3. 【03】Python 文件读写 JSON

    1 打开文件 文件操作步骤: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 1.1 打开方法 f = open('xxx.txt') #需f.close( ...

  4. Debian10+OpenMediaVault(OMV)安装

    前言:测试打造NAS平台,以下是步骤. 安装Debian10 注:请下载amd64,不要下载i836平台,因为OMV外挂插件不支持I836所以不建议用i836,如只使用官方插件可以无视 安装前-安装, ...

  5. 【leetcode】1108. Defanging an IP Address

    题目如下: Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP a ...

  6. SpringBoot最新教程IDEA版【狂神说Java系列】

    Spring Boot入门 1.spring boot是配置好的spring集成框架,约定大于配置 2.微服务:把service拆出来独立运行在各个机器上.看下面这两篇论文 原文地址:http://m ...

  7. 在 centos7.5 使用 DockerFile 构建镜像时报错 "Error parsing reference:"microsoft/dotnet:2.2-aspnetcore-runtime AS base"is not a valid repository/tag: invalid reference format"

    运行 dockerfile 时报出的错误 FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base Error parsing reference: & ...

  8. WEB上传一个文件夹

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...

  9. 基于Cesium的Web3d实现二三维室内外一体化融合

    https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/index.html?src=3D%20Models.html 三维建模 BIM

  10. RabbitMQ幂等性概念(七)

    幂等性是什么? 我们可以借鉴数据库的乐观锁机制 比如我们执行一条更新库存的sql语句update user set count=count-1,version=version+1 where vers ...