1. hdfs shell
- hadoop fs -ls /
三种形式。
(1)hadoop fs -ls hdfs://xxx:9000/ (指定集群)
(2)hadoop fs -ls / (默认集群)
(3)hadoop fs -ls a(特殊用法)
2. FileSystem的使用
maven依赖
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2-4101</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.2-4101</version>
</dependency>
官方api:https://hadoop.apache.org/docs/r2.7.7/api/index.html
org.apache.hadoop.fs 下的FileSystem
实现有 DistributedFileSystem
该类是个抽象类,只能通过来类的get方法得到具体类。
构造方法
该类是一个抽象类,通过以下两种静态工厂方法可以过去FileSystem实例:
public staticFileSystem.get(Configuration conf) throws IOException
public staticFileSystem.get(URI uri, Configuration conf) throws IOException
常见操作:
具体方法实现
1、public boolean mkdirs(Path f) throws IOException
一次性新建所有目录(包括父目录), f是完整的目录路径。
2、public FSOutputStream create(Path f) throws IOException
创建指定path对象的一个文件,返回一个用于写入数据的输出流
create()有多个重载版本,允许我们指定是否强制覆盖已有的文件、文件备份数量、写入文件缓冲区大小、文件块大小以及文件权限。
3、public boolean copyFromLocal(Path src, Path dst) throws IOException
将本地文件拷贝到文件系统
4、public boolean exists(Path f) throws IOException
检查文件或目录是否存在
5、public boolean delete(Path f, Boolean recursive)
永久性删除指定的文件或目录,如果f是一个空目录或者文件,那么recursive的值就会被忽略。只有recursive=true时,一个非空目录及其内容才会被删除。
6、FileStatus类
封装了文件系统中文件和目录的元数据,包括文件长度、块大小、备份、修改时间、所有者以及权限信息。
通过"FileStatus.getPath()
"可查看指定HDFS中某个目录下所有文件。
可以按一下方式初始化:
//initialization
static Configuration conf = newConfiguration();
static FileSystem hdfs;
static {
String path ="/usr/java/hadoop-1.0.3/conf/";
conf.addResource(newPath(path + "core-site.xml"));
conf.addResource(newPath(path + "hdfs-site.xml"));
conf.addResource(newPath(path + "mapred-site.xml"));
path ="/usr/java/hbase-0.90.3/conf/";
conf.addResource(newPath(path + "hbase-site.xml"));
try {
hdfs =FileSystem.get(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
(1) 遍历文件
public class FileSystemTest1 {
static FileSystem fs;
static {
try {
fs = FileSystem.get(new URI("hdfs://cluster-host1:9000"),new Configuration());
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
// 这里可以是conf 或者 是 uri
public static void main(String[] args) throws Exception {
System.out.println(fs);
listFiles("/");
}
public static void listFiles(String dirName)throws IOException {
Path f = new Path(dirName);
FileStatus[] status =fs.listStatus(f);
System.out.println(dirName +" has all files:");
for (int i = 0; i<status.length; i++) {
System.out.println(status[i].getPath().toString());
System.out.println("是否目录:"+status[i].isDirectory());
System.out.println("是否文件:"+status[i].isFile());
System.out.println("permission:"+status[i].getPermission());
System.out.println("owner:"+status[i].getOwner());
System.out.println("-------");
}
}
}
结果:
DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_-1915436331_1, ugi=didi (auth:SIMPLE)]]
/ has all files:
hdfs://cluster-host1:9000/test
是否目录:true
是否文件:false
permission:rwxr-xr-x
owner:hadoop
-------
hdfs://cluster-host1:9000/user
是否目录:true
是否文件:false
permission:rwxr-xr-x
owner:hadoop
-------
(2) 创建目录 mkdir
创建目录可能会遇到权限问题。不传将默认以客户端权限。
@Test
public void mkdir() throws IOException {
String path = "/test/fs";
fs.mkdirs(new Path(path));//绝对路径,相对路径会在每个用户下
}
结果:
org.apache.hadoop.security.AccessControlException: Permission denied: user=didi, access=WRITE, inode="/test":hadoop:supergroup:drwxr-xr-x
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:308)
需要权限。
我们需要将传入用户。在实例化fs
的时候,就要传用户。即可成功。
fs = FileSystem.get(new URI("hdfs://cluster-host1:9000"),
new Configuration(),"hadoop");
(3) 创建一个文件并写入内容 create
把本地的文件的内容写到流中,写入hdfs的创建的文件中
@Test
public void createFile() throws IOException {
// 可以设置块大小,如下边的就是1M,默认128M
FSDataOutputStream out = fs.create(new Path("/test/fs/createFile.txt"),
true, 1024000,
(short) 2, 1048576);
FileInputStream in = new FileInputStream("/Users/didi/Desktop/hello.txt");
//使用自带的工具将流写入
IOUtils.copyBytes(in,out,1024,true);
}
可以看到写入成功,并且块1M:
[hadoop@cluster-host1 ~]$ hadoop fs -text /test/fs/createFile.txt
hello didi,this is a test!
(3)上传本地文件到hdfs
//copy from local file to HDFS file
@Test
public void copyFile() throws IOException{
String localSrc = "/Users/didi/Desktop/hello.txt";
String hdfsDst = "/test/fs/";
Path src = new Path(localSrc);
Path dst = new Path(hdfsDst);
fs.copyFromLocalFile(src,dst);
//list all the files in thecurrent direction
FileStatus files[] =fs.listStatus(dst);
System.out.println("Upload to " + hdfsDst);
for (FileStatus file : files){
System.out.println(file.getPath());
}
}
结果:
Upload to /test/fs/
hdfs://cluster-host1:9000/test/fs/createFile.txt
hdfs://cluster-host1:9000/test/fs/hello.txt
未完待续:参考 https://blog.csdn.net/wdr2003/article/details/79860809
https://blog.csdn.net/qq_26323323/article/details/82938425