Hadoop 基本API 使用 从HDFS文件系统 上读取文件
不多说 看代码:
如果报错 wutils.exe 之类:
请注意一定要添加这一行:
System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.3");
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* 类说明
*
* <pre>
* Modify Information:
* Author Date Description
* ============ =========== ============================
* DELL 2017年3月23日 Create this file
* </pre>
*
*/
public class ReadFromHDFS {
/**
* @param args
* @throws Throwable
*/
public static void main(String[] args) throws Throwable {
String[] ioArg = new String[]{"hdfs://192.168.233.128:9000/user/wangxiaoming/bankstatementconfig/bankstatementconfig.txt"};
String path = ioArg[0];
// String confFile = args[1];
Configuration conf = new Configuration();
// FileInputStream fis = new FileInputStream(confFile);
// conf.addResource(fis);
conf.set("mapred.jop.tracker", "hdfs://192.168.233.128:9001");
conf.set("fs.default.name", "hdfs://192.168.233.128:9000");
System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.3");
FileSystem fileSystem = FileSystem.get(conf);
FSDataInputStream fs = fileSystem.open(new Path(path));
//GBK
BufferedReader bis = new BufferedReader(new InputStreamReader(fs,"GBK"));
// FileWriter fos = new FileWriter(args[2]);
// BufferedWriter bw = new BufferedWriter(fos);
String temp;
int i=0;
while ((temp = bis.readLine()) != null) {
// bw.write(temp);
System.out.println(temp);
// if(temp.startsWith("</doc>")) {
// break;
// }
// if(temp.indexOf("上海") > -1) {
// System.out.println("发现你了 coming");
// break;
// }
}
// bw.close();
bis.close();
fileSystem.close();
}
}