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 ...
随机推荐
- Android开发中使用static变量应该注意的问题
package com.highxin.launcher01; import java.util.ArrayList; import java.util.HashMap; import java.ut ...
- 关于JS的return false
之前真的不知道JS里的return false 还能跳出事件. 今天在修改BUG的时候,用到了这个,就去查了一下,为了加深记忆在此处做个总结. retrun true: 返回正确的处理结果. retu ...
- LiteIDE灰调配色方案
说明 本文写于2017-04-03,使用LiteIDE X31(基于Qt 4.8.5),操作系统为Windows. 使用 LiteIDE下载后解压即可使用.配色方案的所有配置文件都位于liteide/ ...
- 你对SpringMvc是如何理解的?
SpringMVC工作原理 SpringMvc是基于过滤器对servlet进行了封装的一个框架,我们使用的时候就是在web.xml文件中配置DispatcherServlet类:SpringMvc工作 ...
- css 中的背景图片小技巧和存在的坑
body 的背景图设置 第一种 :这种情况下背景图片可以缩放 但是不能完全等比缩放 background: url(imgs/1.jpg)no-repeat; background-position: ...
- Selenium基础知识
本人博客文章网址:https://www.peretang.com/basic-knowledge-of-selenium/ 什么是Selenium Selenium是一个自动化测试工具 是一组不同的 ...
- 50几个photoshop快捷键
一.常用的热键组合 1.图层混合模式快捷键:正常(Shift + Option + N),正片叠底(Shift + Option + M),滤色(Shift + Option + S),叠加(Shif ...
- DataTable数据导出Excel 并且下载
public string Excel(System.Data.DataTable dt) { //模板的路径 string strUploadPath = HttpContext.Current.S ...
- thinkphp中find()和select()的区别
1.find()是查找符合条件的第一条数据,返回的是一个一维数组: select()是查找符合条件的所有的数据,返回的是一个二维数组: 2.以下案例 $tech=M('techlevel','HR_C ...
- ecshop开发帮助
http://www.ecshop.com/template_tutorial/ ECSHOP模板结构说明 http://help.ecshop.com/index.php ECSHOP帮助中心 ht ...