命令行管理HDFS
[root@server1 bin]# hadoop fs Usage: hadoop fs [generic options] [-appendToFile... ] [-cat [-ignoreCrc] ...] [-checksum ...] [-chgrp [-R] GROUP PATH...] [-chmod [-R] PATH...] [-chown [-R] [OWNER][:[GROUP]] PATH...] [-copyFromLocal [-f] [-p] [-l] ... ] [-copyToLocal [-p] [-ignoreCrc] [-crc] ... ] [-count [-q] [-h] ...] [-cp [-f] [-p | -p[topax]] ... ] [-createSnapshot [ ]] [-deleteSnapshot ] [-df [-h] [ ...]] [-du [-s] [-h] ...] [-expunge] [-get [-p] [-ignoreCrc] [-crc] ... ] [-getfacl [-R] ] [-getfattr [-R] {-n name | -d} [-e en] ] [-getmerge [-nl] ] [-help [cmd ...]] [-ls [-d] [-h] [-R] [ ...]] [-mkdir [-p] ...] [-moveFromLocal ... ] [-moveToLocal ] [-mv ... ] [-put [-f] [-p] [-l] ... ] [-renameSnapshot ] [-rm [-f] [-r|-R] [-skipTrash] ...] [-rmdir [--ignore-fail-on-non-empty] ...] [-setfacl [-R] [{-b|-k} {-m|-x } ]|[--set ]] [-setfattr {-n name [-v value] | -x name} ] [-setrep [-R] [-w] ...] [-stat [format] ...] [-tail [-f] ] [-test -[defsz] ] [-text [-ignoreCrc] ...] [-touchz ...] [-usage [cmd ...]]
JAVA操作HDFS
maven依赖:
jdk.tools jdk.tools 1.6 system ${JAVA_HOME}/lib/tools.jar org.apache.hadoop hadoop-hdfs 2.6.5 org.apache.hadoop hadoop-common 2.6.5
上传&下载示例:
public static void upload() throws Exception { Configuration conf = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.110:9000"),conf,"root"); fileSystem.copyFromLocalFile(new Path("D:\\test\\2.txt"), new Path("/2.txt")); fileSystem.close(); } public static void download() throws Exception { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://192.168.1.110:9000"); FileSystem fileSystem = FileSystem.get(conf); fileSystem.copyToLocalFile(false,new Path("/2.txt"),new Path("D:\\test\\2.download.txt"),true); fileSystem.close(); }
参考:
HDFS文件管理:
API文档: