需要注意以下几点

1.所以的包都是org.apache.hadoop.XXX

2.三个配置文件要放到指定文件夹中等待文件系统读取(src/main/resources):core-site.xml hdfs-site.xml log4j.properties

3.文件路径指向要正确

package com.cenzhongman.hadoop.hdfs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; 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.io.IOUtils; public class HdfsApp { /**
* to get fileSystem
*
* @return fileSystem
*/
public static FileSystem getFileSystem() {
// 1.read configuration information : core-site.xml core-default.xml
// hdfs-site.xml hdfs-default.xml
Configuration conf = new Configuration(); // 2.get fileSystem
org.apache.hadoop.fs.FileSystem fileSystem = null;
try {
fileSystem = org.apache.hadoop.fs.FileSystem.get(conf);
} catch (IOException e) {
e.printStackTrace();
} // 3.return fileSystem
return fileSystem;
} /**
* read data form fileSystem
*
* @param fileName
*/
public static void read(String fileName) {
// 1.get fileSystem
FileSystem fileSystem = getFileSystem();
System.out.println(fileSystem); // 2.read path
Path readPath = new Path(fileName); // 3.open file and get FSDataInputStream
FSDataInputStream inStream = null;
try {
inStream = fileSystem.open(readPath);
} catch (IOException e1) {
e1.printStackTrace();
} // 4.read file info
try {
// read
IOUtils.copyBytes(inStream, System.out, 4096, false);
} catch (Exception e) {
e.printStackTrace();
} finally {
// close stream
IOUtils.closeStream(inStream);
}
} public static void uploadFile(String fromFilePath, String putFilePath) {
// 1.get fileSystem
FileSystem fileSystem = getFileSystem(); // 2.write path
Path weitePath = new Path(putFilePath); // 3.Output Stream
FSDataOutputStream ourStream = null;
try {
ourStream = fileSystem.create(weitePath);
} catch (IOException e1) {
e1.printStackTrace();
} // 4.input Stream
FileInputStream inStream = null;
try {
inStream = new FileInputStream(new File(fromFilePath));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // 5.stream read/write
try {
// read
IOUtils.copyBytes(inStream, ourStream, 4096, false);
} catch (Exception e) {
e.printStackTrace();
} finally {
// close stream
IOUtils.closeStream(inStream);
IOUtils.closeStream(ourStream);
}
} public static void main(String[] args) throws Exception {
String fileName = "/tmp/hadoop-yarn/staging/history/done_intermediate/cen/job_1497948413653_0001_conf.xml";
read(fileName); String putFilePath = "/user/cen/output/file-output-test.xml";
String fromFilePath = "/usr/local/hadoop-2.5.0/input/core-site.xml";
uploadFile(fromFilePath, putFilePath);
}
}

Maven下从HDFS文件系统读取文件内容的更多相关文章

  1. 在Spring Boot快捷地读取文件内容的若干种方式

    引言: 在Spring Boot构建的项目中,在某些情况下,需要自行去读取项目中的某些文件内容,那该如何以一种轻快简单的方式读取文件内容呢?  基于ApplicationContext读取 在Spri ...

  2. 7 RandomAccessFile读取文件内容保存--简单例子(需要验证)

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; /** * 读取动态产生的文件内容 */ publ ...

  3. 使用while和read命令读取文件内容

    转:使用while和read命令读取文件内容 1.准备数据文件 $cat a.txt 200:2 300:3 400:4 500:5 2.用while循环从文件中读取数据 #!/bin/ksh whi ...

  4. 使用 istreambuf_iterator 读取文件内容,赋值给 std::string

    需要一个一个字符输入时考虑使用istreambuf_iterator 假设我们要把一个文本文件拷贝到一个字符串对象中.似乎可以用一种很有道理的方法完成: ifstream inputFile(&quo ...

  5. PHP读取文件内容的五种方式(转载)

    php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp); php读取文件内容: -----第一种方法--- ...

  6. h5-21-文件操作-读取文件内容

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. java读取文件内容常见几种方式

    ①随机读取文件内容 ②以行为单位读取文件,常用于读面向行的格式化文件 ③以字符为单位读取文件,常用于读文本,数字等类型的文件 ④以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件 pa ...

  8. shell读取文件内容

           Shell脚本,执行解释速度快.代码简单易于理解.在shell代码编写过程中,经常会用到读取文件内容. 写法一: ------------------------------------ ...

  9. Python跳过第一行读取文件内容

    Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...

随机推荐

  1. Opencv在linux下安装

    Opencv in Linux These steps have been tested for Ubuntu 10.04 but should work with other distros as ...

  2. Nginx教程(一) Nginx入门教程

    Nginx教程(一) Nginx入门教程 1 Nginx入门教程 Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行.由 ...

  3. 016 多对多关联映射 单向(many-to-many)

    一般的设计中,多对多关联映射,需要一个中间表 Hibernate会自动生成中间表 Hibernate使用many-to-many标签来表示多对多的关联 多对多的关联映射,在实体类中,跟一对多一样,也是 ...

  4. 日志组件二:log4j2

    一.背景 随着业务服务(Server App)逐渐增加,我们的业务系统中的日志输出面临的问题越来越多,高并发下对磁盘io这块消耗的越来越大,因此,急需要一个高性能且最好能够支持异步输出日志的日志框架, ...

  5. Js之浅谈dom操作

    JavaScript之浅谈dom操作 1.理解dom: DOM(Document Object Model ,文档对象模型)一种独立于语言,用于操作xml,html文档的应用编程接口. 怎么说,我从两 ...

  6. Python可视化:Seaborn库热力图使用进阶

    前言 在日常工作中,经常可以见到各种各种精美的热力图,热力图的应用非常广泛,下面一起来学习下Python的Seaborn库中热力图(heatmap)如何来进行使用. 本次运行的环境为: windows ...

  7. 一天搞定HTML----a标签02

    1.细说a标签 2.代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  8. Eclipse设置文字大小

    1,选择窗口,preference 2,general

  9. a标签点击之后有个虚线边框,怎么去掉

    1.行内处理方式1 <a hidefocus="true" href="#"></a> 2.行内处理方式2,让a标签获得焦点就失去焦点, ...

  10. Vulkan Tutorial 12 Fixed functions

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 早起的图形API在图形渲染管线的许多阶段提供了默认的状态.在Vulkan中,从vie ...