From 0a7d62c019404fe5ce48f3d39f143ed502ae06d9 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 5 Feb 2021 00:30:30 -0800 Subject: [PATCH] Updated SeaweedFS Java Client (markdown) --- SeaweedFS-Java-Client.md | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/SeaweedFS-Java-Client.md b/SeaweedFS-Java-Client.md index ead04f2..0eeca65 100644 --- a/SeaweedFS-Java-Client.md +++ b/SeaweedFS-Java-Client.md @@ -5,3 +5,59 @@ Here is an SeaweedFS Java API implementation refactored out of the existing code https://github.com/chrislusf/seaweedfs/tree/master/other/java/examples/src/main/java/com/seaweedfs/examples Basically this library will be able to read and write directly to the volume servers, and only use filer servers for meta data. +If you use http to read or write directly via filer, the data still needs to go through filer, which is less efficient. + +## Read File + +``` + FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); + SeaweedInputStream seaweedInputStream = new SeaweedInputStream(filerGrpcClient, "/test.zip"); + // next, you can use seaweedInputStream as a normal InputStream +``` + +## Write File + +``` + FilerGrpcClient filerGrpcClient = new FilerGrpcClient("localhost", 18888); + SeaweedOutputStream seaweedOutputStream = new SeaweedOutputStream(filerGrpcClient, "/test/"+filename); + // next, you can use seaweedOutputStream as a normal OutputStream +``` + +## Watch file changes + +This API streams meta data changes. + +The following is one implementation. It just watch the folder "/buckets" and all the meta data changes under the folder and all sub folders recursively. A bit more code, but should be powerful and simple to use. + +``` + 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()); + } + } +```