2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 China Mobile
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.datalake.feeder.service;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import javax.annotation.PostConstruct;
29 import javax.annotation.PreDestroy;
31 import org.apache.commons.lang3.StringUtils;
32 import org.bson.Document;
34 import org.json.JSONObject;
36 import org.onap.datalake.feeder.domain.Db;
37 import org.onap.datalake.feeder.domain.Topic;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
44 import com.mongodb.MongoClient;
45 import com.mongodb.MongoClientOptions;
46 import com.mongodb.MongoClientOptions.Builder;
47 import com.mongodb.MongoCredential;
48 import com.mongodb.ServerAddress;
49 import com.mongodb.client.MongoCollection;
50 import com.mongodb.client.MongoDatabase;
53 * Service to use MongoDB
59 public class MongodbService {
61 private final Logger log = LoggerFactory.getLogger(this.getClass());
64 private DbService dbService;
66 private MongoDatabase database;
67 private MongoClient mongoClient;
68 private Map<String, MongoCollection<Document>> mongoCollectionMap = new HashMap<>();
72 Db mongodb = dbService.getMongoDB();
74 String host = mongodb.getHost();
76 Integer port = mongodb.getPort();
77 if (port == null || port == 0) {
78 port = 27017; //MongoDB default
81 String databaseName = mongodb.getDatabase();
82 String userName = mongodb.getLogin();
83 String password = mongodb.getPass();
85 MongoCredential credential = null;
86 if (StringUtils.isNoneBlank(userName) && StringUtils.isNoneBlank(password)) {
87 credential = MongoCredential.createCredential(userName, databaseName, password.toCharArray());
90 Builder builder = MongoClientOptions.builder();
91 builder.serverSelectionTimeout(30000);//server selection timeout, in milliseconds
93 //http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/connecting/ssl/
94 builder.sslEnabled(Boolean.TRUE.equals(mongodb.getEncrypt()));// getEncrypt() can be null
95 MongoClientOptions options = builder.build();
97 mongoClient = new MongoClient(new ServerAddress(host, port), credential, options);
98 database = mongoClient.getDatabase(mongodb.getDatabase());
102 public void cleanUp() {
106 public void saveJsons(Topic topic, List<JSONObject> jsons) {
107 List<Document> documents = new ArrayList<>(jsons.size());
108 for (JSONObject json : jsons) {
109 //convert org.json JSONObject to MongoDB Document
110 Document doc = Document.parse(json.toString());
112 String id = topic.getMessageId(json); //id can be null
119 String collectionName = topic.getName().replaceAll("[^a-zA-Z0-9]", "");//remove - _ .
120 MongoCollection<Document> collection = mongoCollectionMap.computeIfAbsent(collectionName, k -> database.getCollection(k));
121 collection.insertMany(documents);
123 log.debug("saved text to topic = {}, topic total count = {} ", topic, collection.countDocuments());