Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java-sdk): java sdk support File API (#325) #356

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/java-sdk/README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mvn clean install
* [Hello world](./examples/src/test/java/io/mosn/layotto/examples/helloworld)
* [State management](./examples/src/test/java/io/mosn/layotto/examples/state)
* [Pubsub API](./examples/src/test/java/io/mosn/layotto/examples/pubsub)
* [File API](./examples/src/test/java/io/mosn/layotto/examples/file)

## java sdk开发指南
### java sdk职责
Expand Down
1 change: 1 addition & 0 deletions sdk/java-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Try the following examples to learn more about this SDK:
* [Hello world](./examples/src/test/java/io/mosn/layotto/examples/helloworld)
* [State management](./examples/src/test/java/io/mosn/layotto/examples/state)
* [Pubsub API](./examples/src/test/java/io/mosn/layotto/examples/pubsub)
* [File API](./examples/src/test/java/io/mosn/layotto/examples/file)

## java sdk developer guide
### How to format java sdk code
Expand Down
2 changes: 1 addition & 1 deletion sdk/java-sdk/examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>runtime-sdk-parent</artifactId>
<groupId>io.mosn.layotto</groupId>
<version>1.0.0</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>examples</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2021 Layotto Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mosn.layotto.examples.file;

import io.mosn.layotto.v1.RuntimeClientBuilder;
import io.mosn.layotto.v1.config.RuntimeProperties;
import spec.sdk.runtime.v1.client.RuntimeClient;
import spec.sdk.runtime.v1.domain.file.PutFileRequest;
import spec.sdk.runtime.v1.domain.file.GetFileRequest;
import spec.sdk.runtime.v1.domain.file.ListFileRequest;
import spec.sdk.runtime.v1.domain.file.GetMetaRequest;
import spec.sdk.runtime.v1.domain.file.DelFileRequest;
import spec.sdk.runtime.v1.domain.file.GetFileResponse;
import spec.sdk.runtime.v1.domain.file.ListFileResponse;
import spec.sdk.runtime.v1.domain.file.FileInfo;
import spec.sdk.runtime.v1.domain.file.GetMeteResponse;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
* Specially
* <p>
* 1. add `"local:{}` to "files" node in layotto/configs/config_file.json
* 2. start server by `./layotto start -c ../../configs/config_file.json`
*/
public class File {

private static final Logger logger = Logger.getLogger(File.class.getName());

static String storeName = "local";
static String fileName = "/tmp/test.log";

public static void main(String[] args) throws Exception {

RuntimeClient client = new RuntimeClientBuilder()
.withPort(RuntimeProperties.DEFAULT_PORT)
.build();

putFile(client);
getFile(client);
listFile(client);
getFileMeta(client);
delFile(client);
}

public static void putFile(RuntimeClient client) throws Exception {

PutFileRequest request = new PutFileRequest();
request.setStoreName(storeName);
request.setFileName(fileName);

Map<String, String> meta = new HashMap<>();
meta.put("FileMode", "521");
meta.put("FileFlag", "0777");
request.setMetaData(meta);

request.setIn(new ByteArrayInputStream("hello world".getBytes()));

client.putFile(request, 3000);
}

public static void getFile(RuntimeClient client) throws Exception {

GetFileRequest request = new GetFileRequest();
request.setStoreName(storeName);
request.setFileName(fileName);

Map<String, String> meta = new HashMap<>();
meta.put("k1", "v1");
request.setMetaData(meta);

GetFileResponse resp = client.getFile(request, 3000);

InputStream reader = resp.getIn();

byte[] buf = new byte[128];
for (int len = reader.read(buf); len > 0; len = reader.read(buf)) {
logger.info(new String(buf, 0, len));
}
}

public static void delFile(RuntimeClient client) throws Exception {

DelFileRequest request = new DelFileRequest();
request.setStoreName(storeName);
request.setFileName(fileName);

client.delFile(request, 3000);
}

public static void listFile(RuntimeClient client) throws Exception {

ListFileRequest request = new ListFileRequest();
request.setStoreName(storeName);
request.setMarker("test.log");
request.setName("/tmp");
request.setPageSize(10);

ListFileResponse resp = client.listFile(request, 3000);

for (FileInfo f : resp.getFiles()) {
logger.info(f.getFileName());
}
}

public static void getFileMeta(RuntimeClient client) throws Exception {

GetMetaRequest request = new GetMetaRequest();
request.setStoreName(storeName);
request.setFileName(fileName);

GetMeteResponse response = client.getFileMeta(request, 3000);
logger.info(response.getLastModified());
logger.info("" + response.getMeta().size());
}
}
2 changes: 1 addition & 1 deletion sdk/java-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk-parent</artifactId>
<version>1.0.0</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>runtime-sdk-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion sdk/java-sdk/sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk-parent</artifactId>
<version>1.0.0</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>runtime-sdk</artifactId>
Expand Down
Loading