2017-09-27 5 views
0

Je tente de construire un programme Hadoop, dont le but est de cat fichiers que j'ai déjà téléchargés sur HDFS, basé en grande partie sur this tutorial, le programme ressemble à ceci:Compilation de programme Hadoop Java avec des dépendances supplémentaires

import java.io.*; 
import java.net.URI; 
import org.apache.hadoop.fs.*; 
import org.apache.hadoop.conf.*; 
import org.apache.hadoop.io.*; 

public class ReadHDFS { 
    public static void main(String[] args) throws IOException { 

     String uri = args[0]; 

     Configuration conf = new Configuration(); 
     FileSystem fs = FileSystem.get(URI.create(uri), conf); 
     FSDataInputStream in = null ; 

     try 
     { 
      in = fs.open(new Path(uri)); 
      IOUtils.copyBytes(in, System.out, 4096, false); 
     } 
     finally 
     { 
      IOUtils.closeStream(in); 
     } 
    } 
} 

Il me semble que le tutoriel est défectueux, Parce que- selon ma compréhension - IOUtils fait partie de la bibliothèque apache.commons. Cependant, bien que j'ai ajouté la ligne suivante au programme que j'ai essayé de déployer:

import org.apache.commons.compress.utils.IOUtils; 

Je suis encore rencontré l'erreur suivante:

enter image description here

C'est:

FileSystemCat.java:37: error: cannot find symbol 
     IOUtils.copyBytes(in, System.out, 4096, false); 
      ^
    symbol: method copyBytes(InputStream,PrintStream,int,boolean) 
    location: class IOUtils 
FileSystemCat.java:40: error: cannot find symbol 
     IOUtils.closeStream(in); 
          ^
    symbol: variable in 
    location: class FileSystemCat 
2 errors 

Je l'exécuter sur le NameNode avec cette commande:

javac -cp /usr/local/hadoop/share/hadoop/common/hadoop-common-2.8.1.jar:/home/ubuntu/job_program/commons-io-2.5/commons-io-2.5.jar FileSystemCat.java 

Répondre

0

annexe nécessaire à ~/.bashrc:

# Classpath for Java 

# export HADOOP_CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath) 
export HADOOP_CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath) 

Comment compule le programme en bas:

javac -cp ${HADOOP_CLASSPATH}:commons-io-2.5.jar ReaderHDFS.java 

Comment générer le fichier jar pour ce programme:

jar cf rhdfs.jar ReaderHDFS*.class 

Run la commande:

$HADOOP_HOME/bin/hadoop jar rhdfs.jar ReaderHDFS hdfs://master:9000/input_1/codes.txt 

Voici le programme:

import org.apache.hadoop.io.IOUtils; 
//import org.apache.commons.io.IOUtils; 
import java.io.*; 
import java.net.URI; 
import org.apache.hadoop.fs.*; 
import org.apache.hadoop.conf.*; 
import org.apache.hadoop.io.*; 

public class ReaderHDFS { 
    public static void main(String[] args) throws IOException { 

     String uri = args[0]; 

     Configuration conf = new Configuration(); 
     FileSystem fs = FileSystem.get(URI.create(uri), conf); 
     FSDataInputStream in = null ; 

     try 
     { 
      in = fs.open(new Path(uri)); 
      IOUtils.copyBytes(in, System.out, 4096, false); 
     } 
     finally 
     { 
      IOUtils.closeStream(in); 
     } 
    } 
}