1package dao; 2 3import org.apache.hadoop.conf.Configuration; 4import org.apache.hadoop.fs.*; 5import java.io.*; 6 7public class HDFSApi { 8 /** 9 * 读取文件内容 10 */ 11 public static void cat(Configuration conf, String remoteFilePath) throws IOException { 12 FileSystem fs = FileSystem.get(conf); 13 Path remotePath = new Path(remoteFilePath); 14 FSDataInputStream in = fs.open(remotePath); 15 BufferedReader d = new BufferedReader(new InputStreamReader(in)); 16 String line = null; 17 while ((line = d.readLine()) != null) { 18 String[] strarray = line.split(" "); 19 for (int i = 0; i < strarray.length; i++) { 20 System.out.print(strarray[i]); 21 System.out.print(" "); 22 23 } 24 25 System.out.println(" "); 26 // System.out.println(line); 27 28 // System.out.print(strarray[0]); 29 } 30 d.close(); 31 in.close(); 32 fs.close(); 33 } 34 35 /** 36 * 主函数 37 */ 38 public static void main(String[] args) { 39 Configuration conf = new Configuration(); 40 conf.set("fs.default.name", "hdfs://yt:9000"); 41 String remoteFilePath = "/hadoop/hadoop1.txt"; // HDFS路径 42 43 try { 44 System.out.println("读取文件: " + remoteFilePath); 45 HDFSApi.cat(conf, remoteFilePath); 46 System.out.println("\n读取完成"); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 } 51}
java Api 读取HDFS文件内容
Wesley13
2021-10-11
1323 1 0
点赞
收藏
评论区
加载中...