码迷,mamicode.com
首页 > 其他好文 > 详细

hadoop中常用的hdfs代码操作

时间:2018-12-06 12:04:13      阅读:1017      评论:0      收藏:0      [点我收藏+]

标签:文件内容   color   txt   from   data   cal   except   主函数   trace   

一:向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件:

技术分享图片
 1 package hadoopTest;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.fs.*;
 5 import java.io.*;
 6 
 7 
 8 public class HDFSApi {
 9      /**
10      * 判断路径是否存在
11      */
12     public static boolean test(Configuration conf, String path) throws IOException {
13         FileSystem fs = FileSystem.get(conf);
14         return fs.exists(new Path(path));
15     }
16 
17     /**
18      * 复制文件到指定路径
19      * 若路径已存在,则进行覆盖
20      */
21     public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException {
22         FileSystem fs = FileSystem.get(conf);
23         Path localPath = new Path(localFilePath);
24         Path remotePath = new Path(remoteFilePath);
25         /* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */
26         fs.copyFromLocalFile(false, true, localPath, remotePath);
27         fs.close();
28     }
29  
30     /**
31      * 追加文件内容
32      */
33     public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException {
34         FileSystem fs = FileSystem.get(conf);
35         Path remotePath = new Path(remoteFilePath);
36         /* 创建一个文件读入流 */
37         FileInputStream in = new FileInputStream(localFilePath);
38         /* 创建一个文件输出流,输出的内容将追加到文件末尾 */
39         FSDataOutputStream out = fs.append(remotePath);
40         /* 读写文件内容 */
41         byte[] data = new byte[1024];
42         int read = -1;
43         while ( (read = in.read(data)) > 0 ) {
44             out.write(data, 0, read);
45         }
46         out.close();
47         in.close();
48         fs.close();
49     }
50     /**
51      * 主函数
52      */
53     public static void main(String[] args) {
54         Configuration conf = new Configuration();
55     conf.set("fs.default.name","hdfs://localhost:9000");
56         String localFilePath = "/home/flyuz/text.txt";    // 本地路径
57         String remoteFilePath = "/text.txt";    // HDFS路径
58         String choice = "append";    // 若文件存在则追加到文件末尾
59 //        String choice = "overwrite";    // 若文件存在则覆盖
60         try {
61             /* 判断文件是否存在 */
62             Boolean fileExists = false;
63             if (HDFSApi.test(conf, remoteFilePath)) {
64                 fileExists = true;
65                 System.out.println(remoteFilePath + " 已存在.");
66             } else {
67                 System.out.println(remoteFilePath + " 不存在.");
68             }
69             /* 进行处理 */
70             if ( !fileExists) { // 文件不存在,则上传
71                 HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath);
72                 System.out.println(localFilePath + " 已上传至 " + remoteFilePath);
73             } else if ( choice.equals("overwrite") ) {    // 选择覆盖
74                 HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath);
75                 System.out.println(localFilePath + " 已覆盖 " + remoteFilePath);
76             } else if ( choice.equals("append") ) {   // 选择追加
77                 HDFSApi.appendToFile(conf, localFilePath, remoteFilePath);
78                 System.out.println(localFilePath + " 已追加至 " + remoteFilePath);
79             }
80         } catch (Exception e) {
81             e.printStackTrace();
82         }
83     }
84 }
追加或覆盖

二:从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;

三:将HDFS中指定文件的内容输出到终端中;

四:显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;

 

hadoop中常用的hdfs代码操作

标签:文件内容   color   txt   from   data   cal   except   主函数   trace   

原文地址:https://www.cnblogs.com/flyuz/p/10075644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!