FalconDB API¶
API¶
Base URL depends on the version of FalconDB:
- FalconDB - Onteon:
http://localhost:8021/_by_name/falcon-db-core-microservice/v1
- FalconDB - standalone:
http://localhost:${PORT}
, where${PORT}
is defined infalcondb/envs
file in-Dhttp-port
system properties (by defaulthttp://localhost:7000/v1
)
NOTE
In the following Swagger API http://localhost:8021/_by_name/falcon-db-core-microservice/v1
is set as base URL.
Examples¶
Dependencies used in the following Java examples
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>
Save file to FalconDB via stream¶
Path path = Paths.get("/path/to/file");
byte[] fileByte;
try {
fileByte = Files.readAllBytes(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut("http://localhost:8020/_by_name/falcon-db-core-microservice/v1/saveBinaryFile");
httpPut.addHeader("key", "file-key");
httpPut.addHeader("fileName", "file");
multipartEntityBuilder.addBinaryBody("file", fileByte, ContentType.DEFAULT_BINARY, "file");
HttpEntity multipart = multipartEntityBuilder.build();
httpPut.setEntity(multipart);
try {
CloseableHttpResponse execute = client.execute(httpPut);
} catch (IOException e) {
e.printStackTrace();
}
curl --location 'http://localhost:8020/_by_name/falcon-db-core-microservice/v1/saveBinaryFile' \
--header 'key: file-key' \
--header 'fileName: file' \
--form 'file=@"/path/to/file"'
Get file as stream¶
String fileKey = "file-key";
String fileName = "test-file";
int chunkSize = 16;
String outputFilePath = "/path/to/file";
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:7000/v1/getBinaryFile?key=" + fileKey +
"&file-name=" + fileName + "&chunk-size=" + chunkSize);
try (CloseableHttpResponse response = client.execute(httpGet)) {
HttpEntity entity = response.getEntity();
if (Objects.nonNull(entity)) {
InputStream responseInputStream = entity.getContent();
File outputFile = new File(outputFilePath);
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
byte[] chunk = new byte[chunkSize];
int bytesRead;
while ((bytesRead = responseInputStream.read(chunk)) != -1) {
fileOutputStream.write(chunk, 0, bytesRead);
}
}
}
} catch (Throwable th) {
throw new Exception(th);
}
} catch (Exception e) {
e.printStackTrace();
}
curl -X GET 'http://localhost:7000/v1/getBinaryFile?file-name=test-file&key=file-key&chunk-size=16' -o output_file.txt
Save file to FalconDB as base64 chunks¶
File file = new File("/path/to/file");
int chunkSize = 16;
try (FileInputStream fileInputStream = new FileInputStream(file)) {
byte[] chunk = new byte[chunkSize];
int bytesRead;
while ((bytesRead = fileInputStream.read(chunk)) != -1) {
FileDTO fileDTO = new FileDTO();
String base64Encoded = Base64.getEncoder().encodeToString(chunk);
fileDTO.setFileName("test-file");
fileDTO.setKey("file-key");
fileDTO.setBase64FileContent(base64Encoded);
String fileDTOAsJson = objectMapper.writeValueAsString(fileDTO);
try (CloseableHttpClient client = HttpClients.createDefault()) {
EntityBuilder entityBuilder = EntityBuilder.create();
HttpPut httpPut = new HttpPut("http://localhost:7000/v1/saveOrAppendFile");
entityBuilder.setText(fileDTOAsJson);
httpPut.setEntity(entityBuilder.build());
client.execute(httpPut);
}
}
} catch (Exception e) {
e.printStackTrace();
}
Get file in base64 chunks¶
String fileKey = "file-key";
String fileName = "test-file";
ObjectMapper objectMapper = new ObjectMapper();
StringBuilder stringBuilder = new StringBuilder();
int chunkNumber = 0;
int chunkSize = 512;
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:7000/v1/getFileChunk?key=" + fileKey +
"&file-name=" + fileName + "&chunk-size=" + chunkSize + "&chunk-number=" + chunkNumber);
CloseableHttpResponse execute;
InputStream inputStream = null;
FileChunkOutputDTO fileChunk = null;
try {
execute = client.execute(httpGet);
HttpEntity entity = execute.getEntity();
inputStream = entity.getContent();
byte[] bytes = inputStream.readAllBytes();
fileChunk = objectMapper.readValue(bytes, FileChunkOutputDTO.class);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (Objects.nonNull(inputStream)) {
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
if (!fileChunk.isEOF()) {
stringBuilder.append(fileChunk.getBase64FileContent());
chunkNumber++;
while (!fileChunk.isEOF()) {
try {
client = HttpClients.createDefault();
httpGet = new HttpGet("http://localhost:7000/v1/getFileChunk?key=" + fileKey +
"&file-name=" + fileName + "&chunk-size=" + chunkSize + "&chunk-number=" + chunkNumber);
execute = client.execute(httpGet);
HttpEntity entity = execute.getEntity();
inputStream = entity.getContent();
byte[] bytes = inputStream.readAllBytes();
fileChunk = objectMapper.readValue(bytes, FileChunkOutputDTO.class);
if (!fileChunk.isEOF()) {
stringBuilder.append(fileChunk.getBase64FileContent());
chunkNumber++;
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (Objects.nonNull(inputStream)) {
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
String base64FileContent = stringBuilder.toString();