操作题目+命令实现
(1)向 HDFS 中上传任意文本文件,如果指定的文件在 HDFS 中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;
(2)从 HDFS 中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;
(3)将 HDFS 中指定文件的内容输出到终端中;
(4)显示HDFS 中指定的文件的读写权限、大小、创建时间、路径等信息;
(5)给定 HDFS 中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;
(6)提供一个 HDFS 内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;
(7)提供一个 HDFS 的目录的路径,对该目录进行创建和删除操作。创建目录时, 如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录;
(8)向 HDFS 中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;
若把a放到b的头部,则先将b放到a尾部,再将a复制给b
(9)删除HDFS 中指定的文件;
(10)删除HDFS 中指定的目录,由用户指定目录中如果存在文件时是否删除目录;
(11)在 HDFS 中,将文件从源路径移动到目的路径。
源代码
package cn.itcast.bigdata.hdfs;
import java.net.URI;//获取互联网资料
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.io.*;
import org.apache.hadoop.conf.Configuration;//配置文件
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;//文件系统
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;
public class hadoopAPI {
**//删除文件**
public static void rmdir(String filePath) throws IOException {
//读取配置文件
Configuration conf = new Configuration();
//获取文件系统
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path path = new Path(filePath);
//调用deleteOnExit()
boolean flag = fs.deleteOnExit(path);
// fs.delete(path);
if(flag) {
System.out.println("delete ok!");
}else {
System.out.println("delete failure");
}
//关闭文件系统
fs.close();
}
**//添加到文件末尾**
public static void appendFile(String src,String dst) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path dstPath = new Path(dst);
InputStream in = new BufferedInputStream(new FileInputStream(src));
FSDataOutputStream out = fs.append(dstPath);
IOUtils.copyBytes(in,out,4096,true);
//fs.close();
}
**//本地文件上传到hdfs**
public static void uploadFile(String src,String dst) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path srcPath = new Path(src);//原路径
Path dstPath = new Path(dst);//目标路径
//判断是否存在相同名称的文件
if(!fs.exists(dstPath))//不存在,直接复制
{
fs.copyFromLocalFile(false,srcPath, dstPath);//(false不删除本地文件;true删除本地文件,原路径,目标路径)
}
else//若存在,由用户选择上传方式
{
System.out.println("请选择 1覆盖原文件;2追加到文件末尾");
Scanner in=new Scanner(System.in);
int x=in.nextInt();
if(x==1)
{
fs.copyFromLocalFile(false,srcPath, dstPath);
}
else if(x==2)
appendFile(src,dst);
}
System.out.println("上传成功!");
}
**//文件重命名**
public static void rename(String oldname,String newname) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path oldPath = new Path(oldname);
Path newPath = new Path(newname);
boolean flag = fs.rename(oldPath,newPath);
if(flag){
System.out.println("rename ok!");
}else{
System.out.println("rename fail!");
}
}
**//文件下载**
public static void download(String oldPath,String newPath) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path ofile = new Path(oldPath);
File nfile=new File(newPath);
FSDataInputStream in = fs.open(ofile);
if(!nfile.exists())
{
FileOutputStream out = new FileOutputStream(nfile);
IOUtils.copyBytes(in,out,2048,true);
}
else{
Scanner sc=new Scanner(System.in);
System.out.println("文件存在,修改名称:");
String new2=sc.next();
File newfile = new File(new2);
FileOutputStream out = new FileOutputStream(newfile);
IOUtils.copyBytes(in,out,2048,true);
}
System.out.println("Dowmload success!");
}
**//文件内容输出到终端**
public static void readFile(String uri) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
InputStream in = null;
try{
in = fs.open(new Path(uri));
IOUtils.copyBytes(in,System.out,4096,false);
System.out.println("show succeed!");
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(in);
}
}
**//显示文件大小、权限、创建时间、权限等**
public static void answer4(String remote) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
FileStatus[] status = fs.listStatus(new Path(remote));
for(FileStatus s:status){
System.out.println("文件大小:"+s.getBlockSize());
System.out.println("文件权限:"+s.getPermission());
System.out.println("文件路径:"+s.getPath());
long time = s.getModificationTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String data = format.format(time);
System.out.println("文件创建时间:"+data);
}
}
**//显示所有文件大小、权限、创建时间、权限等**
public static void allfile(String remote) throws IOException{
Configuration conf = new Configuration();
FileSystem fs =FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path(remote),true);
while(iterator.hasNext())
{
FileStatus status = iterator.next();
System.out.println("文件大小为:"+status.getBlockSize());
System.out.println("文件路径为:"+status.getPath());
System.out.println("文件的权限为:"+status.getPermission());
long time =status.getModificationTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String data = format.format(time);
System.out.println("文件创建时间:"+data);
}
}
**//创建文件**
public static void mk(String upremote,String remote) throws IOException{
Configuration conf = new Configuration();
FileSystem fs =FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path a = new Path(upremote);
Path b = new Path(remote);
if(!fs.exists(a))
{
System.out.println("路径不存在,即将进行创建");
fs.mkdirs(a);
FSDataOutputStream out = fs.create(b);
out.close();
System.out.println("文件创建成功!");
}else{
System.out.println("路径存在,即将创建目标文件");
FSDataOutputStream out = fs.create(b);
out.close();
System.out.println("文件创建成功!");
}
}
**//删除文件,由用户指定删除目录不为空时,是否还进行删除**
public static void rm(String remote) throws IOException{
Configuration conf = new Configuration();
FileSystem fs =FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path a = new Path(remote);
FileStatus[] status = fs.listStatus(a);
if(status.length>0)
{
System.out.println("文件目录不为空,请选择 1继续删除 2不删除");
Scanner in = new Scanner(System.in);
int x=in.nextInt();
if(x==1)
{
fs.delete(a,true);
System.out.println("目录已经删除");
}
else if(x==2)
{
System.out.println("未做任何操作!");
}
}
}
**//向指定文件指定位置追加指定内容**
public static void addword(String prefile,String addfile) throws IOException{
Configuration conf = new Configuration();
FileSystem fs =FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path a = new Path(prefile);
Path b = new Path(addfile);
System.out.println("选择添加位置,请选择 1末尾 2头部");
Scanner in = new Scanner(System.in);
int x = in.nextInt();
if(x==1)
{
appendFile(addfile,prefile);
System.out.println("ok!");
}
else if(x==2)
{
String temp="/usr/local/hadoop/guodu1";
download(prefile,temp);//先下载到本地
fs.copyFromLocalFile(true,b,a);
appendFile(temp,prefile);
System.out.println("ok!");
}
}
**//在hdfs中进行文件的移动**
public static void removeinhdfs(String prepath,String newpath) throws IOException{
Configuration conf = new Configuration();
FileSystem fs =FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path a = new Path(prepath);
Path b = new Path(newpath);
if(fs.rename(a,b))
{
System.out.println("移动成功");
}
}
**//主函数**
public static void main(String[] args) throws IOException{
//rmdir("lwx/l.text");
//uploadFile("/usr/local/hadoop/lwx1.txt","lwx/lwx1.txt");
//download("lwx/lwx.text","/usr/local/hadoop/lwx.text");
//readFile("lwx/lwx.text");
//answer4("lwx/lwx.text");
//allfile("lwx");
//mk("lwx/lwx1","lwx/lwx1/l.text");
//rm("lwx/lwx1");
//addword("lwx/lwx.text","/usr/local/hadoop/lwx2.text");
removeinhdfs("lwx/lwx.text","input");
}
}