1. 程式人生 > >java Api 讀取HDFS檔案內容

java Api 讀取HDFS檔案內容

package dao;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;

public class HDFSApi {
    /**
     * 讀取檔案內容
     */
    public static void cat(Configuration conf, String remoteFilePath) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        Path remotePath 
= new Path(remoteFilePath); FSDataInputStream in = fs.open(remotePath); BufferedReader d = new BufferedReader(new InputStreamReader(in)); String line = null; while ((line = d.readLine()) != null) { String[] strarray = line.split(" "); for (int i = 0; i < strarray.length; i++) { System.out.print(strarray[i]); System.out.print(
" "); } System.out.println(" "); // System.out.println(line); // System.out.print(strarray[0]); } d.close(); in.close(); fs.close(); } /** * 主函式 */ public static void main(String[] args) { Configuration conf
= new Configuration(); conf.set("fs.default.name", "hdfs://yt:9000"); String remoteFilePath = "/hadoop/hadoop1.txt"; // HDFS路徑 try { System.out.println("讀取檔案: " + remoteFilePath); HDFSApi.cat(conf, remoteFilePath); System.out.println("\n讀取完成"); } catch (Exception e) { e.printStackTrace(); } } }