bin/hadoop fs 具体命令 OR bin/hdfs dfs 具体命令
两个是完全相同的。
hdfs dfs df -h
本地推送到HDFS
将LICENSE.txt,推送到HDFS上
hdfs dfs -put LICENSE.txt /
hdfs dfs -copyFromLocal LICENSE.txt /(和put效果相同,不同点在于支持并发)
将文件移动到HDFS上,原始目录不会保留被移动的文件
hdfs dfs -moveFromLocal README.txt /
文件的追加
hdfs dfs -appendToFile README.txt / we.txt
要追加的文件 追加到哪 追加到那个文件
HDFS内部操作
cp
mv
chown
chgrp
chmod
mkdir
du
df
cat
rm
HDFS拉取到本地
hdfs dfs -get / we.txt ./
hdfs dfs -copyToLocal / we.txt ./
哪里的文件 文件名 拉取到哪里
合并下载(将所有已TXT结尾的文件下载本地的1.txt文件中)
hdfs dfs - getmerge /*.txt ./1.txt
哪里的文件 下载到哪 下载到那个文件
设置HDFS中文件的副本数量
注意:副本数量要和节点数量一致,一个节点没有多个副本,一个节点宕机所有副本都会消失,所以一个节点多个副本没有意义,设置副本数量只是在增加节点时可以自动增加副本
hdfs dfs -setrep 10 /README.txt
下载hadoop3.0.0(jdk环境为1.8)
配置HADOOP_HOME环境变量。
配置Path环境变量。然后重启电脑
打开dos窗口输入winutils命令
junit
junit
4.12
org.apache.logging.log4j
log4j-slf4j-impl
2.12.0
org.apache.hadoop
hadoop-client
3.1.3
package com.hanlin.qu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.URI;
public class HdfsClient {
//定义传输对象
FileSystem fileSystem;
//
Configuration configuration;
@Before
public void fileSystem() throws Exception {
configuration = new Configuration();
//创建连接
fileSystem = FileSystem.get(new URI("hdfs://hadoop101:8020"),configuration,"atguigu");
}
//关闭资源
@After
public void close() throws Exception {
fileSystem.close();
}
//上传文件
@Test
public void put() throws Exception {
fileSystem.copyFromLocalFile(new Path(
"E:\\BaiduNetdiskDownload/CentOS-7.5-x86_64-DVD-1804.iso"),new Path("/"));
}
}
package com.hanlin.qu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
public class HdfsClient {
//定义传输对象
FileSystem fileSystem;
//
Configuration configuration;
@Before
public void fileSystem() throws Exception {
configuration = new Configuration();
//创建连接
fileSystem = FileSystem.get(new URI("hdfs://hadoop101:8020"),configuration,"atguigu");
}
//关闭资源
@After
public void close() throws Exception {
fileSystem.close();
}
//下载文件
@Test
public void get() throws IOException {
// boolean delSrc 指是否将原文件删除
// Path src 指要下载的文件路径
// Path dst 指将文件下载到的路径
// boolean useRawLocalFileSystem 是否开启文件校验
fileSystem.copyToLocalFile(true,new Path("/README.txt"),new Path("E:\\"),true);
}
}
8.3HDFS文件夹删除
package com.hanlin.qu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
public class HdfsClient {
//定义传输对象
FileSystem fileSystem;
//
Configuration configuration;
@Before
public void fileSystem() throws Exception {
configuration = new Configuration();
//创建连接
fileSystem = FileSystem.get(new URI("hdfs://hadoop101:8020"),configuration,"atguigu");
}
//关闭资源
@After
public void close() throws Exception {
fileSystem.close();
}
//删除文件
@Test
public void delete() throws IOException {
fileSystem.delete(new Path("/we.txt"),true);
}
package com.hanlin.qu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
public class HdfsClient {
//定义传输对象
FileSystem fileSystem;
//
Configuration configuration;
@Before
public void fileSystem() throws Exception {
configuration = new Configuration();
//创建连接
fileSystem = FileSystem.get(new URI("hdfs://hadoop101:8020"),configuration,"atguigu");
}
//关闭资源
@After
public void close() throws Exception {
fileSystem.close();
}
//HDFS文件名更改
@Test
public void Rename() throws IOException {
fileSystem.rename(new Path("/LICENSE.txt"),new Path("/LICENSE1.txt"));
}
8.5HDFS文件详情查看
package com.hanlin.qu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
public class HdfsClient {
//定义传输对象
FileSystem fileSystem;
//
Configuration configuration;
@Before
public void fileSystem() throws Exception {
configuration = new Configuration();
//创建连接
fileSystem = FileSystem.get(new URI("hdfs://hadoop101:8020"),configuration,"atguigu");
}
//关闭资源
@After
public void close() throws Exception {
fileSystem.close();
}
//查看文件名称、权限、长度、块信息
@Test
public void testListFiles() throws IOException {
// 获取文件详情
RemoteIterator listFiles = fileSystem.listFiles(new Path("/"), true);
while (listFiles.hasNext()){
LocatedFileStatus status = listFiles.next();
// 文件名称
System.out.println(status.getPath().getName());
// 长度
System.out.println(status.getLen());
// 权限
System.out.println(status.getPermission());
// 分组
System.out.println(status.getGroup());
// 获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 获取存储的块信息
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
}
}
}
package com.hanlin.qu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
public class HdfsClient {
//定义传输对象
FileSystem fileSystem;
//
Configuration configuration;
@Before
public void fileSystem() throws Exception {
configuration = new Configuration();
//创建连接
fileSystem = FileSystem.get(new URI("hdfs://hadoop101:8020"),configuration,"atguigu");
}
//关闭资源
@After
public void close() throws Exception {
fileSystem.close();
}
//文件夹的判断
@Test
public void testListStatus() throws IOException {
FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if(fileStatus.isFile()){
System.out.println("是文件:"+fileStatus.getPath().getName());
}else {
System.out.println("不是文件:"+fileStatus.getPath().getName());
}
}
}
输入网址hadoop.apache.org
package com.hanlin.qu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
public class HdfsClient {
//定义传输对象
FileSystem fileSystem;
//
Configuration configuration;
@Before
public void fileSystem() throws Exception {
configuration = new Configuration();
//创建连接
fileSystem = FileSystem.get(new URI("hdfs://hadoop101:8020"),configuration,"atguigu");
}
//关闭资源
@After
public void close() throws Exception {
fileSystem.close();
}
//configuration对集群的配置
@Test
public void block(){
//设置副本数
configuration.set("dfs.replication","6");
}
}