首页 > 其他分享 >test

test

时间:2022-09-22 11:34:29浏览次数:34  
标签:writeModels bulkWriteOptions val import test new def

import com.mongodb.client.MongoCollection
import com.mongodb.client.model.BulkWriteOptions
import com.mongodb.client.model.InsertOneModel
import com.mongodb.client.model.WriteModel
import org.bson.Document
import groovy.json.*

def getCollection(String dataBaseName, String collectionName){
  def mongoService = Client_Service.asControllerService(MongoDBClientService.class)
  return mongoService.getDatabase(dataBaseName).getCollection(collectionName)
}


Main main = new Main()
def batchSize = Batch_Size.val
def bypass = Bypass_Validation.val
def collectionName = Mongo_Collection_Name.val
def databaseName = Mongo_DataBase_Name.val
def ordered = Ordered.val
def writeConcern = Write_Concern.val
int written = 0
def mongoCollection = getCollection(databaseName, collectionName)

BulkWriteOptions bulkWriteOptions = new BulkWriteOptions()
bulkWriteOptions.ordered(ordered)
bulkWriteOptions.bypassDocumentValidation(bypass)

def json = '[{"val":123456789.976543213456},{"val":987654321.1234567898765},{"val":1234567}]'
try{
  def jsonHandler = new JsonSlurper()
  List<Map<String, Object>> docs = jsonHandler.parseText(json)
  List<WriteModel<Document>> writeModels = new ArrayList<>()
  WriteModel<Document> writeModel
  docs.forEach(doc -> 
    {
      writeModel = new InsertOneModel<>(new Document(doc))
      writeModels.add(writeModel)
      if (writeModels.size() == batchSize) 
      {
        mongoCollection.bulkWrite(writeModels, bulkWriteOptions)
        written += batchSize
        writeModels = new ArrayList<>()
      }
    }
  )
  if (writeModels.size() > 0) 
  {
    written += writeModels.size()
    mongoCollection.bulkWrite(writeModels, bulkWriteOptions)
  }
}catch(Exception e) {
  getLogger().error("PutMongoRecord failed with error:", e);
  session.transfer(flowFile, REL_FAILURE);
  error = true;
}finally {
  if (!error)
  {
    session.transfer(flowFile, REL_SUCCESS);
    getLogger().info("Written {} records into MongoDB", new Object[]{ written });
  }
}

标签:writeModels,bulkWriteOptions,val,import,test,new,def
From: https://www.cnblogs.com/INnoVationv2/p/16718608.html

相关文章

  • Test 2022.09.21
    今天是\(JSOI2010\)专场T1满汉全席(2-SAT)比较生疏的知识点,当时看着一眼就觉得是图论,甚至想到了最大流,但是因为建不来图,被迫去打了暴力,然而只得到\(10pts\),事实证明我的想......
  • 【C++】GoogleTest入门指南
    参考:GoogleTest官网基本概念要使用GoogleTest,需要包含headergtest/gtest.h断言Assertions断言是检查条件是否为真的语句,其结果可能是成功或失败,失败分为非致命失败和......
  • 运行 docker run hello-world 报错 Unable to find image ‘hello-world:latest‘ loc
    原文链接:https://blog.csdn.net/weixin_43520450/article/details/107377342报错提示如下:解决办法:1、执行以下命令vi/etc/docker/daemon.json2、添加以下的内容并保......
  • 走进Java接口测试之测试框架TestNG数据驱动(入门篇)
     https://mp.weixin.qq.com/s?__biz=MzIwNDY3MDg1OA==&mid=2247485789&idx=1&sn=fb6c0abd38419747e61d9b14dee10466&chksm=973ddbb8a04a52aeee5869d47b1d0ea01467f2c3bdb9......
  • python学习笔记:pytest单元测试框架
    一、安装配置和运行规则1、安装:pipinstallpytest查看安装版本:pytest--version 2、Pytest用例运行规则用Pytest写用例时候,一定要按照下面的规则去写,否则不符合规......
  • Pytest+allure+requests接口自动化
    实现功能测试数据隔离:测试前后进行数据库备份/还原接口直接的数据依赖:需要B接口使用A接口响应中的某个字段作为参数对接数据库:讲数据库的查询结果可直接用于断言......
  • Testing in the Cloud:使用TPT进行云端测试
    前言  最近,我们经常听说解决方案是去云端。但是为什么呢? 我们注意到,云技术现在在科技领域非常流行。即便在嵌入式开发领域,也有越来越多的人希望将开发转移到云中......
  • Test 2022.09.20
    2022年9月20日的测试(SCOI2005专场)T1扫雷思考起来很简单,对于任意一个输入的\(a[i]\),它会约束的格子只有\(i-1,i,i+1\)三个,也就是只要算出当前在\(i-1,i\)位置摆放的情......
  • Codeforces Round #819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022
    Preface明天的CF一定打啊(绝对不咕),今天白天补现代作业,英语作业到哭,而且还要准备四六级,每天逼着自己背单词A.MainakandArray不难发现换中间的数不会影响答案,因此操作......
  • 在控制台使用npm init vue@latest命令报错npm ERR! Error: EPERM: operation not perm
    问题描述:    在控制台中输入npminitvue@latest命令创建vue3项目,按照流程一步一步走,   结果发现报错npmERR!Error:EPERM:operationnotpermitted,......