因为lakefs 是基于openapi 开发的后端,所以提供sdk 是很简单的,同时官方的sdk就是基于代码生成的
构建sdk
官方提供了中央仓库里的,也可以自己构建
- java 版本自己构建
cd clients/java
mvn clean package install -Dmaven.test.skip -Dgpg.skip
使用
- maven 引用
<dependencies>
<dependency>
<groupId>io.lakefs</groupId>
<artifactId>api-client</artifactId>
<version>0.56.0</version>
</dependency>
</dependencies>
- 代码集成
lakefs sdk 的集成模式就是标准openapi client 的处理,初始化ApiClient,获取token,具体业务操作api初始化传递 ApiClient
进行业务操作就可以了,整体还是比较简单的
package com.dalong;
import io.lakefs.clients.api.*;
import io.lakefs.clients.api.auth.HttpBearerAuth;
import io.lakefs.clients.api.model.AuthenticationToken;
import io.lakefs.clients.api.model.LoginInformation;
import io.lakefs.clients.api.model.Repository;
import java.util.function.Consumer;
public class App {
public static void main(String[] args) throws ApiException {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://localhost:8000/api/v1");
RepositoriesApi repositoriesApi = new RepositoriesApi(defaultClient);
AuthApi authApi = new AuthApi(defaultClient);
LoginInformation loginInformation = new LoginInformation();
loginInformation.secretAccessKey("xxxxxxxx");
loginInformation.setAccessKeyId("xxxxxx");
AuthenticationToken jwtToken = authApi.login(loginInformation);
HttpBearerAuth jwt_token = (HttpBearerAuth) defaultClient.getAuthentication("jwt_token");
jwt_token.setBearerToken(jwtToken.getToken());
try {
repositoriesApi.listRepositories("","",null).getResults().forEach(new Consumer<Repository>() {
@Override
public void accept(Repository repository) {
String repoFormat = "createtime: %s====storagenamespace: %s=====default branch: %s========repositry id: %s";
System.out.println(String.format(repoFormat,repository.getCreationDate(),repository.getStorageNamespace(),repository.getDefaultBranch(),repository.getId()));
}
});
} catch (ApiException e) {
System.err.println("Exception when calling ActionsApi#getRun");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}
说明
官方代码库中的文档还是比较全的,可以直接参考使用,同时对于apiclient 在多线程环境,官方建议是使用独立的
参考实例
package com.dalong;
import io.lakefs.clients.api.ApiClient;
import io.lakefs.clients.api.Configuration;
public class SingletonApiClient {
private SingletonApiClient() {
}
private static ThreadLocal<ApiClient> _threadLocal =
new ThreadLocal<ApiClient>() {
@Override
protected ApiClient initialValue() {
return Configuration.getDefaultApiClient();
}
};
public static ApiClient getInstance() {
return _threadLocal.get();
}
}
使用
ApiClient defaultClient = SingletonApiClient.getInstance();