在操作之前先要做好准备工作。
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
操作名称 | 操作位置 | 代码 |
---|---|---|
1、新建文件夹 | 命令行 | hadoop fs -mkdir /newDir |
Eclipse | fileSystem.mkdirs(new Path(“/newDir”)); | |
2、删除文件/夹 | 命令行 | hadoop fs -rm/r /newDir |
Eclipse | fileSystem.delete(new Path(“/newDir”)); | |
3、存在文件/夹 | 命令行 | hadoop fs -test -e /user >>echo$? 0存在1不存在 |
Eclipse | fileSystem.exists(new Path(“/file”)); |
Demo1
public class myHdfs {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
if(fileSystem.exists(new Path("/newDir"))){
System.out.println("already existing!I'll delete it");
fileSystem.delete(new Path("/newDir"));
}else
{
System.out.println("No existing!I'll create and delete it");
fileSystem.mkdirs(new Path("/newDir"));
fileSystem.delete(new Path("/newDir"));
}
fileSystem.close();
}
}
操作名称 | 操作位置 | 代码 |
---|---|---|
4、读取文件属性 | 命令行 | hadoop fs -ls / |
Eclipse | fileSystem.getFileStatus(new Path(“/”)); |
Demo2
public class myHdfsLs {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
FileStatus[] fStatus = fileSystem.listStatus(new Path("/"));
for(FileStatus fileStatus:fStatus){
System.out.println("文件路径:"+fileStatus.getPath());
System.out.println("块的大小:"+fileStatus.getBlockSize());
System.out.println("文件所有者:"+fileStatus.getOwner()+":"+fileStatus.getGroup());
System.out.println("文件权限:"+fileStatus.getPermission());
System.out.println("文件长度:"+fileStatus.getLen());
System.out.println("备份数:"+fileStatus.getReplication());
System.out.println("修改时间:"+fileStatus.getModificationTime());
}
}
}
操作名称 | 操作位置 | 代码 |
---|---|---|
5、读取内容 | 命令行 | hadoop fs -cat /hello |
Eclipse | 采用输入输出流进行System.out进行显示 | |
Eclipse | fin.seek(num);//从第num个字节开始读取 | |
6、获取属性 | Eclipse | FileStatus status=FileSystem.getFileStatus(new Path(url)); |
Demo3
public class myHdfsCat {
public static void main(String[] args) {
Configuration configuration = new Configuration();
FileSystem fileSystem;
String url = "/hello";
try {
fileSystem = FileSystem.get(configuration);
//读取文件内容,已知文件内容长度
if(fileSystem.exists(new Path(url))) {
FSDataInputStream fin = fileSystem.open(new Path(url));
//fin.seek(2);//从第二个字节开始读取,汉字要注意在UTF-83个字节
FileStatus status = fileSystem.getFileStatus(new Path(url));
int len = Integer.parseInt(String.valueOf(status.getLen()));
System.out.println("文件长度:"+len);
byte[] b = new byte[len];
fin.readFully(0,b);
System.out.println(new String(b));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
7、创建文件/写入
命令行:由于hdfs是一次写入多次读取的文件管理系统,所以不支持在线对文件进行写,可以在本地写好后上传.
创建文件命令:hadoop fs -touchz /file
上传文件命令:hadoop fs -put /datafile
Eclipse:
FSDataOutputStream fout = fs.create(new Path("/newhello"));
fout.write("hello world".getBytes());
操作名称 | 操作位置 | 代码 |
---|---|---|
8、复制文件 | 命令行 | hadoop fs -cp /hello /h1 |
Eclipse | 如下 |
注:剪切可以先复制再删除源文件。
Demo4
public class myHdfsCopy {
public static void main(String[] args) throws IOException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(configuration);
//复制文件,需要使用
FSDataInputStream fin = fileSystem.open(new Path("/hello"));
FSDataOutputStream fout = fileSystem.create(new Path("/hellott"));
IOUtils.copy(fin, fout);
}
}
操作名称 | 操作位置 | 代码 |
---|---|---|
9、上传 | 命令行 | hadoop fs -put /hellott |
Eclipse | 如下 | |
10、下载 | 命令行 | hadoop fs -get /h1 |
Eclipse | 如下 |
Demo5
public class myHdfsUpdateLoad {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
//上传下载要注释其中一个,要保证先上传后下载
//上传
InputStream in = new FileInputStream("/root/myCode/testFile/hello");
FSDataOutputStream fout = fs.create(new Path("/hello1"));
IOUtils.copy(in, fout);
//下载
FSDataInputStream fin = fs.open(new Path("/hello1"));
OutputStream out = new FileOutputStream("/root/myCode/testFile/hello1");
IOUtils.copy(fin, out);
}
}
11、 hadoop路径读取文件
http://blog.csdn.net/timliangl/article/details/78200165