Skip to content

Instantly share code, notes, and snippets.

@lurenx
Created May 3, 2016 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lurenx/0977228fc52243702fdff6462b840e8e to your computer and use it in GitHub Desktop.
Save lurenx/0977228fc52243702fdff6462b840e8e to your computer and use it in GitHub Desktop.
use hadoop fs api to read file in case of hadoop namenode HA (QJM)
package com.hadoop.hdfs.reader;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class HdfsReader extends Configured implements Tool {
public static final String FS_PARAM_NAME = "fs.defaultFS";
public int run(String[] args) throws Exception {
if (args.length < 2) {
System.err.println("HdfsReader [hdfs input path] [local output path]");
return 1;
}
Path inputPath = new Path(args[0]);
String localOutputPath = args[1];
//Configuration conf = getConf();
Configuration conf = new Configuration(false);
conf.set("fs.defaultFS", "hdfs://yourclustername");
conf.set("fs.default.name", conf.get("fs.defaultFS"));
conf.set("dfs.nameservices","yourclustername");
conf.set("dfs.ha.namenodes.yourclustername", "nn1,nn2");
conf.set("dfs.namenode.rpc-address.yourclustername.nn1","hadoop01.yourclustername:8020");
conf.set("dfs.namenode.rpc-address.yourclustername.nn2", "hadoop07.yourclustername:8020");
conf.set("dfs.client.failover.proxy.provider.yourclustername","org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
System.out.println("configured filesystem = " + conf.get(FS_PARAM_NAME));
FileSystem fs = FileSystem.get(conf);
InputStream is = fs.open(inputPath);
OutputStream os = new BufferedOutputStream(new FileOutputStream(localOutputPath));
IOUtils.copyBytes(is, os, conf);
return 0;
}
public static void main( String[] args ) throws Exception {
int returnCode = ToolRunner.run(new HdfsReader(), args);
System.exit(returnCode);
}
}
@lurenx
Copy link
Author

lurenx commented May 3, 2016

add the export to /etc/profile

export CLASSPATH=/usr/hdp/2.4.0.0-169/hadoop/hadoop-common.jar:/usr/hdp/2.4.0.0-169/hadoop-mapreduce/hadoop-mapreduce-client-core.jar:/usr/hdp/2.4.0.0-169/hadoop/client/commons-cli.jar

and source /etc/profile

and run the following command

javac HdfsReader.java -d hdfs

jar cvf HdfsReader.jar -C hdfs .

hadoop jar HdfsReader.jar com.hadoop.hdfs.reader.HdfsReader /tmp/ida8c04a64_date462516 ./hello_output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment