从Java批量导入时覆盖Azure Cosmos DB中已存在的项目

水银01:

Java DocumentDb bulk-executor用来将json数组批量导入到中Azure Cosmos DB

样品JSON

[
  {
    "SId": "101",
    "SName": "ABC"
  },
  {
    "SId": "102",
    "SName": "XYZ"
  }
]

样例代码:

(PARTTION_KEY =“ \ SId”)

DocumentCollection collection = Utilities.createEmptyCollectionIfNotExists(client, DATABASE, CONTAINER, PARTITION_KEY, THROUGHPUT);
            ArrayList<String> list = new ArrayList<String>();
            JSONParser jsonParser = new JSONParser();
            FileReader reader = new FileReader("C:\\samplejson.json");



                Object obj = jsonParser.parse(reader);

                JSONArray jsonArray  = (JSONArray) obj;

                if (jsonArray  != null) {
                    int len = jsonArray.size();
                    for (int i=0;i<len;i++){
                        list.add(jsonArray.get(i).toString());
                    }
                }



                client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(30);
                client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(9);



                DocumentBulkExecutor.Builder bulkExecutorBuilder = DocumentBulkExecutor.builder().from(client, DATABASE, CONTAINER,
                collection.getPartitionKey(), 20000);

                DocumentBulkExecutor bulkExecutor = bulkExecutorBuilder.build();

                client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(0);
                client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(0);
                BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, false, false, null);
                System.out.println(bulkImportResponse.getNumberOfDocumentsImported());

现在,如果我有另一个JSON:

[
  {
    "SId": "101,         // Item with this SID has already been inserted
    "SName": "ABCDEF"
  },
  {
    "SId": "103",
    "SName": "PQR"
  }
]

我想将此JSON插入同一容器。但是它只是存储为一个新条目,并带有一个由Cosmos DB自动创建的不同“ id”。

如何根据“ SId”(如果已存在)同时批量导入和覆盖项目(如果已存在)?

请帮忙!

保罗:

您需要在对importAll的调用中将isUpsert标志更改为true。这将启用Upsert操作,这意味着如果该ID不存在,它将添加一个新文档,或者如果该ID已经存在,它将更新一个现有文档。

换线:

BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, false, false, null);

BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, true, false, null);

https://docs.microsoft.com/zh-CN/azure/cosmos-db/bulk-executor-java#bulk-import-data-to-azure-cosmos-db

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Azure Cosmos DB批量导入

批量插入Azure Cosmos DB

从Java代码将JSON文件批量上传/导入到Azure Cosmos DB

建模Azure Cosmos DB

Azure Cosmos DB 分区

Azure Cosmos DB查询

PYTHON-从Azure Cosmos DB中的集合中删除项目

抛出异常:“ Microsoft.Azure.Cosmos.CosmosException”,将批量导入JSON到Azure Cosmos DB时出现错误请求

在Azure Cosmos Db中没有分区键的批量删除

从.NET Core批量上传/导入JSON文件到Azure Cosmos DB

如何从 Databrick/PySpark 覆盖/更新 Azure Cosmos DB 中的集合

Azure Cosmos db Gremlin elementMap()

Azure Cosmos DB CONTAINS的语法

与Azure Cosmos DB脱机同步

打印 azure cosmos db 容器

Azure Cosmos DB 中嵌套字段的索引

批量删除cosmos DB中的所有项目:找不到404

Azure Cosmos Db文档-希望将replacedocumentasync与文档中已删除的属性一起使用

使用 Java 中的 Azure Cosmos DB 进行发布/订阅的示例

Azure Cosmos DB,删除IDS(肯定存在)

如何将JSON导入Cosmos DB

如何在来自 Azure Cosmos DB 的 JSON 项目的 SQL 查询中添加注释?

是否可以檢查 Azure Cosmos DB 中是否存在邏輯分區?

读取操作是否受益于Cosmos DB中的批量执行?

如何在Cosmos DB中执行大批量操作

尝试删除Cosmos DB Java SDK中不存在的文档时不要抛出DocumentClientException

Azure Cosmos DB-检查项目是否不存在,而不会向Application Insights抛出错误

使用遍历查询Azure Cosmos DB图形

如何调试Azure Cosmos DB存储过程?