From 3e362451d226d9e19b4b652a02926dedc02f6cf9 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 13 Nov 2020 12:10:55 -0800 Subject: add example of watch files --- .../src/main/java/com/example/test/Example.java | 56 ---------------------- .../java/com/seaweedfs/examples/UnzipFile.java | 54 +++++++++++++++++++++ .../java/com/seaweedfs/examples/WatchFiles.java | 42 ++++++++++++++++ 3 files changed, 96 insertions(+), 56 deletions(-) delete mode 100644 other/java/examples/src/main/java/com/example/test/Example.java create mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/UnzipFile.java create mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/WatchFiles.java (limited to 'other/java/examples/src') diff --git a/other/java/examples/src/main/java/com/example/test/Example.java b/other/java/examples/src/main/java/com/example/test/Example.java deleted file mode 100644 index 3d22329a8..000000000 --- a/other/java/examples/src/main/java/com/example/test/Example.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.example.test; - -import seaweed.hdfs.SeaweedInputStream; -import seaweedfs.client.FilerClient; -import seaweedfs.client.FilerGrpcClient; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -public class Example { - - public static FilerClient filerClient = new FilerClient("localhost", 18888); - public static FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); - - public static void main(String[] args) throws IOException { - - long startTime = System.currentTimeMillis(); - // 本地模式,速度很快 - parseZip("/Users/chris/tmp/test.zip"); - - long startTime2 = System.currentTimeMillis(); - - long localProcessTime = startTime2 - startTime; - - // swfs读取,慢 - SeaweedInputStream seaweedInputStream = new SeaweedInputStream( - filerGrpcClient, - new org.apache.hadoop.fs.FileSystem.Statistics(""), - "/", - filerClient.lookupEntry("/", "test.zip") - ); - parseZip(seaweedInputStream); - - long swProcessTime = System.currentTimeMillis() - startTime2; - - System.out.println("Local time: " + localProcessTime); - System.out.println("SeaweedFS time: " + swProcessTime); - - } - - public static void parseZip(String filename) throws IOException { - FileInputStream fileInputStream = new FileInputStream(filename); - parseZip(fileInputStream); - } - - public static void parseZip(InputStream is) throws IOException { - ZipInputStream zin = new ZipInputStream(is); - ZipEntry ze; - while ((ze = zin.getNextEntry()) != null) { - System.out.println(ze.getName()); - } - } -} diff --git a/other/java/examples/src/main/java/com/seaweedfs/examples/UnzipFile.java b/other/java/examples/src/main/java/com/seaweedfs/examples/UnzipFile.java new file mode 100644 index 000000000..0529a5c73 --- /dev/null +++ b/other/java/examples/src/main/java/com/seaweedfs/examples/UnzipFile.java @@ -0,0 +1,54 @@ +package com.seaweedfs.examples; + +import seaweed.hdfs.SeaweedInputStream; +import seaweedfs.client.FilerClient; +import seaweedfs.client.FilerGrpcClient; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class UnzipFile { + + public static void main(String[] args) throws IOException { + + FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); + FilerClient filerClient = new FilerClient(filerGrpcClient); + + long startTime = System.currentTimeMillis(); + parseZip("/Users/chris/tmp/test.zip"); + + long startTime2 = System.currentTimeMillis(); + + long localProcessTime = startTime2 - startTime; + + SeaweedInputStream seaweedInputStream = new SeaweedInputStream( + filerGrpcClient, + new org.apache.hadoop.fs.FileSystem.Statistics(""), + "/", + filerClient.lookupEntry("/", "test.zip") + ); + parseZip(seaweedInputStream); + + long swProcessTime = System.currentTimeMillis() - startTime2; + + System.out.println("Local time: " + localProcessTime); + System.out.println("SeaweedFS time: " + swProcessTime); + + } + + public static void parseZip(String filename) throws IOException { + FileInputStream fileInputStream = new FileInputStream(filename); + parseZip(fileInputStream); + } + + public static void parseZip(InputStream is) throws IOException { + ZipInputStream zin = new ZipInputStream(is); + ZipEntry ze; + while ((ze = zin.getNextEntry()) != null) { + System.out.println(ze.getName()); + } + } +} diff --git a/other/java/examples/src/main/java/com/seaweedfs/examples/WatchFiles.java b/other/java/examples/src/main/java/com/seaweedfs/examples/WatchFiles.java new file mode 100644 index 000000000..c4f4c81b0 --- /dev/null +++ b/other/java/examples/src/main/java/com/seaweedfs/examples/WatchFiles.java @@ -0,0 +1,42 @@ +package com.seaweedfs.examples; + +import seaweedfs.client.FilerClient; +import seaweedfs.client.FilerGrpcClient; +import seaweedfs.client.FilerProto; + +import java.io.IOException; +import java.util.Iterator; + +public class WatchFiles { + + public static void main(String[] args) throws IOException { + FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); + FilerClient filerClient = new FilerClient(filerGrpcClient); + + Iterator watch = filerClient.watch( + "/buckets", + "exampleClient", + System.currentTimeMillis() * 1000000L + ); + + while (watch.hasNext()) { + FilerProto.SubscribeMetadataResponse event = watch.next(); + FilerProto.EventNotification notification = event.getEventNotification(); + if (notification.getNewParentPath() != null) { + // move an entry to a new directory, possibly with a new name + if (notification.hasOldEntry() && notification.hasNewEntry()) { + System.out.println("move " + event.getDirectory() + "/" + notification.getOldEntry().getName() + " to " + notification.getNewParentPath() + "/" + notification.getNewEntry().getName()); + } else { + System.out.println("this should not happen."); + } + } else if (notification.hasNewEntry() && !notification.hasOldEntry()) { + System.out.println("create entry " + event.getDirectory() + "/" + notification.getNewEntry().getName()); + } else if (!notification.hasNewEntry() && notification.hasOldEntry()) { + System.out.println("delete entry " + event.getDirectory() + "/" + notification.getOldEntry().getName()); + } else if (notification.hasNewEntry() && notification.hasOldEntry()) { + System.out.println("updated entry " + event.getDirectory() + "/" + notification.getNewEntry().getName()); + } + } + + } +} -- cgit v1.2.3