1.使用mongotemplate完成对mongodb的数据写入,更新和修改操作

package com.wutos.sync.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.wutos.sync.domain.dto.DataSyncDTO;
import com.wutos.sync.domain.entity.DataReceiveExceptionLog;
import com.wutos.sync.domain.entity.DataReceiveLog;
import com.wutos.sync.domain.entity.DataSyncLog;
import com.wutos.sync.domain.mapper.DataReceiveExceptionLogMapper;
import com.wutos.sync.enums.SyncOptType;
import com.wutos.sync.service.IDataReceiveService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * 数据同步服务端 Mongodb实现类
 * Author:hushasha
 */
@Service("dataReceiveMongoDBService")
@Transactional
public class DataReceiveMongoDBServiceImpl implements IDataReceiveService {

    Logger logger = LoggerFactory.getLogger(DataReceiveMongoDBServiceImpl.class);

    @Autowired
    private DataReceiveExceptionLogMapper dataSyncExceptionLogMapper;

    @Autowired
    MongoTemplate writeMongoTemplate;

    @Override
    public void receiveData(DataSyncDTO dataSyncDTO) {
        List syncList = dataSyncDTO.getDataSyncLogList();
        if (CollectionUtils.isEmpty(syncList)) {
            return;
        }
        //接收异常数据处理
        List dataReceiveExceptionLogList = Lists.newArrayList();
        //接收成功的案例存储

        for (DataSyncLog dataSyncLog : syncList) {
            String sql;
            // 解析sql,执行脚本
            if (SyncOptType.INSERT.getValue().equals(dataSyncLog.getOperateType())) {
                /**实现mogodb的数据插入部分逻辑实现
                 */
                sql=parseMogodbInsert(dataSyncLog);
            } else if (SyncOptType.UPDATE.getValue().equals(dataSyncLog.getOperateType())) {
                /**实现mogodb的数据更改部分逻辑实现
                 */
                sql=parseMogodbUpdate(dataSyncLog);
            } else if (SyncOptType.DELETE.getValue().equals(dataSyncLog.getOperateType())) {
                /**实现mogodb数据删除部分逻辑的实现
                 */
                sql=parseMogodbDelete(dataSyncLog);
            } else {
                continue;
            }
            // 执行mogodb的数据库的插入日志异常操作
            try {
                if(sql!=null){
                    logger.info(" Mogodb Data Synchronizatio sucessfully");
                }
            } catch (Exception ex) {
                DataReceiveExceptionLog exceptionLog = new DataReceiveExceptionLog();
                exceptionLog.setDataJson(dataSyncLog.getDataJson());
                exceptionLog.setDataSource(dataSyncLog.getDataSource());
                exceptionLog.setErrorMsg(ex.getMessage());
                exceptionLog.setOperateType(dataSyncLog.getOperateType());
                exceptionLog.setPkId(dataSyncLog.getPkId());
                exceptionLog.setPlatformId(dataSyncLog.getPlatformId());
                exceptionLog.setTableName(dataSyncLog.getTableName());
                exceptionLog.setCreatedOn(LocalDateTime.now());
                exceptionLog.setModifiedOn(exceptionLog.getCreatedOn());
                dataReceiveExceptionLogList.add(exceptionLog);
            }
            // mogodb插入成功执行的数据表 data_receive_log
            DataReceiveLog Datalog=new DataReceiveLog();
            Datalog.setDataJson(dataSyncLog.getDataJson());
            Datalog.setDataSource(dataSyncLog.getDataSource());
            Datalog.setOperateType(dataSyncLog.getOperateType());
            Datalog.setPkId(dataSyncLog.getPkId());
            Datalog.setPlatformId(dataSyncLog.getPlatformId());
            Datalog.setTableName(dataSyncLog.getTableName());

        }

        if (CollectionUtils.isNotEmpty(dataReceiveExceptionLogList)) {
            dataSyncExceptionLogMapper.insertList(dataReceiveExceptionLogList);
        }

    }

    /***
     * @author
     * @param dataSyncLog
     * @return
     */
    private String parseMogodbInsert(DataSyncLog dataSyncLog) {
        String temp = dataSyncLog.getDataJson();
        JSONObject valuelist = JSONObject.parseObject(temp);
        //将外部获取的对象从jsonobject转成mogodb的dbObject对象
        DBObject dbObject = new BasicDBObject(valuelist);
        // 获取从上级平台获取的数据库表名
        String table = dataSyncLog.getTableName();//获取插入的数据库表
        String mogodb_id = valuelist.getString("id");//获取主键id
        try {
            writeMongoTemplate.insert(dbObject, table);
        }catch (Exception e){
            e.printStackTrace();
        }
        return dataSyncLog.getDataJson();
    }

    /**
     * 进入mogodb的上级平台数据插入
     * @param dataSyncLog
     * @author
     */
    private String parseMogodbUpdate(DataSyncLog dataSyncLog) {
        /**更改操作调用的API
         * writeMongoTemplate.updateFirst(new Query(),update,table);
         */
        String temp = dataSyncLog.getDataJson();
        JSONObject valuelist = JSONObject.parseObject(temp);
        String table = dataSyncLog.getTableName();
        //获取mogodb的主键
        String mogodb_id =dataSyncLog.getPkId();
        Query query = new Query();
        //这里使用mogodb的对应的update的操作
        org.springframework.data.mongodb.core.query.Update update=new org.springframework.data.mongodb.core.query.Update();
        query.addCriteria(Criteria.where("_id").is(mogodb_id));
        if (table!=null) {
            Set sIterator =valuelist.keySet();
            for (String key : sIterator) {
//                    String key = sIterator.iterator().next();
                    String value = valuelist.getString(key);
                    update.set(key,value);
                }
            try {
                writeMongoTemplate.updateFirst(query, update, table);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return dataSyncLog.getDataJson();
    }

    /**
     * 进入mogodb的数据删除操作
     *
     * @author hushasha
     * * demo: db.t_diskstatus.remove({"_id":"24"})
     * * demo: db.t_cameraChannel.remove({"_id":"0"})
     */
    private String parseMogodbDelete(DataSyncLog dataSyncLog) {
        String temp = dataSyncLog.getDataJson();
        JSONObject valuelist = JSONObject.parseObject(temp);
        String table = dataSyncLog.getTableName();//进行删除操作的表名。
        String mogodb_id =dataSyncLog.getPkId();//获取主键id
        Query query = Query.query(Criteria.where("_id").is(mogodb_id));//查询相关的条件
        try {
            writeMongoTemplate.remove(query, table);
        }catch(Exception e){
            e.printStackTrace();
        }
        return dataSyncLog.getDataJson();
    }

}

 

你可能感兴趣的:(大数据内部核心原理)