mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
Updated SeaweedFS Java Client (markdown)
parent
90b3ef05eb
commit
0a7d62c019
|
@ -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
|
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.
|
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<FilerProto.SubscribeMetadataResponse> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in a new issue