接 http://freewxy.iteye.com/admin/blogs/1102759 ,检查hdfs上是否有/user/hadoop/input文件
执行完毕后检查/home/hadoop/tmp/program_getFromHdfs_input文件是否生成
package GetFromHdfs; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class GetFromHdfs { /** * 将文件从hdfs上下载回local * src为hdfs的来源 * dst为local的目的端 */ static boolean getFromHdfs(String src,String dst, Configuration conf) { Path dstPath = new Path(src); try { // 获取操作hdfs的对象 FileSystem hdfs = dstPath.getFileSystem(conf); // 下載 hdfs.copyToLocalFile(false, new Path(src),new Path(dst)); } catch (IOException e) { e.printStackTrace(); return false; } return true; } static public void main(String args[]){ Configuration conf = new Configuration(); String src = "/user/hadoop/input"; String dst = "/home/hadoop/tmp/program_getFromHdfs_input"; boolean status = getFromHdfs(src, dst, conf); System.err.println("download? :" + status); } }
1、将该java代码打包为HelloHadoop.jar
选择程序入口时注意如图选择:
--------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
2、将HelloHadoop.jar从workspace复制到/usr/local/hadoop目录下(你自己的hadoop目录下)
3、运行hadoop框架(伪分布式): $ bin/start-all.sh
4、运行HelloHadoop.jar : $bin/hadoop jar HelloHadoop.jar