资讯专栏INFORMATION COLUMN

HADOOP集群文件上传下载

nevermind / 2387人阅读

摘要:对上的文件进行上传和下载是对集群的基本操作,在权威指南一书中,对文件的上传和下载都有代码的实例,但是对如何配置客户端却是没有讲得很清楚,经过长时间的搜索和调试,总结了一下,如何配置使用集群的方法,以及自己测试可用的对集群上的文件进行操作的程

对HDFS上的文件进行上传和下载是对集群的基本操作,在《HADOOP权威指南》一书中,对文件的上传和下载都有代码的实例,但是对如何配置HADOOP客户端却是没有讲得很清楚,经过长时间的搜索和调试,总结了一下,如何配置使用集群的方法,以及自己测试可用的对集群上的文件进行操作的程序。首先,需要配置对应的环境变量:

</>复制代码

  1. hadoop_HOME="/home/work/tools/java/hadoop-client/hadoop"
  2. for f in $hadoop_HOME/hadoop-*.jar; do
  3. hadoop_CLASSPATH=${hadoop_CLASSPATH}:$f
  4. done
  5. for f in $hadoop_HOME/lib/*.jar; do
  6. hadoop_CLASSPATH=${hadoop_CLASSPATH}:$f
  7. done
  8. hadoopvfs_HOME="/home/work/tools/java/hadoop-client/hadoop-vfs"
  9. for f in $hadoopvfs_HOME/lib/*.jar; do
  10. hadoop_CLASSPATH=${hadoop_CLASSPATH}:$f
  11. done
  12. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/work/tools/java/hadoop-client/hadoop/lib/native/Linux-amd64-64/

其中LD_LIBRARY_PATH是在调用时需要用到的库的路径,hadoop_CLASSPATH则是我们hadoop客户端里各种jar包
有一点需要注意的是最好不要使用HADOOP_HOME这个变量,这个是一个系统使用的环境变量,最好不要和它冲突
编译类的方法:

</>复制代码

  1. javac -classpath $CLASSPATH:$hadoop_CLASSPATH HDFSUtil.java

运行的方法:

</>复制代码

  1. java -classpath $CLASSPATH:$hadoop_CLASSPATH HDFSUtil

但是在实际的使用过程中,会报No Permission之类的错误,或者你能保证代码没有问题的情况下,在运行的时候也会报一些奇奇怪怪的错误
那么问题来了,这是什么鬼?
答案:这是因为没有配置对应集群的配置文件
因为在《HADOOP权威指南》一书中,弱化了配置的东西,所以在具体使用集群的时候就会出现问题,如何解决呢,这样子:

</>复制代码

  1. this.conf = new Configuration(false);
  2. conf.addResource("./hadoop-site.xml");
  3. conf.addResource("./hadoop-default.xml");
  4. conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
  5. conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());

为什么会这样,书上只是很简单的:

</>复制代码

  1. this.conf = new Configuration();

那是因为默认你的集群在本地,所以不需要做配置,但是在实际使用的过程中,各个集群的配置是不同的,所以我们要引入集群的配置
这是非常重要的一点,因为实际使用的过程中我们都是使用的HADOOP的客户端,而且是已经搭好环境的集群,所以我们需要做好本地的配置
hadoop-site.xml和hadoop-default.xml这两个文件在所使用的客户端的conf目录下,在addResource的时候指定好目录就行了

将以上所提到的配置,全部配完之后,这个程序才能真正运行起来,所以配置是非常重要的一环。

以下是对应的工具的代码,有兴趣的看一下吧,使用的是文件流的方式来搞的,这样子也可以打通FTP和HDFS之间文件的互传:

</>复制代码

  1. import java.io.BufferedInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.URI;
  9. import java.net.URL;
  10. import java.io.*;
  11. import org.apache.hadoop.conf.Configuration;
  12. import org.apache.hadoop.fs.FSDataInputStream;
  13. import org.apache.hadoop.fs.FileSystem;
  14. import org.apache.hadoop.fs.Path;
  15. import org.apache.hadoop.io.IOUtils;
  16. import org.apache.hadoop.util.Progressable;
  17. public class HDFSUtil {
  18. private String hdfs_node = "";
  19. private String hdfs_path = "";
  20. private String file_path = "";
  21. private String hadoop_site = "";
  22. private String hadoop_default = "";
  23. private Configuration conf = null;
  24. public HDFSUtil(String hdfs_node) {
  25. this.hdfs_node = hdfs_node;
  26. }
  27. public String getHdfsNode() {
  28. return this.hdfs_node;
  29. }
  30. public void setHdfsPath(String hdfs_path){
  31. this.hdfs_path = hdfs_path;
  32. }
  33. public String getHdfsPath(){
  34. return this.hdfs_path;
  35. }
  36. public void setFilePath(String file_path){
  37. this.file_path = file_path;
  38. }
  39. public String getFilePath(){
  40. return this.file_path;
  41. }
  42. public void setHadoopSite(String hadoop_site){
  43. this.hadoop_site = hadoop_site;
  44. }
  45. public String getHadoopSite(){
  46. return this.hadoop_site;
  47. }
  48. public void setHadoopDefault(String hadoop_default){
  49. this.hadoop_default = hadoop_default;
  50. }
  51. public String getHadoopDefault(){
  52. return this.hadoop_default;
  53. }
  54. public int setConfigure(boolean flag) {
  55. if (flag == false){
  56. if (this.getHadoopSite() == "" || this.getHadoopDefault() == ""){
  57. return -1;
  58. }
  59. else {
  60. this.conf = new Configuration(false);
  61. conf.addResource(this.getHadoopDefault());
  62. conf.addResource(this.getHadoopSite());
  63. conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
  64. conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
  65. return 0;
  66. }
  67. }
  68. this.conf = new Configuration();
  69. return 0;
  70. }
  71. public Configuration getConfigure() {
  72. return this.conf;
  73. }
  74. public int upLoad(String localName, String remoteName) throws FileNotFoundException, IOException {
  75. InputStream inStream = null;
  76. FileSystem fs = null;
  77. try{
  78. inStream = new BufferedInputStream(new FileInputStream(localName));
  79. fs = FileSystem.get(URI.create(this.hdfs_node), this.conf);
  80. OutputStream outStream = fs.create(new Path(remoteName) ,new Progressable() {
  81. public void progress(){
  82. System.out.print(".");
  83. }
  84. });
  85. IOUtils.copyBytes(inStream, outStream, 4096, true);
  86. inStream.close();
  87. return 0;
  88. } catch (IOException e){
  89. inStream.close();
  90. e.printStackTrace();
  91. return -1;
  92. }
  93. }
  94. public int upLoad(InputStream inStream, String remoteName) throws FileNotFoundException, IOException {
  95. FileSystem fs = null;
  96. try{
  97. fs = FileSystem.get(URI.create(this.hdfs_node), this.conf);
  98. OutputStream outStream = fs.create(new Path(remoteName) ,new Progressable() {
  99. public void progress(){
  100. System.out.print(".");
  101. }
  102. });
  103. IOUtils.copyBytes(inStream, outStream, 4096, true);
  104. inStream.close();
  105. return 0;
  106. } catch (IOException e){
  107. inStream.close();
  108. e.printStackTrace();
  109. return -1;
  110. }
  111. }
  112. public int donwLoad(String remoteName, String localName, int lines) throws FileNotFoundException, IOException {
  113. FileOutputStream fos = null;
  114. InputStreamReader isr = null;
  115. BufferedReader br = null;
  116. String str = null;
  117. OutputStreamWriter osw = null;
  118. BufferedWriter buffw = null;
  119. PrintWriter pw = null;
  120. FileSystem fs = null;
  121. InputStream inStream = null;
  122. try {
  123. fs = FileSystem.get(URI.create(this.hdfs_node + remoteName), this.conf);
  124. inStream = fs.open(new Path(this.hdfs_node + remoteName));
  125. fos = new FileOutputStream(localName);
  126. osw = new OutputStreamWriter(fos, "UTF-8");
  127. buffw = new BufferedWriter(osw);
  128. pw = new PrintWriter(buffw);
  129. isr = new InputStreamReader(inStream, "UTF-8");
  130. br = new BufferedReader(isr);
  131. while((str = br.readLine()) != null && lines > 0){
  132. lines--;
  133. pw.println(str);
  134. }
  135. } catch (IOException e){
  136. throw new IOException("Couldn"t write.", e);
  137. } finally {
  138. pw.close();
  139. buffw.close();
  140. osw.close();
  141. fos.close();
  142. inStream.close()
  143. }
  144. return 0;
  145. }
  146. //main to test
  147. public static void main(String[] args){
  148. String hdfspath = null;
  149. String localname = null;
  150. String hdfsnode = null;
  151. int lines = 0;
  152. if (args.length == 4){
  153. hdfsnode = args[0];
  154. hdfspath = args[1];
  155. localname = args[2];
  156. lines = Integer.parseInt(args[3]);
  157. }
  158. else{
  159. hdfsnode = "hdfs://nj01-nanling-hdfs.dmop.baidu.com:54310";
  160. hdfspath = "/app/ps/spider/wdmqa/wangweilong/test/HDFSUtil.java";
  161. localname = "/home/work/workspace/project/dhc2-0/dhc/base/ftp/papapa";
  162. lines = 5;
  163. }
  164. HDFSUtil hdfsutil = new HDFSUtil(hdfsnode);
  165. hdfsutil.setFilePath(hdfsutil.getHdfsNode()+hdfspath);
  166. hdfsutil.setHadoopSite("./hadoop-site.xml");
  167. hdfsutil.setHadoopDefault("./hadoop-default.xml");
  168. hdfsutil.setConfigure(false);
  169. try {
  170. hdfsutil.donwLoad(hdfspath, localname, lines);
  171. } catch (IOException e){
  172. e.printStackTrace();
  173. }
  174. }

如果想要了解FTP上文件的下载,请参考这篇文章:
ftp下载工具

如果想要打通FTP和HDFS文件互传,只要创建一个类,调用这两篇文章中的工具的接口就可以搞定,自己写的代码,实测有效。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64287.html

相关文章

  • Hadoop HA集群 与 开发环境部署

    摘要:伪分布模式在单节点上同时启动等个进程,模拟分布式运行的各个节点。完全分布式模式正常的集群,由多个各司其职的节点构成。在之前在集群中存在单点故障。正确的下载链接会有,这个就是公司需要用户在下载时提供的注册信息。每一次 Hadoop 生态的更新都是如此令人激动像是 hadoop3x 精简了内核,spark3 在调用 R 语言的 UDF 方面,速度提升了 40 倍所以该文章肯定得配备上最新的生态h...

    番茄西红柿 评论0 收藏2637
  • 基于Docker搭建Hadoop集群之升级版

    摘要:总之,项目还算很受欢迎吧,这篇博客将介绍项目的升级版。一项目介绍将打包到镜像中,就可以快速地在单个机器上搭建集群,这样可以方便新手测试和学习。之前的版本使用为集群提供服务,由于网络功能更新,现在并不需要了。运行参考第二部分启动,并运行。 摘要: kiwenlau/hadoop-cluster-docker是去年参加Docker巨好玩比赛开发的,得了二等奖并赢了一块苹果手表,目前这个项目...

    Zoom 评论0 收藏0

发表评论

0条评论

nevermind

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<