【转】Java从hdfs上读取文件中的某一行
【From】https://blog.csdn.net/u010989078/article/details/51790166
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 版权声明:本文为博主原创文章,转载请附上博文链接!