********************************************************************************************
上传文件 vi CopyFile.java
********************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CopyFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path src =new Path("/home/hadoop/bbb/b1");
Path dst =new Path("hdfs://h101:9000/user/hadoop");
hdfs.copyFromLocalFile(src, dst);
System.out.println("Upload to"+conf.get("fs.default.name"));
System.out.println("Upload to"+conf.get("fs.default.name"));
FileStatus files[]=hdfs.listStatus(dst);
for(FileStatus file:files){
System.out.println(file.getPath());
}
}
}
System.out.println(file.getPath());
}
}
}
***************************************************************************************************************************
HDFS创建文件 vi CreateFile.java
**************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
public class CreateFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
FileSystem hdfs=FileSystem.get(conf);
byte[] buff="hello hadoop world!\n".getBytes();
Path dst =new Path("hdfs://h151:9000/user/hadoop");
Path dfs=new Path("hdfs://h151:9000/user/hadoop/hellow.txt");
Path dst =new Path("hdfs://h151:9000/user/hadoop");
Path dfs=new Path("hdfs://h151:9000/user/hadoop/hellow.txt");
FSDataOutputStream outputStream=hdfs.create(dfs);
outputStream.write(buff,0,buff.length);
FileStatus files[]=hdfs.listStatus(dst);
outputStream.write(buff,0,buff.length);
FileStatus files[]=hdfs.listStatus(dst);
for(FileStatus file:files){
System.out.println(file.getPath());
System.out.println(file.getPath());
}
}
}
}
}
**************************************************************************************************************************
创建目录 vi CreateDir.java
**************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CreateDir {
public static void main(String[] args) throws Exception{
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
FileSystem hdfs=FileSystem.get(conf);
Path dfs=new Path("hdfs://h101:9000/user/hadoop/TestDir");
hdfs.mkdirs(dfs);
}
}
}
}
***************************************************************************************************************************
重命名文件 vi Rename.java
***************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Rename{
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
FileSystem hdfs=FileSystem.get(conf);
Path frpaht=new Path("hdfs://h101:9000/user/hadoop/b1"); //旧的文件名
Path topath=new Path("hdfs://h101:9000/user/hadoop/bb111"); //新的文件名
Path topath=new Path("hdfs://h101:9000/user/hadoop/bb111"); //新的文件名
boolean isRename=hdfs.rename(frpaht, topath);
String result=isRename?"成功":"失败";
System.out.println("文件重命名结果为:"+result);
}
}
**********************************************************************************************************************************
删除hdfs 上的文件 vi delete.java
}
**********************************************************************************************************************************
删除hdfs 上的文件 vi delete.java
************************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class DeleteFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
FileSystem hdfs=FileSystem.get(conf);
Path delef=new Path("hdfs://h101:9000/user/hadoop/bb111");
boolean isDeleted=hdfs.delete(delef,false);
boolean isDeleted=hdfs.delete(delef,false);
//递归删除
//boolean isDeleted=hdfs.delete(delef,true);
//boolean isDeleted=hdfs.delete(delef,true);
System.out.println("Delete?"+isDeleted);
}
}
}
}
***********************************************************************************************************************************************
查看文件是否存在 vi CheckFile.java
**********************************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CheckFile {
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path findf=new Path("hdfs://h101:9000/user/hadoop/hellow.txt");
boolean isExists=hdfs.exists(findf);
String result=isRename?"文件存在":"文件不存在";
String result=isRename?"文件存在":"文件不存在";
System.out.println("检查结果为:"+result);
}
}
}
****************************************************************************************************************************************************
查看HDFS文件最后修改时间 vi GetLTime.java
****************************************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class GetLTime {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
FileSystem hdfs=FileSystem.get(conf);
Path fpath =new Path("hdfs://h101:9000/user/hadoop/hellow.txt");
FileStatus fileStatus=hdfs.getFileStatus(fpath);
long modiTime=fileStatus.getModificationTime();
System.out.println("file1.txt的修改时间是"+modiTime);
}
}
}
}
*************************************************************************************************************************************************************
查看 hdfs文件 vi URLcat.java
****************************************************************************************************************************************************************
import java.io.InputStream;
import java.net.URL;
import java.net.URL;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IOUtils;
public class URLcat{
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static void main(String[] args) throws Exception {
InputStream in = null;
try {
in = new URL(args[0]).openStream();
IOUtils.copyBytes(in, System.out, 4086, false);
} finally {
IOUtils.closeStream(in);
}
}
}
InputStream in = null;
try {
in = new URL(args[0]).openStream();
IOUtils.copyBytes(in, System.out, 4086, false);
} finally {
IOUtils.closeStream(in);
}
}
}
************************************************************************************************************************************
判断文件是否存在 vi nimei.java
************************************************************************************************
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataOutputStream;
public class nimei{
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path find=new Path("hdfs://h151:9000/user/hadoop/hellow.txt");
boolean isExists=hdfs.exists(find);
if(isExists==false){
FSDataOutputStream outputStream=hdfs.create(find);
FileStatus files[]=hdfs.listStatus(find);
for(FileStatus file:files){
System.out.println(file.getPath());
}
}
else{
System.out.println("您要创建的文件已经存在");
}
}
}
boolean isExists=hdfs.exists(find);
if(isExists==false){
FSDataOutputStream outputStream=hdfs.create(find);
FileStatus files[]=hdfs.listStatus(find);
for(FileStatus file:files){
System.out.println(file.getPath());
}
}
else{
System.out.println("您要创建的文件已经存在");
}
}
}
**********************************************************************************************************