1

I try this tutorial

https://docs.aws.amazon.com/qldb/latest/developerguide/getting-started.java.step-2.html but I don't understand how to connect to qldb with the java sdk.

I only need to update a document, but this documentation is so complex. Does anyone have any idea? Or something for dummies.

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);
        }
    }
}

AskYous
  • 4,332
  • 9
  • 46
  • 82
alejandro correa
  • 157
  • 2
  • 12

2 Answers2

2

I'm sorry the documentation is complex. Here is a minimal version of the code you referred to with all the customization and options stripped out. It assumes your environment is setup to use the correct AWS region and credentials.

    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 >);
    }

I'd love to help you further, but your question as it stands doesn't make it clear where you got stuck. For example, did you try run the above code and, if so, did you get an error? If you update your question with more information or respond to my answer in the comments I"ll check back in.

Marc
  • 928
  • 5
  • 8
  • 1
    thanks, i was resolve it, with long time, understanding what thing do what it, is a little complex the code, and little pratic, when you only need a llitle thing. – alejandro correa Mar 13 '20 at 22:53
  • @Bagazo thanks, i only needed update a document in QLDB from java but the example code is so complex, only to understand how to do a little thing – alejandro correa Mar 13 '20 at 23:05
0

so i reduce the code because the example need more experience on QLDB sdk java, and 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;
    }
alejandro correa
  • 157
  • 2
  • 12