1、提示:1) 部分Shell命令的参数路径只能是本地路径或者HDFS路径。2) 若Shell命令的参数既可以是本地路径,也可以是HDFS路径时,务必注意区分。为保证操作正确,可指定路径前缀 或者 注意区分相对路径与绝对路径3) 具体命令的说明可参考教材或 (1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;Shell命令:检查文件是否存在: ./hdfs dfs -test -e (执行完这一句不会输出结果,需要继续输入命令 echo $)追加命令: ./hdfs dfs -appendToFile 覆盖命令1: ./hdfs
2、 dfs -copyFromLocal -f 覆盖命令2: ./hdfs dfs -cp -f 也可以使用如下命令实现:(如下代码可视为一行代码,在终端中输入第一行代码后,直到输入 fi 才会真正执行):if $(./hdfs dfs -test -e ;then $(./hdfs dfs -appendToFile ;else $(./hdfs dfs -copyFromLocal -f ;fiJava代码:import .*;public class HDFSApi /* * 判断路径是否存在 */ public static boolean test(Configuration conf
3、, String path) throws IOException FileSystem fs = (conf); return (new Path(path); * 复制文件到指定路径 * 若路径已存在,则进行覆盖 public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException Path localPath = new Path(localFilePath); Path remotePath = new Path(r
4、emoteFilePath); /* 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */ (false, true, localPath, remotePath); (); * 追加文件内容 public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException /* 创建一个文件读入流 */ FileInputStream in = new FileInputStream(localFilePath); /* 创建一个文件输出
5、流,输出的内容将追加到文件末尾 */ FSDataOutputStream out = (remotePath); /* 读写文件内容 */ byte data = new byte1024; int read = -1; while ( (read = (data) 0 ) (data, 0, read); * 主函数 public static void main(String args) Configuration conf = new Configuration(); (,); String localFilePath = /home/hadoop/; ; else + 不存在. /*
6、 进行处理 */ if ( !fileExists) (2)hdfs dfs -test -e ;then $(./hdfs dfs -copyToLocal ./;else $(./hdfs dfs -copyToLocal ./; * 下载文件到本地 * 判断本地路径是否已存在,若已存在,则自动进行重命名 public static void copyToLocal(Configuration conf, String remoteFilePath, String localFilePath) throws IOException File f = new File(localFilePa
7、th); /* 如果文件名存在,自动重命名(在文件名后面加上 _0, _1 .) */ if () + 已存在. Integer i = 0; while (true) f = new File(localFilePath + _ + (); if (!() localFilePath = localFilePath + + (); break; 将重新命名为: + localFilePath);(3)hdfs dfs -cat * 读取文件内容 public static void cat(Configuration conf, String remoteFilePath) throws I
8、OException FSDataInputStream in = (remotePath); BufferedReader d = new BufferedReader(new InputStreamReader(in); String line = null; while ( (line = () != null ) String remoteFilePath = /user/hadoop/(4)hdfs dfs -ls -h import class HDFSApi * 显示指定文件的信息 public static void ls(Configuration conf, String
9、remoteFilePath) throws IOException FileStatus fileStatuses = (remotePath); for (FileStatus s : fileStatuses) 路径: + ().toString();权限:大小: /* 返回的是时间戳,转化为时间日期格式 */ Long timeStamp = (); SimpleDateFormat format = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss String date = (timeStamp);时间: + date);(5)hdfs dfs -ls -R -h /user/hadoop * 显示指定文件夹下所有文件的信息(递归) public static void lsDir(Configuration conf, String remoteDir) throws IOException Path dirPath = new Path(remoteDir); /* 递归获取目录下的所有文件 */ RemoteIterator remoteIterator = (dirPath, true); /* 输出每个文件的信息 */ while () FileStatus s = (); String remoteD
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1