Maven下从HDFS文件系统读取文件内容
需要注意以下几点
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文件系统读取文件内容的更多相关文章
- 在Spring Boot快捷地读取文件内容的若干种方式
引言: 在Spring Boot构建的项目中,在某些情况下,需要自行去读取项目中的某些文件内容,那该如何以一种轻快简单的方式读取文件内容呢? 基于ApplicationContext读取 在Spri ...
- 7 RandomAccessFile读取文件内容保存--简单例子(需要验证)
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; /** * 读取动态产生的文件内容 */ publ ...
- 使用while和read命令读取文件内容
转:使用while和read命令读取文件内容 1.准备数据文件 $cat a.txt 200:2 300:3 400:4 500:5 2.用while循环从文件中读取数据 #!/bin/ksh whi ...
- 使用 istreambuf_iterator 读取文件内容,赋值给 std::string
需要一个一个字符输入时考虑使用istreambuf_iterator 假设我们要把一个文本文件拷贝到一个字符串对象中.似乎可以用一种很有道理的方法完成: ifstream inputFile(&quo ...
- PHP读取文件内容的五种方式(转载)
php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp); php读取文件内容: -----第一种方法--- ...
- h5-21-文件操作-读取文件内容
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- java读取文件内容常见几种方式
①随机读取文件内容 ②以行为单位读取文件,常用于读面向行的格式化文件 ③以字符为单位读取文件,常用于读文本,数字等类型的文件 ④以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件 pa ...
- shell读取文件内容
Shell脚本,执行解释速度快.代码简单易于理解.在shell代码编写过程中,经常会用到读取文件内容. 写法一: ------------------------------------ ...
- Python跳过第一行读取文件内容
Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...
随机推荐
- (数字IC)低功耗设计入门(一)
低功耗设计这个专题整理了好久,有一个月了,有图有证据: 然而最近一直有些烦心事.郁闷事,拖延了一下,虽然现在还是有点烦,但是还是先发表了吧.下面我们就来聊聊低功耗设计吧,由于文章比较长,因此我就不一次 ...
- 使用Post方法模拟登陆爬取网页
最近弄爬虫,遇到的一个问题就是如何使用post方法模拟登陆爬取网页.下面是极简版的代码: import java.io.BufferedReader; import java.io.InputStre ...
- UIDebuggingInformationOverlay在OC语法中使用
转载请注明出处:http://www.cnblogs.com/pretty-guy/p/6924882.html 你可以从这里下载demo 在微博看到几位大牛再说将UIDebuggingInforma ...
- Java IO流之内存流
内存流 1)内存流主要用来操作内存 2)分类 ByteArrayInputStream 主要完成将内容从内存读入程序之中 ByteArrayOutputStream 主要是将数据写入到内存中. 3)输 ...
- java中的流程控制语句总结
程序的结构分类: 顺序结构:按照写代码的顺序 一次执行 选择结构:根据条件的不同有选择的执行不同的代码 循环结构:在一定条件下 反复执行某一片代码 选择结构: 也叫分支结构 根据条件的不同,有选择的执 ...
- vue2入坑随记(二) -- 自定义动态组件
学习了Vue全家桶和一些UI基本够用了,但是用元素的方式使用组件还是不够灵活,比如我们需要通过js代码直接调用组件,而不是每次在页面上通过属性去控制组件的表现.下面讲一下如何定义动态组件. Vue.e ...
- SVN·最新使用教程总结
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- tostring方法
//__tostring()方法//输出内容时不报错 用法实例:class Ren{ public $name; public function __tostring() { return " ...
- Java基本之数据类型
一.创建一个简单的Java应用程序 public class Code { public static void main(String[]args) { System.out.println(&qu ...
- Behavior的使用(一):页面跳转NavigateToPageAction
Behavior的使用,让UI设计师能够更加方便的进行UI设计,更高效地和开发进行合作.Behavior有三种触发方式:EventTriggerBehavior事件触发,DataTriggerBeha ...