SpringBoot2.x集成MongoDB实践

介绍

最近在做一个SNMP设备数据的采集与分析,由于SNMP的设备数据格式是不定的,可以说是一个格式化的非格式化数据,因为基本字段相同,但是真实数据存储就不定了,且数据字段更是很多,用普通的RDS根本无法满足业务要求,这肯定就会用到MongoDB了,存储以集合形式,数据以Document存储,非常适合我的业务场景。

安装MongoDB

这里采用最简单的方式安装,因为这不是我们主要讲解的东西,安装环境在Ubuntu下,配置好mongo源,执行:

$ sudo apt-get update
$ sudo apt-get install mongodb

运行MongoDB

$ sudo /etc/init.d/mongod start

为项目新建一个mongo用户和mongo数据库

$ sudo mongo
> use snmp
> db.createUser({user: "snmp", pwd: "snmp", roles: [ { role: "readWrite", db: "snmp" } ]})

创建成功后退出,使用认证模式启动,登录测试mongo

$ sudo mongod -f /etc/mongod.conf --auth
$ sudo mongo --host 192.168.100.176 --port 27017
> use snmp
> db.auth('snmp', 'snmp')
> db.createCollection('snmp')
> show collections

这样基本上就完成了数据库及用户的创建了,当然我们如果是使用桌面开发的话,推荐使用一个MongoDB GUI 工具:Robo 3T
Robo 3T

SpringBoot构建

开发环境

  • IntelliJ IDEA 2017.1.6 x64
  • JDK1.8+
  • Maven 3.3.9
  • SpringBoot 2.0.2

环境搭建

  1. 使用start.spring.io快速构建SpringBoot项目
    Spring Initializr
    MongoDB

  2. 完成后pom文件及application.yaml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sdnware.odl</groupId>
    <artifactId>sdnware-odl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sdnware-odl</name>
    <description>sdnware-OpendayLight Project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
spring:
    data:
      mongodb:
        uri: mongodb://snmp:snmp@192.168.100.176:27017/snmp

开发测试

  • 构建基础DAO层
package com.sdnware.odl.dao;

import com.sdnware.odl.model.SNMPEntity;

import java.util.Date;
import java.util.List;

public interface SNMPDao {
    void saveData(SNMPEntity entity, String collectionName);
    List<SNMPEntity> getAllData();
}
package com.sdnware.odl.model;

import lombok.Getter;
import lombok.Setter;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

@Document
public class SNMPEntity implements Serializable {

    @Getter @Setter @Id private ObjectId id;
    @Getter @Setter @Field("ifRecvTime") private LocalDateTime ifRecvTime;
    @Getter @Setter @Field("ifEntry") private List<org.bson.Document> ifEntry;
    @Getter @Setter @Field("ifNumber") private Integer ifNumber;
}
  • 实现DAO接口
package com.sdnware.odl.dao.impl;

import com.sdnware.odl.dao.SNMPDao;
import com.sdnware.odl.model.SNMPEntity;
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.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component("snmpDao")
public class SNMPDaoImpl implements SNMPDao {

    static Logger logger = LoggerFactory.getLogger(SNMPDaoImpl.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveData(SNMPEntity entity, String collectionName) {
        mongoTemplate.save(entity, collectionName);
        logger.info("save snmp data in {} succeed.", collectionName);
    }

    @Override
    public List<SNMPEntity> getAllData(String collectionName) {
        return mongoTemplate.findAll(SNMPEntity.class, collectionName);
    }
}
  • 测试
package com.sdnware.odl;

import com.sdnware.odl.dao.SNMPDao;
import com.sdnware.odl.model.SNMPEntity;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SdnwareOdlApplicationTests {
    @Autowired
    SNMPDao snmpDao;
    @Test
    public void saveSNMPDataTests(){
        List<Document> entrys = new ArrayList<>();
        Document entry = new Document();
        entry.put("ifIndex", 1);
        entry.put("ifInOctets", 586888423);
        entry.put("ifOutOctets", 1054861105);
        entry.put("ifInUcastPkts", 24821672);
        entry.put("ifOutUcastPkts", 15946353);
        entry.put("ifSpeed", 1000000000);
        entry.put("ifMtu", 1514);
        entrys.add(entry);

        SNMPEntity entity = new SNMPEntity();
        entity.setId(ObjectId.get());
        entity.setIfEntry(entrys);
        entity.setIfRecvTime(LocalDateTime.now().plusHours(8));
        entity.setIfNumber(49);
        snmpDao.saveData(entity, "20180619");
    }

    @Test
    public void getAllDataTests() {
        List<SNMPEntity> entitys = snmpDao.getAllData("20180619");
        for (SNMPEntity entity : entitys) {
            LocalDateTime ifRecvTime = entity.getIfRecvTime();
            System.out.println(ifRecvTime + ": ");
            List<Document> ifEntrys = entity.getIfEntry();
            for (Document ifEntry : ifEntrys) {
                Integer ifIndex = ifEntry.getInteger("ifIndex");
                Long ifInOctets = ifEntry.getLong("ifInOctets");
                Long ifOutOctets = ifEntry.getLong("ifOutOctets");
                System.out.println("ifIndex = " + ifIndex);
                System.out.println("ifInOctets = " + ifInOctets);
                System.out.println("ifOutOctets = " + ifOutOctets);
            }
        }
    }
}

后记

在SpringBoot中使用MongoDB非常方便,添加依赖并配置MongoDB的相关连接信息,即可使用MongoTemplate来操作MongoDB了,当然,这里只是做了简单的保存操作,更复杂条件查询操作等会涉及到Criteria、Query的使用,具体可以翻阅相关API文档,也是非常简单的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值