Apache Camel 访问MongoDB表记录的groovy脚本

        本例用到了java的java.text.SimpleDateFormat包对时间进行格式化,calendar.set()设定一个早于当前的时间点或者晚于当前的时间点。calendar的java用法可以百度或者GPT查到。

        需要用到的MongoDB依赖代码里面已经import了。将自己的MongoDB服务器信息对应填入这段def template = new MongoTemplate(new MongoClient(new ServerAddress("你的MongoDB服务器IP",port),MongoCredential.createCredential("操作系统用户名","数据库用户名","数据库密码".toCharArray()), MongoClientOptions.builder().build()),"数据库实例名"),以下示例代码段。

package com.kingdeehit.cloud.integration;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.kingdeehit.cloud.integration.service.IGroovyPlugin
import com.kingdeehit.cloud.integration.streaming.Logevent;
import com.kingdeehit.cloud.integration.utils.SpringUtils
import com.mongodb.MongoClient
import com.mongodb.MongoClientOptions
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress;
import org.apache.camel.Exchange
import org.bson.Document
import org.springframework.data.mongodb.MongoDbFactory
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Map;
import java.time.LocalDateTime
import java.util.Date
import java.text.SimpleDateFormat


public class Groovy extends IGroovyPlugin {

    @Override
    public void process(Exchange exchange) {
        Calendar calendar = Calendar.getInstance();
        //calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);/* HOUR_OF_DAY 指示一天中的小时 */
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 3)/* 当前时间的前3分钟 */
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String current = df.format(System.currentTimeMillis());//当前时间 年月日 时分秒
        String front = df.format(calendar.getTime());//一个小时前的时间 年月日 时分秒
        //front = "2024-04-01";//指定一个具体的时间点 年月日 时分秒
        def template = new MongoTemplate(new MongoClient(new ServerAddress("192.168.xxx.xxx",27679),MongoCredential.createCredential("root","admin","a*********19".toCharArray()), MongoClientOptions.builder().build()),"xxx")
        Query query = new Query(Criteria.where("creatTime").gte(front));//creatTime是作为条件的字段名
        def find = template.find(query, Object.class, "message_log");//message_log要查询的表名
        //exchange.getIn().setHeader("dt",front);
       //exchange.getIn().setHeader("dt1",current);
        exchange.getIn().setBody(find);
    }
}

        上面代码如果front = "2024-04-01",则相当于在数据库查询工具中执行查询语句db.getCollection("message_log").find({"creatTime" : { $gte : "2024-04-01" }}) 得出的结果按照json格式输出如下。本文用的是navicat数据库查询工具。

// 1 第1条记录
{
    "_id": ObjectId("660cce6433a19b0eac5959e1"),
    "messageName": "互联网xxxxx记录",
    "creatTime": "2024-04-03 11:35:00",
    "requestParam": "[{\"age\":20,\"appointDate\":1712115224000,\"appointStatus\":1,\"appointUserMobile\":\"19729122562\",\"appointUserName\":\"李xx\",\"bussID\":\"ZSP0100543202404031360\",\"deptID\":\"0269\",\"deptName\":\"xx科门诊\",\"doctorCertID\":\"230281198204151635\",\"doctorId\":\"313\",\"doctorName\":\"王xx\",\"endTime\":1712115300021,\"medicalFlag\":1,\"mobile\":\"19729122562\",\"organId\":\"43836097562050211A201\",\"organName\":\"xxx机构\",\"patientCertID\":\"620502200311200424\",\"patientCertType\":\"01\",\"patientName\":\"李xx\",\"price\":11.0,\"sex\":2,\"startTime\":1712115224000,\"subjectCode\":\"04.03\",\"subjectName\":\"xx专业\",\"unitId\":\"0EE6568D-3A47-434A-AD54-42D0884DE958\",\"updateTime\":1712115300022,\"workDate\":1712073600000}]",
    "responseParam": "{\"extend\":{},\"msg\":\"service not found!\",\"msgCode\":\"error\",\"success\":false}",
    "isSuccess": false,
    "messageId": "1712115300075",
    "_class": "xxx.cloud.xxx.common.MessageLog"
}

// 2 第2条记录
{
    "_id": ObjectId("660d5c3033a19b0eac5959eb"),
    "messageName": "互联网xxxxx记录",
    "creatTime": "2024-04-03 21:40:00",
    "requestParam": "[{\"age\":33,\"appointDate\":1712151548000,\"appointStatus\":1,\"appointUserMobile\":\"18309486667\",\"appointUserName\":\"李x\",\"bussID\":\"ZSP0100543202404033618\",\"deptID\":\"0269\",\"deptName\":\"xxx二科门诊\",\"doctorCertID\":\"620502196810280060\",\"doctorId\":\"089\",\"doctorName\":\"李x\",\"endTime\":1712151600017,\"medicalFlag\":1,\"mobile\":\"18309486667\",\"organId\":\"43836097562050211A201\",\"organName\":\"xxx机构\",\"patientCertID\":\"620503199011235714\",\"patientCertType\":\"01\",\"patientName\":\"李x\",\"price\":11.0,\"sex\":1,\"startTime\":1712151548000,\"subjectCode\":\"04.03\",\"subjectName\":\"xx专业\",\"unitId\":\"0EE6568D-3A47-434A-AD54-42D0884DE958\",\"updateTime\":1712151600019,\"workDate\":1712073600000}]",
    "responseParam": "{\"extend\":{},\"msg\":\"service not found!\",\"msgCode\":\"error\",\"success\":false}",
    "isSuccess": false,
    "messageId": "1712151600066",
    "_class": "xxx.cloud.xxx.common.MessageLog"
}

// 3 第3条记录
{
    "_id": ObjectId("660e211033a19b0eac5959ef"),
    "messageName": "互联网xxxxx记录",
    "creatTime": "2024-04-04 11:40:00",
    "requestParam": "[{\"age\":20,\"appointDate\":1712115224000,\"appointStatus\":1,\"appointUserMobile\":\"19729122562\",\"appointUserName\":\"李xx\",\"bussID\":\"ZSP0100543202404031360\",\"deptID\":\"0269\",\"deptName\":\"xx科门诊\",\"doctorCertID\":\"230281198204151635\",\"doctorId\":\"313\",\"doctorName\":\"王xx\",\"endTime\":1712202000009,\"medicalFlag\":1,\"mobile\":\"19729122562\",\"organId\":\"43836097562050211A201\",\"organName\":\"xxx机构\",\"patientCertID\":\"620502200311200424\",\"patientCertType\":\"01\",\"patientName\":\"李xx\",\"price\":11.0,\"sex\":2,\"startTime\":1712115224000,\"subjectCode\":\"04.03\",\"subjectName\":\"xx专业\",\"unitId\":\"0EE6568D-3A47-434A-AD54-42D0884DE958\",\"updateTime\":1712202000011,\"workDate\":1712073600000}]",
    "responseParam": "{\"extend\":{},\"msg\":\"service not found!\",\"msgCode\":\"error\",\"success\":false}",
    "isSuccess": false,
    "messageId": "1712202000101",
    "_class": "xxx.cloud.xxx.common.MessageLog"
}

// 4 第4条记录
{
    "_id": ObjectId("660fe65833a19b0eac5959fb"),
    "messageName": "在线xx信息",
    "creatTime": "2024-04-05 19:54:00",
    "requestParam": "[{\"age\":34,\"answerFlag\":\"1\",\"applyDate\":1712151548000,\"bussID\":\"ZSP0100543202404033618\",\"consultationPrice\":11.0,\"consultationType\":\"1\",\"content\":\"大夫你好,李x,34岁,我去年6月份在本医院看过高血压,现在现在吃其他血压药降不下来,在本医院开的药血压能降下来,能帮我查一下开的什么药吗\",\"deptID\":\"0269\",\"deptName\":\"xxx二科门诊\",\"doctorCertID\":\"620502196810280060\",\"doctorId\":\"089\",\"doctorName\":\"李x\",\"endDate\":1712318075000,\"mobile\":\"18309486667\",\"organID\":\"43836097562050211A201\",\"organName\":\"xxx机构\",\"patientCertID\":\"620503199011235714\",\"patientCertType\":\"1\",\"patientName\":\"李x\",\"paymentChannel\":\"2\",\"processDataURL\":\"https://www.mhealth100.com/ip-healthmanager-manager-web/manager-free-login/order_detail?orderId=182B2B416DC54E2524B9D1D64ACD678AF1EE8F79FEF2A5D5F869E14650A0F692\",\"refuseTime\":1712318075000,\"refuseType\":\"2\",\"sex\":\"1\",\"startDate\":1712231431000,\"subjectCode\":\"04.03\",\"subjectName\":\"xx专业\",\"unitID\":\"0EE6568D-3A47-434A-AD54-42D0884DE958\",\"updateTime\":1712318040008}]",
    "responseParam": "{\"extend\":{},\"msg\":\"success\",\"msgCode\":\"200\",\"success\":true}",
    "isSuccess": true,
    "messageId": "1712318040116",
    "_class": "xxx.cloud.xxx.common.MessageLog"
}

// 5 第5条记录
{
    "_id": ObjectId("66110de433a19b0eac595a07"),
    "messageName": "互联网xxxxx记录",
    "creatTime": "2024-04-06 16:55:00",
    "requestParam": "[{\"age\":33,\"appointDate\":1712393684000,\"appointStatus\":1,\"appointUserMobile\":\"18309486667\",\"appointUserName\":\"李x\",\"bussID\":\"ZSP0100543202404062190\",\"deptID\":\"0269\",\"deptName\":\"xxx二科门诊\",\"doctorCertID\":\"620502196810280060\",\"doctorId\":\"089\",\"doctorName\":\"李x\",\"endTime\":1712393700020,\"medicalFlag\":1,\"mobile\":\"18309486667\",\"organId\":\"43836097562050211A201\",\"organName\":\"xxx机构\",\"patientCertID\":\"620503199011235714\",\"patientCertType\":\"01\",\"patientName\":\"李x\",\"price\":11.0,\"sex\":1,\"startTime\":1712393684000,\"subjectCode\":\"04.03\",\"subjectName\":\"xx专业\",\"unitId\":\"0EE6568D-3A47-434A-AD54-42D0884DE958\",\"updateTime\":1712393700021,\"workDate\":1712332800000}]",
    "responseParam": "{\"extend\":{},\"msg\":\"service not found!\",\"msgCode\":\"error\",\"success\":false}",
    "isSuccess": false,
    "messageId": "1712393700070",
    "_class": "xxx.cloud.xxx.common.MessageLog"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值