将Java函数输出到DocumentDB

时间:2018-01-30 09:07:37

标签: java azure-cosmosdb azure-functions

我正在研究一个具有计时器触发器的Java函数,并尝试在DocumentDB集合中插入一条简单记录。 Java代码如下

import java.util.Calendar;
import java.util.UUID;

import com.microsoft.azure.documentdb.Document;
import com.microsoft.azure.serverless.functions.ExecutionContext;
import com.microsoft.azure.serverless.functions.annotation.*;

public class DocumentDBFunction {
    @FunctionName("documentDBFunction")
    @DocumentDBOutput(name = "testdoc",
                  createIfNotExists = true,
                  databaseName = "functiondb",
                  collectionName="functioncoll",
                  connection = "CosmosDBConnectionString")
    public Document functionHandler(
            @TimerTrigger(name = "timerInfo", schedule = "*/30 * * * * *")
            String timerInfo,
            final ExecutionContext executionContext) {

    String randomString=UUID.randomUUID().toString();
    executionContext.getLogger().info("Insert obj documentDB: " + randomString);
    Document document=new Document();
    document.set("id",randomString);
    document.set("_id",randomString);
    document.set("uuid", randomString);
    return document;
    }
}

CosmosDBConnectionString在AppSetting中可用,我也创建了该集合。 Function.json是以下

{
  "scriptFile": "../azurefunctions-1.0-SNAPSHOT.jar",
  "entryPoint":     "test.azure.functions.DocumentDBFunction.functionHandler",
  "bindings": [
    {
      "type": "timerTrigger",
      "name": "timerInfo",
      "direction": "in",
      "schedule": "*/30 * * * * *"
    },
    {
      "type": "documentdb",
      "name": "$return",
      "direction": "out",
      "databaseName": "functiondb",
      "collectionName": "functioncoll",
      "connection": "CosmosDBConnectionString",
      "createIfNotExists": true
    }
  ],
  "disabled": false
}

如果我运行此函数,我在日志文件上没有错误,但DocumentDB集合中没有新对象。有任何想法吗?欢迎提出意见和建议

1 个答案:

答案 0 :(得分:2)

我试图测试你的java azure功能代码。

Java代码:

@FunctionName("doc")
@DocumentDBOutput(name = "testdoc",
            createIfNotExists = true,
            databaseName = "db",
            collectionName="coll",
            connection = "CosmosDBConnectionString")
public String functionHandler(
            @TimerTrigger(name = "timerInfo", schedule = "*/30 * * * * *")
                    String timerInfo,
            final ExecutionContext executionContext) {

        String randomString=UUID.randomUUID().toString();
        executionContext.getLogger().info("Insert obj documentDB: " + randomString);
        Document document=new Document();
        document.set("id",randomString);
        document.set("name","Jay!!!");
        return document.toString();
    }

function.json

{
  "scriptFile" : "..\\fabrikam-functions-1.0-SNAPSHOT.jar",
  "entryPoint" : "com.fabrikam.functions.Function.functionHandler",
  "bindings" : [ {
    "type" : "timerTrigger",
    "name" : "timerInfo",
    "direction" : "in",
    "schedule" : "*/30 * * * * *"
  }, {
    "type" : "documentdb",
    "name" : "$return",
    "direction" : "out",
    "databaseName" : "db",
    "collectionName" : "coll",
    "connection" : "CosmosDBConnectionString",
    "createIfNotExists" : true
  } ],
  "disabled" : false
}

Azure功能在本地成功运行,但文档不会像您一样传递到Document DB中。

enter image description here

我尝试在Azure中运行相同的代码,但它没有显示出任何差异。

enter image description here

据我所知,Java Azure功能仍然是preview版本,我发现针对Annotation的{​​{1}} Cosmos dbN/A

enter image description here

您可以检查webjob仪表板,以验证AzureWebJobsStorage中配置的表存储中是否存在任何错误日志。

此外,我建议您在TimerTrigger中调用Document DB Java SDK。请参阅以下代码片段:

private static final String account = "***";
private static final String key = "***";

private static DocumentClient client = new DocumentClient("***",
        key, ConnectionPolicy.GetDefault(),
        ConsistencyLevel.Session);

@FunctionName("doc")
public String functionHandler(
        @TimerTrigger(name = "timerInfo", schedule = "*/5 * * * * *")
                String timerInfo,
        final ExecutionContext executionContext) {

    try {
        String randomString = UUID.randomUUID().toString();
        executionContext.getLogger().info("Insert obj documentDB: " + randomString);
        Document document = new Document();
        document.set("id", randomString);
        document.set("name", "Jay!!!");
        client.createDocument("dbs/db/colls/coll",document,null,false);
        return "Insert Success id: "+ randomString;
    } catch (Exception e) {
        executionContext.getLogger().info(e.toString());
        return e.toString();
    }

}

enter image description here

希望它对你有所帮助。