hdfs命令行客户端的常用操作命令
0、查看hdfs中的目录信息
hadoop fs -ls /hdfs路径
1、上传文件到hdfs中
hadoop fs -put /本地文件 /aaa
hadoop fs -copyFromLocal /本地文件 /hdfs路径 ## copyFromLocal等价于 put
hadoop fs -moveFromLocal /本地文件 /hdfs路径 ## 跟copyFromLocal的区别是:从本地移动到hdfs中
2、下载文件到客户端本地磁盘
hadoop fs -get /hdfs中的路径 /本地磁盘目录
hadoop fs -copyToLocal /hdfs中的路径 /本地磁盘路径 ## 跟get等价
hadoop fs -moveToLocal /hdfs路径 /本地路径 ## 从hdfs中移动到本地
3、在hdfs中创建文件夹
hadoop fs -mkdir -p /aaa/xxx
4、移动hdfs中的文件(更名)
hadoop fs -mv /hdfs的路径 /hdfs的另一个路径
5、删除hdfs中的文件或文件夹
hadoop fs -rm -r /aaa
6、修改文件的权限
hadoop fs -chown user:group /aaa
hadoop fs -chmod 700 /aaa
7、追加内容到已存在的文件
hadoop fs -appendToFile /本地文件 /hdfs中的文件
8、显示文本文件的内容
hadoop fs -cat /hdfs中的文件
hadoop fs -tail /hdfs中的文件
补充:hdfs命令行客户端的所有命令列表
Usage: hadoop fs [generic options]
[-appendToFile … ]
[-cat [-ignoreCrc] …]
[-checksum …]
[-chgrp [-R] GROUP PATH…]
[-chmod [-R] <MODE[,MODE]… | OCTALMODE> PATH…]
[-chown [-R] [OWNER][:[GROUP]] PATH…]
[-copyFromLocal [-f] [-p] [-l] [-d] … ]
[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] … ]
[-count [-q] [-h] [-v] [-t []] [-u] [-x]
[-cp [-f] [-p | -p[topax]] [-d] … ]
[-createSnapshot []]
[-deleteSnapshot ]
[-df [-h] [
[-du [-s] [-h] [-x]
[-expunge]
[-find
[-get [-f] [-p] [-ignoreCrc] [-crc] … ]
[-getfacl [-R]
[-getfattr [-R] {-n name | -d} [-e en]
[-getmerge [-nl] [-skip-empty-file] ]
[-help [cmd …]]
[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [
[-mkdir [-p]
[-moveFromLocal … ]
[-moveToLocal ]
[-mv … ]
[-put [-f] [-p] [-l] [-d] … ]
[-renameSnapshot ]
[-rm [-f] [-r|-R] [-skipTrash] [-safely] …]
[-rmdir [–ignore-fail-on-non-empty]
[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>}
[-setfattr {-n name [-v value] | -x name}
[-setrep [-R] [-w]
[-stat [format]
[-tail [-f] ]
[-test -[defsz]
[-text [-ignoreCrc] …]
[-touchz
[-truncate [-w]
[-usage [cmd …]]
Hadoop学习之路(十)HDFS API的使用
Configuration参数对象的机制: 构造时,会加载jar包中的默认配置 xx-default.xml 再加载
用户配置xx-site.xml ,覆盖掉默认参数 构造完成之后,还可以conf.set(“p”,“v”),会再次覆盖用户配置文件中的参数值
创建一个Configuration对象时,其构造方法会默认加载hadoop中的两个配置文件,分别是hdfs-site.xml以及core-site.xml,这两个文件中会有访问hdfs所需的参数值,主要是fs.default.name,指定了hdfs的地址,有了这个地址客户端就可以通过这个地址访问hdfs了。即可理解为configuration就是hadoop中的配置信息。通过根据这个类,可以根据这个类的参数设定,更改对hdfs的参数和设定。
//会从项目的classpath中加载core-default.xml hdfs-default.xml,根据这个类
Configuration conf = new Configuration();
// 指定本客户端上传文件到hdfs时需要保存的副本数为:2
conf.set("dfs.replication", "2");
// 指定本客户端上传文件到hdfs时切块的规格大小:64M
conf.set("dfs.blocksize", "64m");
// 构造一个访问指定HDFS系统的客户端对象:
fs = FileSystem.get(new URI("hdfs://hdp-01:9000/"), conf, "root");
// 参数1:——HDFS系统的URI,参数2:——客户端要特别指定的参数,参数3:客户端的身份(用户名)
//------------------------------------------常用api-----------------------------------------------
// 上传一个文件到HDFS中
fs.copyFromLocalFile(new Path("D:/install-pkgs/hbase-1.2.1-bin.tar.gz"), new Path("/aaa/"));
// 从HDFS中下载文件到客户端本地磁盘
fs.copyToLocalFile(new Path("/hdp20-05.txt"), new Path("f:/"));
//在hdfs内部移动文件\修改名称
fs.rename(new Path("/install.log"), new Path("/aaa/in.log"));
//在hdfs中创建文件夹
fs.mkdirs(new Path("/xx/yy/zz"));
//在hdfs中删除文件或文件夹
fs.delete(new Path("/aaa"), true);
//查询hdfs指定目录下的文件信息
RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/"), true);
// 只查询文件的信息,不返回文件夹的信息
while (iter.hasNext()) {
LocatedFileStatus status = iter.next();
System.out.println("文件全路径:" + status.getPath());
System.out.println("块大小:" + status.getBlockSize());
System.out.println("文件长度:" + status.getLen());
System.out.println("副本数量:" + status.getReplication());
System.out.println("块信息:" + Arrays.toString(status.getBlockLocations()));
System.out.println("--------------------------------");
}
fs.close();
//查询hdfs指定目录下的文件和文件夹信息
public void testLs2() throws Exception {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus status : listStatus) {
System.out.println("文件全路径:" + status.getPath());
System.out.println(status.isDirectory() ? "这是文件夹" : "这是文件");
System.out.println("块大小:" + status.getBlockSize());
System.out.println("文件长度:" + status.getLen());
System.out.println("副本数量:" + status.getReplication());
System.out.println("--------------------------------");
}
fs.close();
//读取hdfs中的文件的内容
FSDataInputStream in = fs.open(new Path("/test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
in.close();
fs.close();
//往hdfs中的文件写内容
FSDataOutputStream out = fs.create(new Path("/zz.jpg"), false);
// D:\images\006l0mbogy1fhehjb6ikoj30ku0ku76b.jpg
FileInputStream in = new FileInputStream("D:/images/006l0mbogy1fhehjb6ikoj30ku0ku76b.jpg");
byte[] buf = new byte[1024];
int read = 0;
while ((read = in.read(buf)) != -1) {
out.write(buf,0,read);
}
in.close();
out.close();
fs.close();