读取hdfs文件内容

基础环境: cdh2.71

需要注意: url地址参照

<property>
<name>dfs.namenode.servicerpc-address</name>
<value>node11.com:8022</value>
</property>
hdfs url地址参照 hdfs-site.xml

public static void main(String[] args) throws Exception{

public static String readHDFSDatas(String url) {
// 第一个参数传递进来的是hadoop文件系统中的某个文件的URI,以hdfs://ip 的theme开头
String str="";
Configuration conf = new Configuration();
// conf.set("ts.node1.com", "hadoop-user,hadoop-user");
conf.set("ts.node1.com", "");


FileSystem fs;
FSDataInputStream in = null;
String readLine ="";
try {
fs = FileSystem.get(URI.create(url), conf);
in = fs.open(new Path(url));
//IOUtils.copyBytes(in, System.out, 5000, false);
BufferedReader br = new BufferedReader(new
InputStreamReader(in));
String red2 = br.readLine();
while (red2 != null){
readLine = readLine+ " " + red2 ;
red2 = br.readLine();
}
br.close();
in.close();

} catch (IOException e) {

e.printStackTrace();
} finally {
IOUtils.closeStream(in);
}
return readLine;


}

    } 
上一篇:java对txt文件内容追加


下一篇:综合应用WPF/WCF/WF/LINQ之五:将Workflow使用WCF技术Host到IIS中,并实现调用