commit aa9934079f6bd20d0d6a05f39ae026d7f41bbae7 Author: fly6516 Date: Wed Feb 19 01:38:18 2025 +0000 update code diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..895c164 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..754d8fa --- /dev/null +++ b/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + org.example + hdfs_pro + 1.0-SNAPSHOT + + + 8 + 8 + + + \ No newline at end of file diff --git a/src/main/java/CreateDir.java b/src/main/java/CreateDir.java new file mode 100644 index 0000000..6d176b8 --- /dev/null +++ b/src/main/java/CreateDir.java @@ -0,0 +1,21 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import java.util.Scanner; +import java.net.URI; + +public class CreateDir { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String dirPath = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + Path hdfsPath = new Path(dirPath); + if(fs.mkdirs(hdfsPath)){ + System.out.println("Directory "+ dirPath +" has been created successfully!"); + } + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/CreateFile.java b/src/main/java/CreateFile.java new file mode 100644 index 0000000..a4760ac --- /dev/null +++ b/src/main/java/CreateFile.java @@ -0,0 +1,20 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; + +import java.net.URI; +import java.util.Scanner; + +public class CreateFile { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String filePath = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + Path hdfsPath = new Path(filePath); + fs.create(hdfsPath); + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/DeleteDir.java b/src/main/java/DeleteDir.java new file mode 100644 index 0000000..88501d0 --- /dev/null +++ b/src/main/java/DeleteDir.java @@ -0,0 +1,22 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; + +import java.net.URI; +import java.util.Scanner; + +public class DeleteDir { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String dirPath = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + Path hdfsPath = new Path(dirPath); + if(fs.delete(hdfsPath,true)){ + System.out.println("Directory "+ dirPath +" has been deleted successfully!"); + } + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/DeleteFile.java b/src/main/java/DeleteFile.java new file mode 100644 index 0000000..16c12fe --- /dev/null +++ b/src/main/java/DeleteFile.java @@ -0,0 +1,23 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; + +import java.net.URI; +import java.util.Scanner; + +public class DeleteFile { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String filePath = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + + Path hdfsPath = new Path(filePath); + if(fs.delete(hdfsPath,false)){ + System.out.println("File "+ filePath +" has been deleted successfully!"); + } + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/DirExist.java b/src/main/java/DirExist.java new file mode 100644 index 0000000..657d330 --- /dev/null +++ b/src/main/java/DirExist.java @@ -0,0 +1,22 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import java.util.Scanner; +import java.net.URI; + +public class DirExist { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String dirName = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + if(fs.exists(new Path(dirName ))) { + System.out.println("Directory Exists!"); + } else { + System.out.println("Directory not Exists!"); + } + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/FileExist.java b/src/main/java/FileExist.java new file mode 100644 index 0000000..58f22dc --- /dev/null +++ b/src/main/java/FileExist.java @@ -0,0 +1,24 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; + +import java.net.URI; +import java.util.Scanner; + +public class FileExist { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String fileName = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + + if(fs.exists(new Path(fileName))) { + System.out.println("File Exists!"); + } else { + System.out.println("File not Exists!"); + } + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/ListFiles.java b/src/main/java/ListFiles.java new file mode 100644 index 0000000..f122ab4 --- /dev/null +++ b/src/main/java/ListFiles.java @@ -0,0 +1,25 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FileUtil; +import org.apache.hadoop.fs.Path; + +import java.net.URI; +import java.util.Scanner; + +public class ListFiles { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String filePath = sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + Path srcPath = new Path(filePath); + FileStatus[] stats = fs.listStatus(srcPath); + Path[] paths = FileUtil.stat2Paths(stats); + for(Path p : paths) + System.out.println(p.getName()); + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/ReadFile.java b/src/main/java/ReadFile.java new file mode 100644 index 0000000..49ca38a --- /dev/null +++ b/src/main/java/ReadFile.java @@ -0,0 +1,32 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; + +import java.net.URI; +import java.util.Scanner; + +public class ReadFile { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String filePath = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + + Path srcPath = new Path(filePath); + + FSDataInputStream is = fs.open(srcPath); + while(true) { + String line = is.readLine(); + if(line == null) { + break; + } + System.out.println(line); + } + is.close(); + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/Rename.java b/src/main/java/Rename.java new file mode 100644 index 0000000..81397e1 --- /dev/null +++ b/src/main/java/Rename.java @@ -0,0 +1,24 @@ +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; + +import java.net.URI; +import java.util.Scanner; + +public class Rename { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String srcStrPath = '/'+sc.next(); + String dstStrPath = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + Path srcPath = new Path(srcStrPath); + Path dstPath = new Path(dstStrPath); + if(fs.rename(srcPath,dstPath)) { + System.out.println("rename from " + srcStrPath + " to " + dstStrPath + "successfully!"); + } + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/WriteFile.java b/src/main/java/WriteFile.java new file mode 100644 index 0000000..a7292de --- /dev/null +++ b/src/main/java/WriteFile.java @@ -0,0 +1,32 @@ +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 java.net.URI; +import java.util.Scanner; + +public class WriteFile { + public static void main(String[] args) { + try { + Scanner sc = new Scanner(System.in); + String filePath = '/'+sc.next(); + FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration()); + + Path srcPath = new Path(filePath); + FSDataOutputStream os = fs.create(srcPath,true,1024,(short)1,(long)(1<<26)); + String str = "Hello, this is a sentence that should be written into the file.\n"; + os.write(str.getBytes()); + os.flush(); + os.close(); + + os = fs.append(srcPath); + str = "Hello, this is another sentence that should be written into the file.\n"; + os.write(str.getBytes()); + os.flush(); + os.close(); + }catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/target/classes/CreateDir.class b/target/classes/CreateDir.class new file mode 100644 index 0000000..55699b8 Binary files /dev/null and b/target/classes/CreateDir.class differ diff --git a/target/classes/CreateFile.class b/target/classes/CreateFile.class new file mode 100644 index 0000000..4790d23 Binary files /dev/null and b/target/classes/CreateFile.class differ diff --git a/target/classes/DeleteDir.class b/target/classes/DeleteDir.class new file mode 100644 index 0000000..e470cf1 Binary files /dev/null and b/target/classes/DeleteDir.class differ diff --git a/target/classes/DeleteFile.class b/target/classes/DeleteFile.class new file mode 100644 index 0000000..93cbc7e Binary files /dev/null and b/target/classes/DeleteFile.class differ diff --git a/target/classes/DirExist.class b/target/classes/DirExist.class new file mode 100644 index 0000000..2003f91 Binary files /dev/null and b/target/classes/DirExist.class differ diff --git a/target/classes/FileExist.class b/target/classes/FileExist.class new file mode 100644 index 0000000..3325c69 Binary files /dev/null and b/target/classes/FileExist.class differ diff --git a/target/classes/ListFiles.class b/target/classes/ListFiles.class new file mode 100644 index 0000000..cc4b227 Binary files /dev/null and b/target/classes/ListFiles.class differ diff --git a/target/classes/ReadFile.class b/target/classes/ReadFile.class new file mode 100644 index 0000000..2683c6f Binary files /dev/null and b/target/classes/ReadFile.class differ diff --git a/target/classes/Rename.class b/target/classes/Rename.class new file mode 100644 index 0000000..ac55b1f Binary files /dev/null and b/target/classes/Rename.class differ diff --git a/target/classes/WriteFile.class b/target/classes/WriteFile.class new file mode 100644 index 0000000..2872291 Binary files /dev/null and b/target/classes/WriteFile.class differ