文件系统操作命令
hdfs文件系统命令基本与linux系统命令相同,只需要在前面名加上hdfs dfs -
命令 | 作用 | 说明 |
---|---|---|
-mkdir | 创建目录 | -p选项递归创建 |
-ls | 显示内容 | -R选项递归显示 |
-put | 上传文件 | |
-copyFrmoLocal | 上传文件 | 与put作用相同 |
-get | 下载文件 | |
-copyToLocal | 下载文件 | 与get作用相同 |
-mv | 移动文件 | 不允许跨文件系统 |
-cp | 复制文件 | 不允许跨文件系统 |
-rm | 删除文件 | -r选项递归删除 |
-cat | 查看文件内容 |
admin操作
命令以hdfs dfsadmin开头
命令 | 作用 |
---|---|
-report | 返回集群的状态信息 |
-metasave | 默认存放到{HADOOP_LOG_DIR}路径下,包含datanode和块的信息 |
-fetchImage | 从namenode节点获取最新的fsimage文件至本地指定目录 |
-setQuota | 设置文件目录配额 |
-setSpaceQuota | 设置文件目录空间配额 |
JavaAPI
第一步:搭建windows下的hadoop环境
1、将hadoop文件夹拷贝到一个没有中文和空格的路径下
2、在windows上配置hadoop环境变量
3、把hadoop文件夹中bin目录下的hadoop.dll文件放到系统盘:C:\Windows\System32目录
4、关闭windows重启
第二步:导入maven依赖
第三步:编写代码
首先连接到hdfs文件系统,并获取FileSystem对象
//第一种方式:URL
package pac1;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class Hdf_API_Test {
@Test
public void test()throws IOException {
//1、注册URL
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
//2、获取hdfs文件输入流
InputStream is = new URL("hdfs://hadoop1:8020/core-site.xml").openStream();
//3、获取本地文件输出流
FileOutputStream fos = new FileOutputStream("D:\\hello.txt");
//4、文件拷贝
IOUtils.copy(is,fos);
//5、关闭流
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(is);
}
}
//第二种方式:使用文件系统方式
//获取FileSystem的几种方式
//第一种方式:
public void getFileSystem1()throws IOException{
//创建Configuration对象
Configuration configuration = new Configuration();
//设置文件系统类型
configuration.set("fs.defaultFs","hdfs://hadoop1:8020");
//生成FileSystem对象
FileSystem system = FileSystem.get(configuration);
System.out.println(system);
}
//第二种方式:
@Test
public void getFileSystem2() throws URISyntaxException, IOException {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop1:8020"), new Configuration());
System.out.println(fileSystem);
}
//第三种方式:
@Test
public void getFileSystem3() throws IOException {
Configuration configuration=new Configuration();
configuration.set("fs.defaultFs","hdfs://hadoop1:8020");
FileSystem fileSystem = FileSystem.newInstance(configuration);
System.out.println(fileSystem);
}
//第四种方式
@Test
public void getFileSystem4() throws IOException, URISyntaxException {
FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://hadoop1:8020"),new Configuration());
System.out.println(fileSystem);
}
//遍历文件
@Test
public void listFile() throws URISyntaxException, IOException {
//获取FileSystem对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop1:8020"), new Configuration());
/*
获取RemoteIterator 方法的第一个参数时要遍历的文件夹路径,第二个参数为true代表要递归获取
*/
RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
while (iterator.hasNext()){
LocatedFileStatus fileStatus = iterator.next();
System.out.println(fileStatus.getPath().getName());
}
fileSystem.close();
}
//创建文件夹
@Test
public void makeDir() throws URISyntaxException, IOException {
FileSystem fileSystem=FileSystem.newInstance(new URI("hdfs://hadoop1:8020"),new Configuration());
fileSystem.mkdirs(new Path("/dir1/apiDictionary"));
fileSystem.close();
}
#//创建文件 fileSystem.create(new Path("xxx"));
/*
下载文件
*/
@Test
public void downLoad() throws URISyntaxException, IOException {
FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://hadoop1:8020"), new Configuration());
// FSDataInputStream open = fileSystem.open(new Path("/hdfs-site.xml"));
// FileOutputStream fos = new FileOutputStream("d://hahaha.xml");
// IOUtils.copy(open,fos);
// IOUtils.closeQuietly(fos);
// IOUtils.closeQuietly(open);
fileSystem.copyToLocalFile(new Path("/hdfs-site.xml"),new Path("d://ha.xml"));
fileSystem.close();
}
/*
上传文件
*/
@Test
public void upload() throws IOException, URISyntaxException {
FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://hadoop1:8020"), new Configuration());
fileSystem.copyFromLocalFile(new Path("d://ha.xml"),new Path("/"));
fileSystem.close();
}
文件权限默认是失效的,因为配置文件hdfs-site.xml中的dfs.permissions值为false
创建fileSystem的两个方法都有一个重载的三变量方法,第三个变量就是字符串形式的用户名,可以以指定的用户取访问文件系统
合并小文件
shell命令模式下:
cd /software
hdfs dfs -getmerge /config/*.xml ./hello.xml #合并下载
可以尝试封装以上方法,生成jar包到linux系统运行
package course.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class TestHDFS {
private FileSystem fs;
public static void main(String[] args) throws IOException {
TestHDFS obj = new TestHDFS();
obj.initFs(args[1], args[2]);
String operatorType = args[0];
switch (operatorType) {
case "mkdir":
obj.createDir(args[3]);
break;
case "touch":
obj.createNewFile(args[3]);
break;
case "del":
obj.deleteFile(args[3], Boolean.parseBoolean(args[4]));
break;
case "downLoad":
obj.downLoad(args[3], args[4]);
break;
case "upLoad":
obj.upLoad(args[3], args[4]);
break;
default:
throw new RuntimeException("指令错误");
}
obj.close();
}
private void upLoad(String src, String dst) {
try {
fs.copyFromLocalFile(new Path(src), new Path(dst));
} catch (IOException e) {
e.printStackTrace();
}
}
private void downLoad(String src, String dst) {
try {
fs.copyToLocalFile(new Path(src), new Path(dst));
} catch (IOException e) {
e.printStackTrace();
}
}
private void deleteFile(String path, Boolean recursion) {
try {
fs.delete(new Path(path), recursion);
} catch (IOException e) {
e.printStackTrace();
}
}
private void createDir(String path) {
try {
fs.mkdirs(new Path(path));
} catch (IOException e) {
e.printStackTrace();
}
}
private void createNewFile(String path) {
try {
fs.createNewFile(new Path(path));
} catch (IOException e) {
e.printStackTrace();
}
}
private void initFs(String uri, String user) {
try {
fs = FileSystem.get(new URI(uri), new Configuration(), user);
} catch (IOException | InterruptedException | URISyntaxException e) {
e.printStackTrace();
}
}
private void close() {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}