2017-11-07 8 views
-1

Gibt es eine Möglichkeit, Zeilen von HDFS-Verzeichnis in JAVA zu zählen, wie wir tun, indem Sie folgenden Befehl an der Eingabeaufforderung tun?Hdfs Datei Zeilenanzahl

hadoop fs -cat /abc/def/* | wc -l 

Insbesondere mit HADOOP API anstelle von Map-Reduce oder Spark-Code schreiben.

Antwort

2

So etwas sollte funktionieren: -

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.FSDataInputStream; 
import org.apache.hadoop.fs.FileStatus; 
import org.apache.hadoop.fs.FileSystem; 
import org.apache.hadoop.fs.Path; 

public class LineCounter { 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 

     Configuration conf = new Configuration(); 
     conf.addResource(new FileInputStream("hdfs-site.xml")); 
     conf.addResource(new FileInputStream("core-site.xml")); 

     conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName()); 
     conf.set("fs.file.impl",org.apache.hadoop.fs.LocalFileSystem.class.getName()); 

     FileSystem fs = FileSystem.get(conf); 
     Path pt = new Path("/some/path"); 

     FileStatus[] status = fs.listStatus(pt); 

     int count = 0; 

     for(FileStatus f : status){ 
      if(f.isFile()){ 
       FSDataInputStream inputStream = fs.open(f.getPath()); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 

       String line = reader.readLine(); 

       while(line!=null){ 
        count++; 
        line = reader.readLine(); 
       } 

       if(reader!=null){ 
        reader.close(); 
       } 
      } 
     } 

    } 

}