如何在 Azure Cosmos DB 中注册和使用存储过程、触发器与用户定义的函数How to register and use stored procedures, triggers, and user-defined functions in Azure Cosmos DB

Azure Cosmos DB 中的 SQL API 支持注册和调用以 JavaScript 编写的存储过程、触发器与用户定义函数 (UDF)。The SQL API in Azure Cosmos DB supports registering and invoking stored procedures, triggers, and user-defined functions (UDFs) written in JavaScript. 可以使用 SQL API .NET.NET CoreJavaJavaScriptNode.jsPython SDK 注册和调用存储过程。You can use the SQL API .NET, .NET Core, Java, JavaScript, Node.js, or Python SDKs to register and invoke the stored procedures. 在定义一个或多个存储过程、触发器和用户定义的函数之后,可以使用数据资源管理器在 Azure 门户中加载和查看它们。Once you have defined one or more stored procedures, triggers, and user-defined functions, you can load and view them in the Azure portal by using Data Explorer.

如何运行存储过程How to run stored procedures

存储过程是使用 JavaScript 编写的。Stored procedures are written using JavaScript. 它们可以在 Azure Cosmos 容器中创建、更新、读取、查询和删除项。They can create, update, read, query, and delete items within an Azure Cosmos container. 有关如何在 Azure Cosmos DB 中编写存储过程的详细信息,请参阅如何在 Azure Cosmos DB 中编写存储过程一文。For more information on how to write stored procedures in Azure Cosmos DB, see How to write stored procedures in Azure Cosmos DB article.

以下示例演示如何使用 Azure Cosmos DB SDK 注册和调用存储过程。The following examples show how to register and call a stored procedure by using the Azure Cosmos DB SDKs. 请参阅创建文档,因为此存储过程的源代码保存为 spCreateToDoItem.jsRefer to Create a Document as the source for this stored procedure is saved as spCreateToDoItem.js.

备注

对于已分区的容器,在执行存储过程时,必须在请求选项中提供分区键值。For partitioned containers, when executing a stored procedure, a partition key value must be provided in the request options. 存储过程的范围始终限定为分区键。Stored procedures are always scoped to a partition key. 存储过程看不到具有不同分区键值的项。Items that have a different partition key value will not be visible to the stored procedure. 这一点也适用于触发器。This also applied to triggers as well.

存储过程 - .NET SDK V2Stored procedures - .NET SDK V2

以下示例演示如何使用 .NET SDK V2 注册存储过程:The following example shows how to register a stored procedure by using the .NET SDK V2:

string storedProcedureId = "spCreateToDoItems";
StoredProcedure newStoredProcedure = new StoredProcedure
   {
       Id = storedProcedureId,
       Body = File.ReadAllText($@"..\js\{storedProcedureId}.js")
   };
Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
var response = await client.CreateStoredProcedureAsync(containerUri, newStoredProcedure);
StoredProcedure createdStoredProcedure = response.Resource;

以下代码演示如何使用 .NET SDK V2 调用存储过程:The following code shows how to call a stored procedure by using the .NET SDK V2:

dynamic[] newItems = new dynamic[]
{
    new {
        category = "Personal",
        name = "Groceries",
        description = "Pick up strawberries",
        isComplete = false
    },
    new {
        category = "Personal",
        name = "Doctor",
        description = "Make appointment for check up",
        isComplete = false
    }
};

Uri uri = UriFactory.CreateStoredProcedureUri("myDatabase", "myContainer", "spCreateToDoItem");
RequestOptions options = new RequestOptions { PartitionKey = new PartitionKey("Personal") };
var result = await client.ExecuteStoredProcedureAsync<string>(uri, options, new[] { newItems });

存储过程 - .NET SDK V3Stored procedures - .NET SDK V3

以下示例演示如何使用 .NET SDK V3 注册存储过程:The following example shows how to register a stored procedure by using the .NET SDK V3:

string storedProcedureId = "spCreateToDoItems";
StoredProcedureResponse storedProcedureResponse = await client.GetContainer("myDatabase", "myContainer").Scripts.CreateStoredProcedureAsync(new StoredProcedureProperties
{
    Id = storedProcedureId,
    Body = File.ReadAllText($@"..\js\{storedProcedureId}.js")
});

以下代码演示如何使用 .NET SDK V3 调用存储过程:The following code shows how to call a stored procedure by using the .NET SDK V3:

dynamic[] newItems = new dynamic[]
{
    new {
        category = "Personal",
        name = "Groceries",
        description = "Pick up strawberries",
        isComplete = false
    },
    new {
        category = "Personal",
        name = "Doctor",
        description = "Make appointment for check up",
        isComplete = false
    }
};

var result = await client.GetContainer("database", "container").Scripts.ExecuteStoredProcedureAsync<string>("spCreateToDoItem", new PartitionKey("Personal"), new[] { newItems });

存储过程 - Java SDKStored procedures - Java SDK

以下示例演示如何使用 Java SDK 注册存储过程:The following example shows how to register a stored procedure by using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
StoredProcedure newStoredProcedure = new StoredProcedure(
    "{" +
        "  'id':'spCreateToDoItems'," +
        "  'body':" + new String(Files.readAllBytes(Paths.get("..\\js\\spCreateToDoItems.js"))) +
    "}");
//toBlocking() blocks the thread until the operation is complete and is used only for demo.  
StoredProcedure createdStoredProcedure = asyncClient.createStoredProcedure(containerLink, newStoredProcedure, null)
    .toBlocking().single().getResource();

以下代码演示如何使用 Java SDK 调用存储过程:The following code shows how to call a stored procedure by using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
String sprocLink = String.format("%s/sprocs/%s", containerLink, "spCreateToDoItems");
final CountDownLatch successfulCompletionLatch = new CountDownLatch(1);

List<ToDoItem> ToDoItems = new ArrayList<ToDoItem>();

class ToDoItem {
    public String category;
    public String name;
    public String description;
    public boolean isComplete;
}

ToDoItem newItem = new ToDoItem();
newItem.category = "Personal";
newItem.name = "Groceries";
newItem.description = "Pick up strawberries";
newItem.isComplete = false;

ToDoItems.add(newItem)

newItem.category = "Personal";
newItem.name = "Doctor";
newItem.description = "Make appointment for check up";
newItem.isComplete = false;

ToDoItems.add(newItem)

RequestOptions requestOptions = new RequestOptions();
requestOptions.setPartitionKey(new PartitionKey("Personal"));

Object[] storedProcedureArgs = new Object[] { ToDoItems };
asyncClient.executeStoredProcedure(sprocLink, requestOptions, storedProcedureArgs)
    .subscribe(storedProcedureResponse -> {
        String storedProcResultAsString = storedProcedureResponse.getResponseAsString();
        successfulCompletionLatch.countDown();
        System.out.println(storedProcedureResponse.getActivityId());
    }, error -> {
        successfulCompletionLatch.countDown();
        System.err.println("an error occurred while executing the stored procedure: actual cause: "
                + error.getMessage());
    });

successfulCompletionLatch.await();

存储过程 - JavaScript SDKStored procedures - JavaScript SDK

以下示例演示如何使用 JavaScript SDK 注册存储过程The following example shows how to register a stored procedure by using the JavaScript SDK

const container = client.database("myDatabase").container("myContainer");
const sprocId = "spCreateToDoItems";
await container.scripts.storedProcedures.create({
    id: sprocId,
    body: require(`../js/${sprocId}`)
});

以下代码演示如何使用 JavaScript SDK 调用存储过程:The following code shows how to call a stored procedure by using the JavaScript SDK:

const newItem = [{
    category: "Personal",
    name: "Groceries",
    description: "Pick up strawberries",
    isComplete: false
}];
const container = client.database("myDatabase").container("myContainer");
const sprocId = "spCreateToDoItems";
const {body: result} = await container.scripts.storedProcedure(sprocId).execute(newItem, {partitionKey: newItem[0].category});

存储过程 - Python SDKStored procedures - Python SDK

以下示例演示如何使用 Python SDK 注册存储过程:The following example shows how to register a stored procedure by using the Python SDK:

import azure.cosmos.cosmos_client as cosmos_client

url = "your_cosmos_db_account_URI"
key = "your_cosmos_db_account_key"
database_name = 'your_cosmos_db_database_name'
container_name = 'your_cosmos_db_container_name'

with open('../js/spCreateToDoItems.js') as file:
    file_contents = file.read()

sproc = {
    'id': 'spCreateToDoItem',
    'serverScript': file_contents,
}
client = cosmos_client.CosmosClient(url, key)
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
created_sproc = container.scripts.create_stored_procedure(body=sproc) 

以下代码演示如何使用 Python SDK 调用存储过程:The following code shows how to call a stored procedure by using the Python SDK:

import uuid

new_id= str(uuid.uuid4())

# Creating a document for a container with "id" as a partition key.

new_item =   {
      "id": new_id, 
      "category":"Personal",
      "name":"Groceries",
      "description":"Pick up strawberries",
      "isComplete":False
   }
result = container.scripts.execute_stored_procedure(sproc=created_sproc,params=[[new_item]], partition_key=new_id) 

如何运行前触发器How to run pre-triggers

以下示例演示如何使用 Azure Cosmos DB SDK 注册和调用前触发器。The following examples show how to register and call a pre-trigger by using the Azure Cosmos DB SDKs. 请参阅前触发器示例,因为此前触发器的源代码保存为 trgPreValidateToDoItemTimestamp.jsRefer to the Pre-trigger example as the source for this pre-trigger is saved as trgPreValidateToDoItemTimestamp.js.

执行前触发器时,将会通过指定 PreTriggerInclude 在该触发器中传入 RequestOptions 对象,然后在 List 对象中传递该触发器的名称。When executing, pre-triggers are passed in the RequestOptions object by specifying PreTriggerInclude and then passing the name of the trigger in a List object.

备注

即使以列表的形式传递触发器的名称,也仍只能对每个操作执行一个触发器。Even though the name of the trigger is passed as a List, you can still execute only one trigger per operation.

前触发器 - .NET SDK V2Pre-triggers - .NET SDK V2

以下代码演示如何使用 .NET SDK V2 注册前触发器:The following code shows how to register a pre-trigger using the .NET SDK V2:

string triggerId = "trgPreValidateToDoItemTimestamp";
Trigger trigger = new Trigger
{
    Id =  triggerId,
    Body = File.ReadAllText($@"..\js\{triggerId}.js"),
    TriggerOperation = TriggerOperation.Create,
    TriggerType = TriggerType.Pre
};
Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
await client.CreateTriggerAsync(containerUri, trigger);

以下代码演示如何使用 .NET SDK V2 调用前触发器:The following code shows how to call a pre-trigger using the .NET SDK V2:

dynamic newItem = new
{
    category = "Personal",
    name = "Groceries",
    description = "Pick up strawberries",
    isComplete = false
};

Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
RequestOptions requestOptions = new RequestOptions { PreTriggerInclude = new List<string> { "trgPreValidateToDoItemTimestamp" } };
await client.CreateDocumentAsync(containerUri, newItem, requestOptions);

前触发器 - .NET SDK V3Pre-triggers - .NET SDK V3

以下代码演示如何使用 .NET SDK V3 注册前触发器:The following code shows how to register a pre-trigger using the .NET SDK V3:

await client.GetContainer("database", "container").Scripts.CreateTriggerAsync(new TriggerProperties
{
    Id = "trgPreValidateToDoItemTimestamp",
    Body = File.ReadAllText("@..\js\trgPreValidateToDoItemTimestamp.js"),
    TriggerOperation = TriggerOperation.Create,
    TriggerType = TriggerType.Pre
});

以下代码演示如何使用 .NET SDK V3 调用前触发器:The following code shows how to call a pre-trigger using the .NET SDK V3:

dynamic newItem = new
{
    category = "Personal",
    name = "Groceries",
    description = "Pick up strawberries",
    isComplete = false
};

await client.GetContainer("database", "container").CreateItemAsync(newItem, null, new ItemRequestOptions { PreTriggers = new List<string> { "trgPreValidateToDoItemTimestamp" } });

前触发器 - Java SDKPre-triggers - Java SDK

以下代码演示如何使用 Java SDK 注册前触发器:The following code shows how to register a pre-trigger using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
String triggerId = "trgPreValidateToDoItemTimestamp";
Trigger trigger = new Trigger();
trigger.setId(triggerId);
trigger.setBody(new String(Files.readAllBytes(Paths.get(String.format("..\\js\\%s.js", triggerId)));
trigger.setTriggerOperation(TriggerOperation.Create);
trigger.setTriggerType(TriggerType.Pre);
//toBlocking() blocks the thread until the operation is complete and is used only for demo. 
Trigger createdTrigger = asyncClient.createTrigger(containerLink, trigger, new RequestOptions()).toBlocking().single().getResource();

以下代码演示如何使用 Java SDK 调用前触发器:The following code shows how to call a pre-trigger using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
    Document item = new Document("{ "
            + "\"category\": \"Personal\", "
            + "\"name\": \"Groceries\", "
            + "\"description\": \"Pick up strawberries\", "
            + "\"isComplete\": false, "
            + "}"
            );
RequestOptions requestOptions = new RequestOptions();
requestOptions.setPreTriggerInclude(Arrays.asList("trgPreValidateToDoItemTimestamp"));
//toBlocking() blocks the thread until the operation is complete and is used only for demo. 
asyncClient.createDocument(containerLink, item, requestOptions, false).toBlocking();

前触发器 - JavaScript SDKPre-triggers - JavaScript SDK

以下代码演示如何使用 JavaScript SDK 注册前触发器:The following code shows how to register a pre-trigger using the JavaScript SDK:

const container = client.database("myDatabase").container("myContainer");
const triggerId = "trgPreValidateToDoItemTimestamp";
await container.triggers.create({
    id: triggerId,
    body: require(`../js/${triggerId}`),
    triggerOperation: "create",
    triggerType: "pre"
});

以下代码演示如何使用 JavaScript SDK 调用前触发器:The following code shows how to call a pre-trigger using the JavaScript SDK:

const container = client.database("myDatabase").container("myContainer");
const triggerId = "trgPreValidateToDoItemTimestamp";
await container.items.create({
    category: "Personal",
    name = "Groceries",
    description = "Pick up strawberries",
    isComplete = false
}, {preTriggerInclude: [triggerId]});

前触发器 - Python SDKPre-triggers - Python SDK

以下代码演示如何使用 Python SDK 注册前触发器:The following code shows how to register a pre-trigger using the Python SDK:

import azure.cosmos.cosmos_client as cosmos_client

url = "your_cosmos_db_account_URI"
key = "your_cosmos_db_account_key"
database_name = 'your_cosmos_db_database_name'
container_name = 'your_cosmos_db_container_name'

with open('../js/trgPreValidateToDoItemTimestamp.js') as file:
    file_contents = file.read()

trigger_definition = {
    'id': 'trgPreValidateToDoItemTimestamp',
    'serverScript': file_contents,
    'triggerType': documents.TriggerType.Pre,
    'triggerOperation': documents.TriggerOperation.All
}
client = cosmos_client.CosmosClient(url, key)
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
trigger = container.scripts.create_trigger(trigger_definition)

以下代码演示如何使用 Python SDK 调用前触发器:The following code shows how to call a pre-trigger using the Python SDK:

item = {'category': 'Personal', 'name': 'Groceries',
        'description': 'Pick up strawberries', 'isComplete': False}
container.create_item(item, {'pre_trigger_include': 'trgPreValidateToDoItemTimestamp'})

如何运行后触发器How to run post-triggers

以下示例演示如何使用 Azure Cosmos DB SDK 注册后触发器。The following examples show how to register a post-trigger by using the Azure Cosmos DB SDKs. 请参阅后触发器示例,因为此后触发器的源代码保存为 trgPostUpdateMetadata.jsRefer to the Post-trigger example as the source for this post-trigger is saved as trgPostUpdateMetadata.js.

后触发器 - .NET SDK V2Post-triggers - .NET SDK V2

以下代码演示如何使用 .NET SDK V2 注册后触发器:The following code shows how to register a post-trigger using the .NET SDK V2:

string triggerId = "trgPostUpdateMetadata";
Trigger trigger = new Trigger
{
    Id = triggerId,
    Body = File.ReadAllText($@"..\js\{triggerId}.js"),
    TriggerOperation = TriggerOperation.Create,
    TriggerType = TriggerType.Post
};
Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
await client.CreateTriggerAsync(containerUri, trigger);

以下代码演示如何使用 .NET SDK V2 调用后触发器:The following code shows how to call a post-trigger using the .NET SDK V2:

var newItem = { 
    name: "artist_profile_1023",
    artist: "The Band",
    albums: ["Hellujah", "Rotators", "Spinning Top"]
};

RequestOptions options = new RequestOptions { PostTriggerInclude = new List<string> { "trgPostUpdateMetadata" } };
Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
await client.createDocumentAsync(containerUri, newItem, options);

后触发器 - .NET SDK V3Post-triggers - .NET SDK V3

以下代码演示如何使用 .NET SDK V3 注册后触发器:The following code shows how to register a post-trigger using the .NET SDK V3:

await client.GetContainer("database", "container").Scripts.CreateTriggerAsync(new TriggerProperties
{
    Id = "trgPostUpdateMetadata",
    Body = File.ReadAllText(@"..\js\trgPostUpdateMetadata.js"),
    TriggerOperation = TriggerOperation.Create,
    TriggerType = TriggerType.Post
});

以下代码演示如何使用 .NET SDK V3 调用后触发器:The following code shows how to call a post-trigger using the .NET SDK V3:

var newItem = { 
    name: "artist_profile_1023",
    artist: "The Band",
    albums: ["Hellujah", "Rotators", "Spinning Top"]
};

await client.GetContainer("database", "container").CreateItemAsync(newItem, null, new ItemRequestOptions { PostTriggers = new List<string> { "trgPostUpdateMetadata" } });

后触发器 - Java SDKPost-triggers - Java SDK

以下代码演示如何使用 Java SDK 注册后触发器:The following code shows how to register a post-trigger using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
String triggerId = "trgPostUpdateMetadata";
Trigger trigger = new Trigger();
trigger.setId(triggerId);
trigger.setBody(new String(Files.readAllBytes(Paths.get(String.format("..\\js\\%s.js", triggerId)))));
trigger.setTriggerOperation(TriggerOperation.Create);
trigger.setTriggerType(TriggerType.Post);
Trigger createdTrigger = asyncClient.createTrigger(containerLink, trigger, new RequestOptions()).toBlocking().single().getResource();

以下代码演示如何使用 Java SDK 调用后触发器:The following code shows how to call a post-trigger using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
Document item = new Document(String.format("{ "
    + "\"name\": \"artist_profile_1023\", "
    + "\"artist\": \"The Band\", "
    + "\"albums\": [\"Hellujah\", \"Rotators\", \"Spinning Top\"]"
    + "}"
));
RequestOptions requestOptions = new RequestOptions();
requestOptions.setPostTriggerInclude(Arrays.asList("trgPostUpdateMetadata"));
//toBlocking() blocks the thread until the operation is complete, and is used only for demo.
asyncClient.createDocument(containerLink, item, requestOptions, false).toBlocking();

后触发器 - JavaScript SDKPost-triggers - JavaScript SDK

以下代码演示如何使用 JavaScript SDK 注册后触发器:The following code shows how to register a post-trigger using the JavaScript SDK:

const container = client.database("myDatabase").container("myContainer");
const triggerId = "trgPostUpdateMetadata";
await container.triggers.create({
    id: triggerId,
    body: require(`../js/${triggerId}`),
    triggerOperation: "create",
    triggerType: "post"
});

以下代码演示如何使用 JavaScript SDK 调用后触发器:The following code shows how to call a post-trigger using the JavaScript SDK:

const item = {
    name: "artist_profile_1023",
    artist: "The Band",
    albums: ["Hellujah", "Rotators", "Spinning Top"]
};
const container = client.database("myDatabase").container("myContainer");
const triggerId = "trgPostUpdateMetadata";
await container.items.create(item, {postTriggerInclude: [triggerId]});

后触发器 - Python SDKPost-triggers - Python SDK

以下代码演示如何使用 Python SDK 注册后触发器:The following code shows how to register a post-trigger using the Python SDK:

import azure.cosmos.cosmos_client as cosmos_client

url = "your_cosmos_db_account_URI"
key = "your_cosmos_db_account_key"
database_name = 'your_cosmos_db_database_name'
container_name = 'your_cosmos_db_container_name'

with open('../js/trgPostValidateToDoItemTimestamp.js') as file:
    file_contents = file.read()

trigger_definition = {
    'id': 'trgPostValidateToDoItemTimestamp',
    'serverScript': file_contents,
    'triggerType': documents.TriggerType.Post,
    'triggerOperation': documents.TriggerOperation.All
}
client = cosmos_client.CosmosClient(url, key)
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
trigger = container.scripts.create_trigger(trigger_definition)

以下代码演示如何使用 Python SDK 调用后触发器:The following code shows how to call a post-trigger using the Python SDK:

item = {'category': 'Personal', 'name': 'Groceries',
        'description': 'Pick up strawberries', 'isComplete': False}
container.create_item(item, {'post_trigger_include': 'trgPreValidateToDoItemTimestamp'})

如何使用用户定义的函数How to work with user-defined functions

以下示例演示如何使用 Azure Cosmos DB SDK 注册用户定义的函数。The following examples show how to register a user-defined function by using the Azure Cosmos DB SDKs. 请参阅用户定义的函数示例,因为此后触发器的源代码保存为 udfTax.jsRefer to this User-defined function example as the source for this post-trigger is saved as udfTax.js.

用户定义的函数 - .NET SDK V2User-defined functions - .NET SDK V2

以下代码演示如何使用 .NET SDK V2 注册用户定义的函数:The following code shows how to register a user-defined function using the .NET SDK V2:

string udfId = "Tax";
var udfTax = new UserDefinedFunction
{
    Id = udfId,
    Body = File.ReadAllText($@"..\js\{udfId}.js")
};

Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
await client.CreateUserDefinedFunctionAsync(containerUri, udfTax);

以下代码演示如何使用 .NET SDK V2 调用用户定义的函数:The following code shows how to call a user-defined function using the .NET SDK V2:

Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
var results = client.CreateDocumentQuery<dynamic>(containerUri, "SELECT * FROM Incomes t WHERE udf.Tax(t.income) > 20000"));

foreach (var result in results)
{
    //iterate over results
}

用户定义的函数 - .NET SDK V3User-defined functions - .NET SDK V3

以下代码演示如何使用 .NET SDK V3 注册用户定义的函数:The following code shows how to register a user-defined function using the .NET SDK V3:

await client.GetContainer("database", "container").Scripts.CreateUserDefinedFunctionAsync(new UserDefinedFunctionProperties
{
    Id = "Tax",
    Body = File.ReadAllText(@"..\js\Tax.js")
});

以下代码演示如何使用 .NET SDK V3 调用用户定义的函数:The following code shows how to call a user-defined function using the .NET SDK V3:

var iterator = client.GetContainer("database", "container").GetItemQueryIterator<dynamic>("SELECT * FROM Incomes t WHERE udf.Tax(t.income) > 20000");
while (iterator.HasMoreResults)
{
    var results = await iterator.ReadNextAsync();
    foreach (var result in results)
    {
        //iterate over results
    }
}

用户定义的函数 - Java SDKUser-defined functions - Java SDK

以下代码演示如何使用 Java SDK 注册用户定义的函数:The following code shows how to register a user-defined function using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
String udfId = "Tax";
UserDefinedFunction udf = new UserDefinedFunction();
udf.setId(udfId);
udf.setBody(new String(Files.readAllBytes(Paths.get(String.format("..\\js\\%s.js", udfId)))));
//toBlocking() blocks the thread until the operation is complete and is used only for demo.
UserDefinedFunction createdUDF = client.createUserDefinedFunction(containerLink, udf, new RequestOptions()).toBlocking().single().getResource();

以下代码演示如何使用 Java SDK 调用用户定义的函数:The following code shows how to call a user-defined function using the Java SDK:

String containerLink = String.format("/dbs/%s/colls/%s", "myDatabase", "myContainer");
Observable<FeedResponse<Document>> queryObservable = client.queryDocuments(containerLink, "SELECT * FROM Incomes t WHERE udf.Tax(t.income) > 20000", new FeedOptions());
final CountDownLatch completionLatch = new CountDownLatch(1);
queryObservable.subscribe(
        queryResultPage -> {
            System.out.println("Got a page of query result with " +
                    queryResultPage.getResults().size());
        },
        // terminal error signal
        e -> {
            e.printStackTrace();
            completionLatch.countDown();
        },

        // terminal completion signal
        () -> {
            completionLatch.countDown();
        });
completionLatch.await();

用户定义的函数 - JavaScript SDKUser-defined functions - JavaScript SDK

以下代码演示如何使用 JavaScript SDK 注册用户定义的函数:The following code shows how to register a user-defined function using the JavaScript SDK:

const container = client.database("myDatabase").container("myContainer");
const udfId = "Tax";
await container.userDefinedFunctions.create({
    id: udfId,
    body: require(`../js/${udfId}`)

以下代码演示如何使用 JavaScript SDK 调用用户定义的函数:The following code shows how to call a user-defined function using the JavaScript SDK:

const container = client.database("myDatabase").container("myContainer");
const sql = "SELECT * FROM Incomes t WHERE udf.Tax(t.income) > 20000";
const {result} = await container.items.query(sql).toArray();

用户定义的函数 - Python SDKUser-defined functions - Python SDK

以下代码演示如何使用 Python SDK 注册用户定义的函数:The following code shows how to register a user-defined function using the Python SDK:

import azure.cosmos.cosmos_client as cosmos_client

url = "your_cosmos_db_account_URI"
key = "your_cosmos_db_account_key"
database_name = 'your_cosmos_db_database_name'
container_name = 'your_cosmos_db_container_name'

with open('../js/udfTax.js') as file:
    file_contents = file.read()
udf_definition = {
    'id': 'Tax',
    'serverScript': file_contents,
}
client = cosmos_client.CosmosClient(url, key)
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
udf = container.scripts.create_user_defined_function(udf_definition)

以下代码演示如何使用 Python SDK 调用用户定义的函数:The following code shows how to call a user-defined function using the Python SDK:

results = list(container.query_items(
    'query': 'SELECT * FROM Incomes t WHERE udf.Tax(t.income) > 20000'))

后续步骤Next steps

详细了解概念以及如何在 Azure Cosmos DB 中编写或使用存储过程、触发器和用户定义的函数:Learn more concepts and how-to write or use stored procedures, triggers, and user-defined functions in Azure Cosmos DB: