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 ...
随机推荐
- 【Netty】EventLoop和线程模型
一.前言 在学习了ChannelHandler和ChannelPipeline的有关细节后,接着学习Netty的EventLoop和线程模型. 二.EventLoop和线程模型 2.1. 线程模型 线 ...
- python 字符串常用方法
字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...
- Detailed Information for Outputted Files from Somatic Mutation Annotators(annovar 注释文件条目详细解释)
CONTENTS *_annoTable.txt (ANNOVAR) *_annoTable.txt (SnpEff) *_genelist.txt (ANNOVAR & SnpEff) db ...
- MySQL的复制
1.复制概述1.1.复制解决的问题数据复制技术有以下一些特点:(1) 数据分布(2) 负载平衡(load balancing)(3) 备份(4) 高可用性(high avail ...
- Java对字符串进行的操作
本篇总结归纳对字符串或数组进行相关操作问题 数组倒序输出 查找字符串中第一次重复的字符 查找字符串中第一次没有重复的字符 删除字符串中重复的元素 倒序输出问题 第一种:对于数组 public int[ ...
- JavaSE教程-02Java基本语法-思维导图
思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 2)右击在新标签中打开放大查看 1.注释 定义:用于解释说明程序作用的文字 注释类别 单行注释 格式: //注释文字 多行注释 格式: ...
- a标签点击之后有个虚线边框,怎么去掉
1.行内处理方式1 <a hidefocus="true" href="#"></a> 2.行内处理方式2,让a标签获得焦点就失去焦点, ...
- 16.3Sum Closet
思路: 暴力,复杂度为 \(O(n^3)\),超时 class Solution { public: int threeSumClosest(vector<int>& nums, ...
- OpenGL ES 2.0 渲染管线 学习笔记
图中展示整个OpenGL ES 2.0可编程管线 图中Vertex Shader和Fragment Shader 是可编程管线: Vertex Array/Buffer objects 顶点数据来源, ...
- selenium+python等待时间
等待时间可以有多种 1.硬等待 import time time.sleep(x)#等待x秒 2.浏览器每次查找一个元素都进行等待 import time br.implicitly_wait(x)# ...