hadoop非mapreduce过程对hdfs文件的读写
来源:互联网 发布:js谷歌地图获取经纬度 编辑:程序博客网 时间:2024/06/05 04:05
package cn.ytu.hdfsrwfile;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;/** * 非mapreduce过程在HDFS上读文件 * @author LiuYinxing * */public class ReadHDFSfile {/** * 非mapreduce过程在HDFS上读取文件 * @param path 文件路径 * @return 内容 * @throws Exception */@SuppressWarnings("deprecation")public List<String> getData(String path) throws Exception{Configuration conf = new Configuration();FileSystem hdfs = FileSystem.get(conf);Path inPath = new Path(path);FSDataInputStream dis = hdfs.open(inPath); String s = null; s = dis.readLine(); String[] strings = s.split(","); List<String> attributesvalue = new ArrayList<>(); for (int i = 0; i < strings.length; i++) {attributesvalue.add(strings[i]);} return attributesvalue;}}
package cn.ytu.hdfsrwfile;import java.io.FileNotFoundException;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;/** * 非mapreduce过程在HDFS上写文件 * @author LiuYinxing * */public class WriteHDFSfile {/** * 非mapreduce过程在HDFS上写文件 * @param path * @throws FileNotFoundException * @throws IOException */ public void writehdfsdata(String path) throws FileNotFoundException, IOException { String string = "hello world i love you";Configuration conf = new Configuration();FileSystem hdfs = FileSystem.get(conf);Path inPath = new Path(path);FSDataOutputStream dos = hdfs.create(inPath);dos.writeBytes(string);dos.close();}}
package cn.ytu.hdfsrwfile;public class HRWmain {public static void main(String[] args) throws Exception {// TODO Auto-generated method stubString path = "hdfs://localhost:9000/user/hadoop/output/helloworld.txt";WriteHDFSfile writeHDFSfile = new WriteHDFSfile();writeHDFSfile.writehdfsdata(path);ReadHDFSfile readHDFSfile = new ReadHDFSfile();for (String string : readHDFSfile.getData(path)) {System.out.print(string+" ");}System.out.println();}}
运行结果如下:
注意如果在eclipse上运行出现如下异常:
请参考上篇博客:
hadoop程序抛出异常:java.lang.IllegalArgumentException: Wrong FS: hdfs:/ expected file:///
0 0
- hadoop非mapreduce过程对hdfs文件的读写
- Hadoop之HDFS文件读写过程
- HDFS读写文件的过程
- Hadoop中hdfs的读写过程
- hadoop HDFS读写文件
- HDFS文件读写过程
- Hadoop 键值对的mapreduce过程剖析
- Hadoop 键值对的MapReduce过程剖析
- Hadoop 键值对的mapreduce过程剖析
- Hadoop读写Hdfs系统文件
- Hadoop中hdfs的体系结构和读写过程
- hadoop文件读写过程
- Hadoop MapReduce之上传文件到HDFS
- Hadoop: MapReduce使用hdfs中的文件
- hadoop的mapreduce过程
- HDFS数据的读写过程
- hadoop 在hdfs中读写文件
- hdfs——hadoop文件读写操作
- 社説 20150322 サリン事件20年 教訓をテロ封じ込めに生かせ
- java语言GUI编程之Anoymous匿名类2
- 社説 20150322 日中韓外相会談 「歴史」乗り越え対話進めたい
- bzoj 1036 [ZJOI2008]树的统计Count 树链剖分点权果题
- 用getchar输入字符,用putchar输出字符
- hadoop非mapreduce过程对hdfs文件的读写
- Maven概述及安装
- Lua weak table
- C++注册开机自启动服务代码
- JS cookie操作
- C/C++ 笔试、面试题目汇总(续)
- 大数取模运算
- mfc程序退出时删除托盘图标
- 大一第二学期第2周【项目4:图书馆的书】