如何将 java sdk 连接到 qldb?

How do I connect the java sdk to qldb?

我试试这个教程

https://docs.aws.amazon.com/qldb/latest/developerguide/getting-started.java.step-2.html 但我不明白如何使用 java sdk 连接到 qldb。

我只需要更新一个文档,但是这个文档太复杂了。有人有什么主意吗?或者傻瓜的东西。

public final class ConnectToLedger {
    public static final Logger log = LoggerFactory.getLogger(ConnectToLedger.class);
    public static AWSCredentialsProvider credentialsProvider;
    public static String endpoint = null;
    public static String ledgerName = Constants.LEDGER_NAME;
    public static String region = null;

    public static PooledQldbDriver driver = createQldbDriver();

    private ConnectToLedger() { }

    /**
     * Create a pooled driver for creating sessions.
     *
     * @return The pooled driver for creating sessions.
     */
    public static PooledQldbDriver createQldbDriver() {
        AmazonQLDBSessionClientBuilder builder = AmazonQLDBSessionClientBuilder.standard();
        if (null != endpoint && null != region) {
            builder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region));
        }
        if (null != credentialsProvider) {
            builder.setCredentials(credentialsProvider);
        }
        return PooledQldbDriver.builder()
                .withLedger(ledgerName)
                .withRetryLimit(Constants.RETRY_LIMIT)
                .withSessionClientBuilder(builder)
                .build();
    }

    /**
     * Connect to a ledger through a {@link QldbDriver}.
     *
     * @return {@link QldbSession}.
     */
    public static QldbSession createQldbSession() {
        return driver.getSession();
    }

    public static void main(final String... args) {
        try (QldbSession qldbSession = createQldbSession()) {
            log.info("Listing table names ");
            for (String tableName : qldbSession.getTableNames()) {
                log.info(tableName);
            }
        } catch (QldbClientException e) {
            log.error("Unable to create session.", e);
        }
    }
}

很抱歉文档很复杂。这是您引用的代码的最小版本,其中删除了所有自定义和选项。它假定您的环境设置为使用正确的 AWS 区域和凭证。

    PooledQldbDriver driver = PooledQldbDriver.builder()
            .withLedger("my-ledger-name")
            .withSessionClientBuilder(AmazonQLDBSessionClientBuilder.standard())
            .build();

    try (QldbSession session = driver.getSession()) {
            session.execute("UPDATE my-table SET my-field = ?", < Ion value here >);
    }

我很乐意为您提供进一步的帮助,但您的问题目前并没有说明您被困在哪里。例如,您是否尝试 运行 上面的代码,如果是,您是否遇到错误?如果您使用更多信息更新您的问题或在评论中回复我的回答,我会回来查看。

所以我减少了代码,因为该示例需要更多关于 QLDB sdk java 和 java 的经验。

 public QldbSession getQldbSession(String ledgerName) {
        final AmazonQLDBSessionClientBuilder builder = AmazonQLDBSessionClientBuilder.standard();
        if (null != endpoint && null != region) {
            builder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region));
        }
        if (null != credentialsProvider) {
            builder.setCredentials(credentialsProvider);
        }
        final PooledQldbDriver driver = PooledQldbDriver.builder().withLedger(ledgerName).withRetryLimit(4)
                .withSessionClientBuilder(builder).build();
        return  driver.getSession();
    }
 Result result = null;
        try  {
            final String query = "!query here¡";
            final IonObjectMapper MAPPER = new IonValueMapper(IonSystemBuilder.standard().build());
            final List<IonValue> parameters = new ArrayList<>();
            parameters.add(MAPPER.writeValueAsIonValue("parameter"));
            parameters.add(MAPPER.writeValueAsIonValue("parameter"));
            parameters.add(MAPPER.writeValueAsIonValue("parameter"));
            result = qldbSession.execute(query, parameters);

        } catch (final QldbClientException e) {
            System.out.println("Unable to create session.");
        } catch (final IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }