需要注意以下几点

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. 关于JS的页面跳转

    "window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一 ...

  2. Swift 入门之简单语法(三)

    集合 数组 数组使用 [] 定义,这一点与 OC 相同 //: [Int] let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 遍历 for num in nu ...

  3. Workout Wednesday Redux (2017 Week 3)

    I had started a "52 Vis" initiative back in 2016 to encourage folks to get practice making ...

  4. Python有哪些好用的语言翻译方法

    最近有个需求,要将几万条数据从日语翻译成中文.因为数据的获取和处理用的是python代码,所以想先尝试翻译部分也用python实现. 目前网上查到的翻译方法有百度.有道云以及谷歌翻译,下面会对这三个方 ...

  5. Hybrid App开发之JavaScript基础

    前言: 前面学习了html和css的基本使用,今天开始学习JavaScript的使用. 什么是JavaScript JavaScript是一种基于对象(Object)和事件驱动(Event Drive ...

  6. python中defaultdict的用法

    初识defaultdict 之前在使用字典的时候, 用的比较随意, 只是简单的使用dict. 然而这样在使用不存在的key的时候发生KeyError这样的一个报错, 这时候就该defaultdict登 ...

  7. Java中4种类型的内部类 .

    在Java中有4种不同类型的内部类可以使用.下面给出它们的名称和例子. 1.静态嵌套类(Static Nested Classes) class Outer { static class Inner  ...

  8. div,css命名规范!

    命名规则说明: 1).所有的命名最好都小写 2).属性的值一定要用双引号("")括起来,且一定要有值如class="pcss5",id="pcss5& ...

  9. Python的核心数据结构

    数据结构 例子 数字 1234,3.1415,3+4j 字符串 'spam'."grace's" 列表 [1,[2,'three'],4] 字典 {'food':'spam','t ...

  10. 为网页生成二维码(jquery.qrcode.min.js)

    做网站活动页面的时候,要为每个活动生成一个二维码,虽然简单,但还是习惯记录下来. jquery.qrcode.min.js是js的一个库,主流的浏览器都支持:IE6~10, Chrome, Firef ...