Git Product home page Git Product logo

weed-client's Introduction

SeaweedFS Client For Java

Build Status codecov Codacy Badge Maven Central GitHub Release MIT license

Features

SeaweedFS is a simple and highly scalable distributed file system and started by implementing Facebook's Haystack design paper. SeaweedFS is currently growing, with more features on the way.

This Java client is encapsulates the functionality full of the SeaweedFS API and provides a simple interface.

For performance, we make some progress:

  • Include Ehcache and used it for cached lookup volume.
  • Include HttpClient Connection Pool for handle HTTP request.
  • Volume server location load balance.
  • Heuristic cache is used for fetch file stream.
  • Auto switch leader server (master) at failover.

Quick Start

Maven
<dependency>
  <groupId>org.lokra.seaweedfs</groupId>
  <artifactId>seaweedfs-client</artifactId>
  <version>0.7.3.RELEASE</version>
</dependency>
Gradle
repositories {
    mavenCentral()
}

dependencies {
    compile('org.lokra.seaweedfs:seaweedfs-client:0.7.3.RELEASE')
}
Create a connection manager
FileSource fileSource = new FileSource();
// SeaweedFS master server host
fileSource.setHost("localhost");
// SeaweedFS master server port
fileSource.setPort(9333);
// Startup manager and listens for the change
fileSource.startup();
Create a file operation template
// Template used with connection manager
FileTemplate template = new FileTemplate(fileSource.getConnection());
template.saveFileByStream("filename.doc", someFile);

License

The MIT License (MIT)

Copyright (c) 2016 Lokra Studio

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

weed-client's People

Contributors

apocarteres avatar chihosin avatar guo9310 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

weed-client's Issues

how to use yourself unique id

When I find a file ,I must use two params ( volum id and magic number) as fid .
How I want to use myself uniqu_id (just like uuid) ,and I also want to use this uuid that it can find out file
Thx !

org.ehcache.spi.serialization.UnsupportedTypeException: No serializer found for type LookupVolumeResult

FileSource fileSource = new FileSource(); // SeaweedFS master server host fileSource.setHost("192.168.100.114"); // SeaweedFS master server portaaaaa fileSource.setPort(9335); // Startup manager and listens for the change try { fileSource.startup(); } catch (IOException e) { e.printStackTrace(); } FileTemplate template = new FileTemplate(fileSource.getConnection()); File file = new File("G:\\007.JPG"); FileHandleStatus status = template.saveFileByStream("007.jpg", new FileInputStream(file));

that's my code get this error
org.ehcache.spi.serialization.UnsupportedTypeException: No serializer found for type LookupVolumeResult.class
so i rewrite LookupVolumeResult to implements java.io.Serializable the problem sloved

Cannot get the quickstart example to work

This is the code that I have right now, and it puzzles me. It doesn't work. I'm sure I have SeaweedFS set up correctly because I got pyseaweed to work perfectly: https://github.com/utek/pyseaweed.

import org.lokra.seaweedfs.core.FileSource;
import org.lokra.seaweedfs.core.FileTemplate;

import java.io.*;

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World");

        FileSource fileSource = new FileSource();
        fileSource.setHost("localhost");
        fileSource.setPort(9333);
        try {
            fileSource.startup();
            System.out.println("Finished starting up!");
        } catch (IOException e) {
            System.out.println("Aiya! Cannot connect!");
            return;
        }

        try {
            InputStream is = new FileInputStream("/Users/Pac/Code/golang/flow.jpg");
            FileTemplate template = new FileTemplate(fileSource.getConnection());
            template.saveFileByStream("flow.jpg", is);
        } catch(FileNotFoundException e) {
            System.out.println("Cannot save the file!");
            return;
        } catch(IOException e) {
            System.out.println("Cannot write to Seaweed!");
            return;
        }

        System.out.println("Success and done!");
    }
}

This is the console output.

Hello, World
Nov 23, 2016 11:00:11 PM org.lokra.seaweedfs.core.FileSource startup
INFO: start connect to the seaweedfs core server [http://localhost:9333]
Nov 23, 2016 11:00:13 PM org.lokra.seaweedfs.core.Connection startup
INFO: core connection is startup now
Finished starting up!
329 [main] INFO org.ehcache.core.EhcacheManager - Cache 'lookupVolumeCache' created in EhcacheManager.
Cannot write to Seaweed!

Documentation / Developer Guide?

Hello,

I'm interested in using your library in a production system, and was wondering if there's a developer guide somewhere I couldn't find? Your library looks promising.

Fetching FileHandleStatus first and FileStream after leads to NPE

Whenever you request a file and try to get the FileHandleStatus and after that the FileStream to i.e. compose a response with both combined, there will be a NPE thrown at org.lokra.seaweedfs.core.Connection.fetchStreamCacheByRequest(Connection.java:328).

public class SeaweedFile {
	@JsonProperty
	public FileHandleStatus status;
	@JsonProperty
	public String data;
}

@GET
@Path("{id}")
public Response read(@PathParam("id") String fileID) {
	Response response = null;
	FileTemplate template = new FileTemplate(fileSource.getConnection());
	try {
		SeaweedFile file = new SeaweedFile();
		file.setStatus(template.getFileStatus(fileID)); // first get the FileStatus  *X
		
		StreamResponse fileStream = template.getFileStream(fileID); // *Y
		InputStream is = fileStream.getInputStream();
		file.setData(new String(ByteStreams.toByteArray(is)));
               // do return stuff ...
       } ...
}

If you switch lines marked with *X and *Y, the request will return smoothly.

Get filename is wrong .

seaweedfs use http head method to get file information.

Headers:

Accept-Ranges →bytes
Content-Disposition →inline; filename="1.txt"
Content-Encoding →gzip
Content-Length →686
Content-Type →text/plain; charset=utf-8
Date →Mon, 03 Jul 2017 06:45:16 GMT
Etag →"1e1b55d9"
Last-Modified →Mon, 03 Jul 2017 03:05:04 GMT

seaweedfs-client code here

FileTemplate#getFileStatus(String)
headerResponse.getLastHeader("Content-Disposition").getValue().substring(10, headerResponse.getLastHeader("Content-Disposition").getValue().length() - 1)

resolved result is : lename="1.txt

maybe it can be

Header contentDisposition = headerResponse.getLastHeader("Content-Disposition");
HeaderElement[] elements = contentDisposition.getElements();
if(ArrayUtils.isEmpty(elements))  { // org.apache.commons.lang3.ArrayUtils
  return "";
}else {
  return elements[elements.length-1].getValue();
}

InputStream is instantly closed when response size is larger than fileStreamCacheSize

line 267 of Connection.java:
EntityUtils.consume(entity);

this line of code will close the InputStream of reponse entity instantly, which cause the InputStream returned throw Exception when read.
So you simply cannot read a file if reponse body is larger than fileStreamCacheSize!

It won't work if you just delete this line of code. The connection is closed in the finally clause. So the InputStream returned by FileTemplate.getFileStream will always be closed when you try to read it.

The fix is to determine whether the reponse is cacheable(smaller than fileStreamCacheSize), if is not, read the response body into memory and return a InputStream point to the memory.

HTTP Connection Pooling is not working

In Connection.java:, there are 4 places found with code request.setHeader("Connection", "close");

This line of code disabled the keep-alive feature, so the connection will be discarded instead of pooled.

I don't know why did the author explicitly disabled keep-alive feature while using a PooledHttpClient.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.