lakefs sdk 使用

因为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();

参考资料

https://docs.lakefs.io/reference/api.html#/
https://github.com/treeverse/lakeFS/tree/master/clients/java

posted on   荣锋亮  阅读(185)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-02-10 dremio 与apache drill 的一些区别
2021-02-10 基于json lines 进行数据交换
2021-02-10 使用idea 自带的工具反编译jar包
2020-02-10 pgspider oracle fdw docker 镜像简单使用说明
2020-02-10 pgspider tds fdw sql server docker 镜像中文乱码问题解决
2019-02-10 awesome-workflow-engines
2019-02-10 Next generation configuration mgmt

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示