【From】http://www.javashuo.com/article/p-dyjyirdz-dc.htmljava
package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class ReadHDFS { public static String getStringByTXT(String txtFilePath, Configuration conf) { StringBuffer buffer = new StringBuffer(); FSDataInputStream fsr = null; BufferedReader bufferedReader = null; String lineTxt = null; try { FileSystem fs = FileSystem.get(URI.create(txtFilePath),conf); fsr = fs.open(new Path(txtFilePath)); bufferedReader = new BufferedReader(new InputStreamReader(fsr)); while ((lineTxt = bufferedReader.readLine()) != null) { if(lineTxt.split("\t")[0].trim().equals("00067")){ return lineTxt; } } } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return lineTxt; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Configuration conf = new Configuration(); String txtFilePath = "hdfs://master:9000/OutPutBus/line/part-r-00000"; String mbline = getStringByTXT(txtFilePath, conf); System.out.println(mbline); } } --------------------- 做者:cao_yaqi 來源:CSDN 原文:https://blog.csdn.net/u010989078/article/details/51790166 版權聲明:本文爲博主原創文章,轉載請附上博文連接!