From 42e5ef4b0150d339befedb06f6ed23a6c9890296 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 4 Feb 2021 21:02:54 -0800 Subject: Java: adjust examples --- .../com/seaweedfs/examples/ExampleReadFile.java | 48 ++++++++++++++++++++++ .../examples/ExampleWatchFileChanges.java | 46 +++++++++++++++++++++ .../com/seaweedfs/examples/ExampleWriteFile.java | 48 ++++++++++++++++++++++ .../java/com/seaweedfs/examples/UnzipFile.java | 48 ---------------------- .../java/com/seaweedfs/examples/WatchFiles.java | 46 --------------------- .../java/com/seaweedfs/examples/WriteFile.java | 48 ---------------------- 6 files changed, 142 insertions(+), 142 deletions(-) create mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/ExampleReadFile.java create mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWatchFileChanges.java create mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWriteFile.java delete mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/UnzipFile.java delete mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/WatchFiles.java delete mode 100644 other/java/examples/src/main/java/com/seaweedfs/examples/WriteFile.java (limited to 'other/java/examples/src/main') diff --git a/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleReadFile.java b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleReadFile.java new file mode 100644 index 000000000..bd73df802 --- /dev/null +++ b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleReadFile.java @@ -0,0 +1,48 @@ +package com.seaweedfs.examples; + +import seaweedfs.client.FilerGrpcClient; +import seaweedfs.client.SeaweedInputStream; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class ExampleReadFile { + + public static void main(String[] args) throws IOException { + + FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); + + long startTime = System.currentTimeMillis(); + parseZip("/Users/chris/tmp/test.zip"); + + long startTime2 = System.currentTimeMillis(); + + long localProcessTime = startTime2 - startTime; + + SeaweedInputStream seaweedInputStream = new SeaweedInputStream( + filerGrpcClient, "/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/ExampleWatchFileChanges.java b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWatchFileChanges.java new file mode 100644 index 000000000..72c572d31 --- /dev/null +++ b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWatchFileChanges.java @@ -0,0 +1,46 @@ +package com.seaweedfs.examples; + +import seaweedfs.client.FilerClient; +import seaweedfs.client.FilerProto; + +import java.io.IOException; +import java.util.Date; +import java.util.Iterator; + +public class ExampleWatchFileChanges { + + public static void main(String[] args) throws IOException { + + FilerClient filerClient = new FilerClient("localhost", 18888); + + long sinceNs = (System.currentTimeMillis() - 3600 * 1000) * 1000000L; + + Iterator watch = filerClient.watch( + "/buckets", + "exampleClientName", + sinceNs + ); + + System.out.println("Connected to filer, subscribing from " + new Date()); + + while (watch.hasNext()) { + FilerProto.SubscribeMetadataResponse event = watch.next(); + FilerProto.EventNotification notification = event.getEventNotification(); + if (!event.getDirectory().equals(notification.getNewParentPath())) { + // move an entry to a new directory, possibly with a new name + if (notification.hasOldEntry() && notification.hasNewEntry()) { + System.out.println("moved " + 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("created entry " + event.getDirectory() + "/" + notification.getNewEntry().getName()); + } else if (!notification.hasNewEntry() && notification.hasOldEntry()) { + System.out.println("deleted entry " + event.getDirectory() + "/" + notification.getOldEntry().getName()); + } else if (notification.hasNewEntry() && notification.hasOldEntry()) { + System.out.println("updated entry " + event.getDirectory() + "/" + notification.getNewEntry().getName()); + } + } + + } +} diff --git a/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWriteFile.java b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWriteFile.java new file mode 100644 index 000000000..228a3c0b7 --- /dev/null +++ b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWriteFile.java @@ -0,0 +1,48 @@ +package com.seaweedfs.examples; + +import seaweedfs.client.FilerGrpcClient; +import seaweedfs.client.SeaweedInputStream; +import seaweedfs.client.SeaweedOutputStream; + +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class ExampleWriteFile { + + public static void main(String[] args) throws IOException { + + FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); + + SeaweedInputStream seaweedInputStream = new SeaweedInputStream( + filerGrpcClient, "/test.zip"); + unZipFiles(filerGrpcClient, seaweedInputStream); + + } + + public static void unZipFiles(FilerGrpcClient filerGrpcClient, InputStream is) throws IOException { + ZipInputStream zin = new ZipInputStream(is); + ZipEntry ze; + while ((ze = zin.getNextEntry()) != null) { + + String filename = ze.getName(); + if (filename.indexOf("/") >= 0) { + filename = filename.substring(filename.lastIndexOf("/") + 1); + } + if (filename.length()==0) { + continue; + } + + SeaweedOutputStream seaweedOutputStream = new SeaweedOutputStream(filerGrpcClient, "/test/"+filename); + byte[] bytesIn = new byte[16 * 1024]; + int read = 0; + while ((read = zin.read(bytesIn))!=-1) { + seaweedOutputStream.write(bytesIn,0,read); + } + seaweedOutputStream.close(); + + 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 deleted file mode 100644 index 12eab1a2c..000000000 --- a/other/java/examples/src/main/java/com/seaweedfs/examples/UnzipFile.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.seaweedfs.examples; - -import seaweedfs.client.FilerGrpcClient; -import seaweedfs.client.SeaweedInputStream; - -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); - - long startTime = System.currentTimeMillis(); - parseZip("/Users/chris/tmp/test.zip"); - - long startTime2 = System.currentTimeMillis(); - - long localProcessTime = startTime2 - startTime; - - SeaweedInputStream seaweedInputStream = new SeaweedInputStream( - filerGrpcClient, "/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 deleted file mode 100644 index e489cb3b1..000000000 --- a/other/java/examples/src/main/java/com/seaweedfs/examples/WatchFiles.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.seaweedfs.examples; - -import seaweedfs.client.FilerClient; -import seaweedfs.client.FilerProto; - -import java.io.IOException; -import java.util.Date; -import java.util.Iterator; - -public class WatchFiles { - - public static void main(String[] args) throws IOException { - - FilerClient filerClient = new FilerClient("localhost", 18888); - - long sinceNs = (System.currentTimeMillis() - 3600 * 1000) * 1000000L; - - Iterator watch = filerClient.watch( - "/buckets", - "exampleClientName", - sinceNs - ); - - System.out.println("Connected to filer, subscribing from " + new Date()); - - while (watch.hasNext()) { - FilerProto.SubscribeMetadataResponse event = watch.next(); - FilerProto.EventNotification notification = event.getEventNotification(); - if (!event.getDirectory().equals(notification.getNewParentPath())) { - // move an entry to a new directory, possibly with a new name - if (notification.hasOldEntry() && notification.hasNewEntry()) { - System.out.println("moved " + 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("created entry " + event.getDirectory() + "/" + notification.getNewEntry().getName()); - } else if (!notification.hasNewEntry() && notification.hasOldEntry()) { - System.out.println("deleted entry " + event.getDirectory() + "/" + notification.getOldEntry().getName()); - } else if (notification.hasNewEntry() && notification.hasOldEntry()) { - System.out.println("updated entry " + event.getDirectory() + "/" + notification.getNewEntry().getName()); - } - } - - } -} diff --git a/other/java/examples/src/main/java/com/seaweedfs/examples/WriteFile.java b/other/java/examples/src/main/java/com/seaweedfs/examples/WriteFile.java deleted file mode 100644 index b0bd54997..000000000 --- a/other/java/examples/src/main/java/com/seaweedfs/examples/WriteFile.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.seaweedfs.examples; - -import seaweedfs.client.FilerGrpcClient; -import seaweedfs.client.SeaweedInputStream; -import seaweedfs.client.SeaweedOutputStream; - -import java.io.IOException; -import java.io.InputStream; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -public class WriteFile { - - public static void main(String[] args) throws IOException { - - FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); - - SeaweedInputStream seaweedInputStream = new SeaweedInputStream( - filerGrpcClient, "/test.zip"); - unZipFiles(filerGrpcClient, seaweedInputStream); - - } - - public static void unZipFiles(FilerGrpcClient filerGrpcClient, InputStream is) throws IOException { - ZipInputStream zin = new ZipInputStream(is); - ZipEntry ze; - while ((ze = zin.getNextEntry()) != null) { - - String filename = ze.getName(); - if (filename.indexOf("/") >= 0) { - filename = filename.substring(filename.lastIndexOf("/") + 1); - } - if (filename.length()==0) { - continue; - } - - SeaweedOutputStream seaweedOutputStream = new SeaweedOutputStream(filerGrpcClient, "/test/"+filename); - byte[] bytesIn = new byte[16 * 1024]; - int read = 0; - while ((read = zin.read(bytesIn))!=-1) { - seaweedOutputStream.write(bytesIn,0,read); - } - seaweedOutputStream.close(); - - System.out.println(ze.getName()); - } - } -} -- cgit v1.2.3