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
SpringBoot2.x集成MongoDB实践_第1张图片

SpringBoot构建

开发环境

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

环境搭建

  1. 使用start.spring.io快速构建SpringBoot项目
    SpringBoot2.x集成MongoDB实践_第2张图片
    SpringBoot2.x集成MongoDB实践_第3张图片

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


<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.0modelVersion>
    <groupId>com.sdnware.odlgroupId>
    <artifactId>sdnware-odlartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>sdnware-odlname>
    <description>sdnware-OpendayLight Projectdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-mongodbartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.16.14version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <fork>truefork>
                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 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 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 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 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 entitys = snmpDao.getAllData("20180619");
        for (SNMPEntity entity : entitys) {
            LocalDateTime ifRecvTime = entity.getIfRecvTime();
            System.out.println(ifRecvTime + ": ");
            List 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文档,也是非常简单的。

你可能感兴趣的:(Spring,Java)