Java client for accessing Algorithmia's algorithm marketplace and data APIs.
The Algorithmia java client is published to Maven central and can be added as a dependency via:
<dependency>
<groupId>com.algorithmia</groupId>
<artifactId>algorithmia-client</artifactId>
<version>[,1.1.0)</version>
</dependency>Instantiate a client using your API Key:
AlgorithmiaClientclient = Algorithmia.client(apiKey);Notes:
- API key may be omitted only when making calls from algorithms running on the Algorithmia cluster
- Using version range
[,1.1.0)is recommended as it implies using the latest backward-compatible bugfixes.
Algorithms are called with the pipe method using
any input that can be serialized to JSON, or binary byte data.
AlgorithmaddOne = client.algo("docs/JavaAddOne");
AlgoResponseresponse = addOne.pipe(41);
Integerresult = response.as(newTypeToken<Integer>(){});
DoubledurationInSeconds = response.getMetadata().duration;If you already have serialzied JSON, you can call call pipeJson instead:
Algorithmfoo = client.algo("")
StringjsonWords = "[\"transformer\", \"terraforms\", \"retransform\"]"AlgoResponseresponse = addOne.pipeJson(jsonWords)You can also set options (query parameters in the API spec) on calls. There are several approaches to do this, and they are all equivalent
// Helper methods for specific parameters in the API spec:AlgorithmaddOne = client.algo("docs/JavaAddOne")
.setTimeout(5, TimeUnit.MINUTES)
.setStdout(false);
AlgoResponseresponse = addOne.pipe(41);For an algorithm that returns a string:
stringResult.as(newTypeToken<String>(){});For an algorithm that returns an array of strings:
stringArrayResult.as(newTypeToken<List<String>>(){});For an algorithm that returns a custom class, cast the result to that class:
classCustomClass {
intmaxCount;
List<String> items;
}
customClassResult.as(newTypeToken<CustomClass>(){});For debugging, it is often helpful to get the JSON String representation of the result:
anyResult.asJsonString();In order to cast the result to a specific type, call .as() with a TypeToken.
On the right pane, you'll find examples of how to do this to return a string, an array of strings, and a custom class.
Manage your data stored within Algorithmia:
// Create a directory "foo"DataDirectoryfoo = client.dir("data://.my/foo");
foo.create();
// Create a directory with specific ACLDataDirectoryfooLimited = client.dir("data://.my/fooLimited");
fooLimited.create(DataAcl.PRIVATE);
// Or, update the directory's ACL after creationfooLimited.updatePermissions(DataAcl.PRIVATE);
// View the directory's permissionsfooLimited.getPermissions().getReadPermissions() == DataAclType.PRIVATE// Upload files to "foo" directoryfoo.file("sample.txt").put("sample text contents");
foo.file("binary_file").put(newbyte[] { (byte)0xe0, 0x4f, (byte)0xd0, 0x20 });
foo.putFile(newFile("/path/to/myfile"));
// List files in "foo"for(DataFilefile : foo.getFileIter()) {
System.out.println(file.toString() " at URL: " + file.url());
}
// Get contents of filesStringsampleText = foo.file("sample.txt").getString();
byte[] binaryContent = foo.file("binary_file").getBytes();
FiletempFile = foo.file("myfile").getFile();
// Delete files and directoriesfoo.file("sample.txt").delete();
foo.delete(true); // true implies force deleting the directory and its contents