如何以编程方式启动本地 DynamoDB? [英] How to launch local DynamoDB programmatically?

查看:15
本文介绍了如何以编程方式启动本地 DynamoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下命令从 bash 启动 本地 DynamoDB 服务器:

I am able to launch a local DynamoDB server from bash through this command:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb &

难道没有一种纯 java 的方式来用自己的代码启动服务器吗?我不是指通过 Process 对象对 shell 的 java 调用,而是一种在我运行我的应用程序时,服务器启动,当我的应用程序被终止时,服务器被终止的方式.

Is there not a pure-java way to start the server in one's code? I don't mean a java callout to the shell through the Process object but a way such that when I run my app, the server starts, and when my app is killed, the server is killed.

如果存在这样的模式,我可以使用嵌入式数据库,但反映服务器一致性语义的东西会是理想的.

I can live with an embedded database if such a mode exists, though something that reflects server consistency semantics would be ideal.

推荐答案

2015 年 9 月 23 日

2015 年 8 月 3 日 上有一个公告,现在添加在同一进程中运行嵌入式 DynamoDB 本地的能力.您可以添加一个 Maven 测试依赖项并使用以下方式之一来运行它.

There was an announcement on Aug 3, 2015 that now adds the ability to have an embedded DynamoDB local running in the same process. You can add a Maven test dependency and use one of the ways below to run it.

<!--Dependency:-->
<dependencies>
    <dependency>
       <groupId>com.amazonaws</groupId>
       <artifactId>DynamoDBLocal</artifactId>
       <version>[1.11,2.0)</version>
    </dependency>
</dependencies>
<!--Custom repository:-->
<repositories>
    <repository>
       <id>dynamodb-local-oregon</id>
       <name>DynamoDB Local Release Repository</name>
       <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
    </repository>
</repositories>

这是一个取自 awslabs/aws-dynamodb-examples Github 存储库:

And here is an example taken from the awslabs/aws-dynamodb-examples Github repository:

AmazonDynamoDB dynamodb = null;
try {
    // Create an in-memory and in-process instance of DynamoDB Local that skips HTTP
    dynamodb = DynamoDBEmbedded.create().amazonDynamoDB();
    // use the DynamoDB API with DynamoDBEmbedded
    listTables(dynamodb.listTables(), "DynamoDB Embedded");
} finally {
    // Shutdown the thread pools in DynamoDB Local / Embedded
    if(dynamodb != null) {
        dynamodb.shutdown();
    }
}

// Create an in-memory and in-process instance of DynamoDB Local that runs over HTTP
final String[] localArgs = { "-inMemory" };
DynamoDBProxyServer server = null;
try {
    server = ServerRunner.createServerFromCommandLineArgs(localArgs);
    server.start();

    dynamodb = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
        // we can use any region here
        new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
        .build();

    // use the DynamoDB API over HTTP
    listTables(dynamodb.listTables(), "DynamoDB Local over HTTP");
} finally {
    // Stop the DynamoDB Local endpoint
    if(server != null) {
        server.stop();
    }
}

<小时>

旧答案

正如您所说,目前 DynamoDBLocal 或 SDK 没有内置方式来执行此操作.如果您可以在同一进程中启动嵌入式 DynamoDBLocal,那就太好了.

Like you said, there is currently no built-in way from DynamoDBLocal or the SDK to do this right now. It would be nice if there was an embedded DynamoDBLocal that you could start up in the same process.

这是一个简单的解决方法/解决方案,使用 java.lang.Process 以编程方式启动和关闭它,以防其他人感兴趣.

Here is a simple workaround/solution using java.lang.Process to start it up and shut it down programmatically in case others are interested.

可在此处找到 DynamoDBLocal 的文档和以下是参数的当前定义:

Documentation for DynamoDBLocal can be found here and here are the current definition of the arguments:

  • -inMemory — 在内存中运行,无文件转储
  • -port 4000 — 使用端口 4000 进行通信.
  • -sharedDb — 使用单个数据库文件,而不是为每个凭据和区域使用单独的文件
  • -inMemory — Run in memory, no file dump
  • -port 4000 — Communicate using port 4000.
  • -sharedDb — Use a single database file, instead of separate files for each credential and region

请注意,这是使用截至 2015 年 8 月 5 日的最新版本的 DynamoDBLocal.

final ProcessBuilder processBuilder = new ProcessBuilder("java",
        "-Djava.library.path=./DynamoDBLocal_lib",
        "-jar",
        "DynamoDBLocal.jar",
        "-sharedDb",
        "-inMemory",
        "-port",
        "4000")
        .inheritIO()
        .directory(new File("/path/to/dynamo/db/local"));

final Process process = processBuilder.start();

Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        System.out.println("Shutdown DynamoDBLocal");
        process.destroy();
        try {
            process.waitFor(3, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            System.out.println("Process did not terminate after 3 seconds.");
        }
        System.out.println("DynamoDBLocal isAlive=" + process.isAlive());
    }
});
// Do some stuff

这篇关于如何以编程方式启动本地 DynamoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆