命令基本格式:
hadoop fs -cmd < args >
- ls
hadoop fs -ls /
hadoop fs -ls -R /
- put
hadoop fs -put < local file > < hdfs file >
hdfs file的父目录一定要存在,否则命令不会执行
hadoop fs -put < local file or dir >...< hdfs dir >
hdfs dir 一定要存在,否则命令不会执行
hadoop fs -put - < hdsf file>
从键盘读取输入到hdfs file中,按Ctrl+D结束输入,hdfs file不能存在,否则命令不会执行
- moveFromLocal
hadoop fs -moveFromLocal < local src > ... < hdfs dst >
与put相类似,命令执行后源文件 local src 被删除,也可以从从键盘读取输入到hdfs file中
- copyFromLocal
hadoop fs -copyFromLocal < local src > ... < hdfs dst >
与put相类似,也可以从从键盘读取输入到hdfs file中
- get
hadoop fs -get < hdfs file > < local file or dir>
local file不能和 hdfs file名字不能相同,否则会提示文件已存在,没有重名的文件会复制到本地
hadoop fs -get < hdfs file or dir > ... < local dir >
注意:如果用户不是root, local 路径要为用户文件夹下的路径,否则会出现权限问题,
- copyToLocal
hadoop fs -copyToLocal < local src > ... < hdfs dst >
与get相类似
- rm
hadoop fs -rm < hdfs file > ...
hadoop fs -rm -r < hdfs dir>...
- mkdir
hadoop fs -mkdir < hdfs path>
只能一级一级的建目录,父目录不存在的话使用这个命令会报错
hadoop fs -mkdir -p < hdfs path>
所创建的目录如果父目录不存在就创建该父目录
- getmerge
hadoop fs -getmerge < hdfs dir > < local file >
将hdfs指定目录下所有文件排序后合并到local指定的文件中,文件不存在时会自动创建,文件存在时会覆盖里面的内容
hadoop fs -getmerge -nl < hdfs dir > < local file >
加上nl后,合并到local file中的hdfs文件之间会空出一行
- cp
hadoop fs -cp < hdfs file > < hdfs file >
目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件还存在
hadoop fs -cp < hdfs file or dir >... < hdfs dir >
目标文件夹要存在,否则命令不能执行
- mv
hadoop fs -mv < hdfs file > < hdfs file >
目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件不存在
hadoop fs -mv < hdfs file or dir >... < hdfs dir >
源路径有多个时,目标路径必须为目录,且必须存在。
注意:跨文件系统的移动(local到hdfs或者反过来)都是不允许的
- count
hadoop fs -count < hdfs path >
HDFS JavaAPI
package Hdfs;
import com.sun.xml.internal.ws.api.ha.StickyFeature;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.hdfs.util.IoUtilsClient;
import org.apache.hadoop.io.IoUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HdfsTest {
private static Configuration conf = new Configuration();
static {
//部署在远程服务器上的集群
conf.set("fs.defaultFS", "172.18.74.236:9000");
}
//创建新文件
public static void createFile(String dst, byte[] contents) throws IOException
{
FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(dst);//目标路径
//打开一个输出流
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contents);
outputStream.close();
fs.close();
System.out.println("文件创建成功");
}
//将本地文件导入到Hdfs中
public static void uploadFile(String src,String dst) throws IOException{
//Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(src); //本地上传文件路径
Path dstPath = new Path(dst); //hdfs目标路径
//调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为false
fs.copyFromLocalFile(false, srcPath, dstPath);
//打印文件路径
System.out.println("Upload to "+conf.get("fs.default.name"));
System.out.println("------------list files------------"+"\n");
FileStatus [] fileStatus = fs.listStatus(dstPath);
for (FileStatus file : fileStatus)
{
System.out.println(file.getPath());
}
fs.close();
}
public static void upload(String src,String dst) throws IOException{
FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(dst); //hdfs目标路径
FSDataOutputStream os = fs.create(dstPath);
FileInputStream is = new FileInputStream(src);
org.apache.commons.io.IoUtils.copy(is,os);
}
//文件重命名
public static void rename(String oldName, String newName) throws IOException{
FileSystem fs = FileSystem.get(conf);
Path oldpath = new Path(oldName);
Path newPath = new Path(newName);
boolean isok = fs.rename(oldpath, newPath);
if(isok){
System.out.println("rename ok!");
}
else {
System.out.println("rename failure");
}
fs.close();
}
//删除文件
public static void delete(String filePath) throws IOException{
FileSystem fs = FileSystem.get(conf);
Path path = new Path(filePath);
boolean isok = fs.deleteOnExit(path);
if(isok){
System.out.println("delect ok");
}
else {
System.out.println("delect failure");
}
fs.close();
}
//创建目录
public static void mkdir(String path) throws IOException{
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(path);
boolean isok = fs.mkdirs(srcPath);
if (isok){
System.out.println("create " + path + " dir ok !");
}
else{
System.out.println("create "+ path +" dir failure!");
}
fs.close();
}
//读取文件中的内容
public static void readFile(String filePath) throws IOException{
FileSystem fs = FileSystem.get(conf);
Path fielPath = new Path(filePath);
InputStream in =null;
try{
in =fs.open(fielPath);
IoUtils.copyBytes(in, System.out, 4096, false);
}
finally {
IoUtils.closeStream(in);
}
}
/**
* 遍历制定目录下的所有文件
*/
public static void getDiretoryFromHdfs(String direPath){
try {
FileSystem fs = FileSystem.get(conf);
FileStatus[] filelist = fs.listStatus(new Path(direPath));
for (int i = 0; i < filelist.length; i++){
System.out.println("______" + direPath + "目录下的所有文件_________");
FileStatus fileStatus = filelist[i];
System.out.println("Name: "+fileStatus.getPath().getName());
System.out.println("Size: "+fileStatus.getLen());
System.out.println("Path: "+ fileStatus.getPath());
}
fs.close();
} catch (FileNotFoundException e) {
e.printstacktrace();
} catch (IOException e) {
e.printstacktrace();
}
}
public static void main(String[] args) throws IOException{
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String localFilePath = "C:\\Users\\Charon\\Desktop\\To do.txt";
String hdfsFilePath = "/Test" +today.substring(0,7) + "/upload_date=" + today + "/";
//1. 遍历根目录下的所有文件
getDiretoryFromHdfs("/");
//2. 新建目录
// mkdir(hdfsFilePath);
//3. 上传文件
// uploadFile(localFilePath,hdfsFilePath);
// getDiretoryFromHdfs(hdfsFilePath);
//4. 读取文件
// readFile("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/To do.txt");
//5. 重命名
// rename("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/To do.txt","hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/Test.txt");
//6. 创建文件,并向文件写入内容
// byte[] contents = "\n2019年5月26日20:22:37 添加写入内容\n".getBytes();
// createFile("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/Test1.txt",contents);
// readFile("hdfs://172.18.74.236:9000/Test2019-05/upload_date=2019-05-26/Test1.txt");
//7. 删除文件
delect(hdfsFilePath);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。