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 stub
String 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:///