Java createUserDefinedFunction:是否已存在?

Java createUserDefinedFunction:是否已存在?,java,azure-cosmosdb,Java,Azure Cosmosdb,我使用来创建和使用“用户定义函数(UDF)” 因此,从中我终于找到了(使用Java客户端)创建UDF的方法: String regexUdfJson = "{" + "id:\"REGEX_MATCH\"," + "body:\"function (input, pattern) { return input.match(pattern) !== null; }\"," + "}"; UserDefinedFunction udfREG

我使用来创建和使用“用户定义函数(UDF)”

因此,从中我终于找到了(使用Java客户端)创建UDF的方法:

String regexUdfJson = "{"
          + "id:\"REGEX_MATCH\","
          + "body:\"function (input, pattern) { return input.match(pattern) !== null; }\","
          + "}";
UserDefinedFunction udfREGEX = new UserDefinedFunction(regexUdfJson);
getDC().createUserDefinedFunction(
    myCollection.getSelfLink(),
    udfREGEX,
    new RequestOptions());
下面是一个示例查询:

SELECT * FROM root r WHERE udf.REGEX_MATCH(r.name, "mytest_.*")
我只能创建一次自定义项,因为如果我尝试重新创建现有的自定义项,会出现异常:

DocumentClientException: Message: {"Errors":["The input name presented is already taken. Ensure to provide a unique name property for this resource type."]}
我该如何知道UDF是否已经存在? 我尝试使用“readUserDefinedFunctions”函数,但没有成功。有什么例子/其他想法吗


也许从长远来看,我们应该建议在

上使用“createOrReplaceUserDefinedFunction(…)”,您可以通过使用
queryUserDefinedFunctions
运行查询来检查现有的UDF

例如:

List<UserDefinedFunction> udfs = client.queryUserDefinedFunctions(
        myCollection.getSelfLink(),
        new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id",
                         new SqlParameterCollection(new SqlParameter("@id", myUdfId))),
        null).getQueryIterable().toList();
if (udfs.size() > 0) {
    // Found UDF.
}
List udfs=client.queryUserDefinedFunctions(
myCollection.getSelfLink(),
新建SqlQuerySpec(“从根r中选择*,其中r.id=@id”,
新的SqlParameterCollection(新的SqlParameter(“@id”,myUdfId)),
null).GetQueryItemable().toList();
如果(udfs.size()>0){
//找到了UDF。
}

这是对.NET用户的回答

`var collectionAltLink = documentCollections["myCollection"].AltLink; // Target collection's AltLink
var udfLink = $"{collectionAltLink}/udfs/{sampleUdfId}"; // sampleUdfId is your UDF Id
var result = await _client.ReadUserDefinedFunctionAsync(udfLink);
var resource = result.Resource;
if (resource != null)
{
   // The UDF with udfId exists
}`
这里的
\u client
是Azure的
DocumentClient
documentCollections
是documentDb集合的字典


如果上述集合中没有这样的UDF,
\u客户端
抛出NotFound异常。

亲爱的illuminator@abatishchev为什么要将我的java代码更新为c?对此表示抱歉。你的问题缺少语言标签。我放错了。