1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件;-put覆盖
-appendToFile追加
2)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;
if $(hadoop fs -test -e /home/fenghao);
then $(hadoop fs -copyToLocal text.txt ./text.txt);
else $(hadoop fs -copyToLocal text.txt ./text2.txt);
fi
3)将HDFS中指定文件的内容输出到终端中;cat
4)显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;ls
5)给定HDFS中某一个目录,递归输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息;lsr
6)提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;mkdir -p
if $(hadoop dfs -test -d “dirpath”);then $(hadoop dfs -touch “filepath”); else $(hadoop dfs -mkdir -p “dirpath” && hadoop dfs -touch “filepath”); fi
7)提供一个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在,则自动创建相应目录;删除目录时,当该目录为空时删除,当该目录不为空时不删除该目录;mkdir -p
if $(hadoop dfs -test -z “dirpath”);then $(hadoop dfs -rmdir “dirpath”); else $(); fi
8)向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;appendToFile -localpath -hdfspath 结尾
1.copyToLocal
2.cat file1>>file2 3.copyFromLocal -f
9)删除HDFS中指定的文件; rm
10)在HDFS中,将文件从源路径移动到目的路径。mv
public class HDFS {
public static FileSystem fs;
public static void init(){
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
//conf.set("dfs.replication","1");
try {
fs = FileSystem.get(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void uploads(String localFile,String remoteFile) throws IOException{
//假设 /user/hadoop/input路径已存在 如没存在 在命令行输入 hadoop fs -mkdir -p /user/hadoop/input
init();
//String localFileName = "/home/hadoop/myfile.txt";
//String remoteFileName = "/user/hadoop/input/myfile.txt";
Path localFilePath = new Path(localFile);
Path remoteFilePath = new Path(remoteFile);
if(!fs.exists(remoteFilePath)) {
远程文件不存在
System.out.println("remote don't have this file");
fs.copyFromLocalFile(localFilePath, remoteFilePath);
System.out.println("success upload");
}else {
//远程文件已存在
System.out.println("already exists:1 cover ;2 append ");
int choice = 0;
Scanner input = new Scanner(System.in);
choice = input.nextInt();
switch(choice) {
case 1:fs.copyFromLocalFile(false, true, localFilePath,remoteFilePath);
System.out.println("successful cover");break;
case 2:FSDataOutputStream out = fs.append(remoteFilePath);
FileInputStream in = new FileInputStream(localFile); //字节流
byte buff[] = new byte[1024];
int read = -1;
while((read =in.read(buff))>0) {
//in.read()返回读取数据的长度
out.write(buff, 0, read);
}
out.close()