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.bson.Document;
 
  33 import org.json.JSONObject;
 
  35 import org.onap.datalake.feeder.domain.Db;
 
  36 import org.onap.datalake.feeder.domain.Topic;
 
  37 import org.slf4j.Logger;
 
  38 import org.slf4j.LoggerFactory;
 
  40 import org.springframework.beans.factory.annotation.Autowired;
 
  41 import org.springframework.stereotype.Service;
 
  43 import com.mongodb.MongoClient;
 
  44 import com.mongodb.client.MongoCollection;
 
  45 import com.mongodb.client.MongoDatabase;
 
  48  * Service to use MongoDB
 
  54 public class MongodbService {
 
  56         private final Logger log = LoggerFactory.getLogger(this.getClass());
 
  59         private DbService dbService;
 
  61         private MongoDatabase database;
 
  62         private MongoClient mongoClient;
 
  63         private Map<String, MongoCollection<Document>> mongoCollectionMap = new HashMap<>();
 
  67                 Db mongodb = dbService.getMongoDB();
 
  69                 mongoClient = new MongoClient(mongodb.getHost(), mongodb.getPort());
 
  70                 database = mongoClient.getDatabase(mongodb.getProperty1());
 
  74         public void cleanUp() {
 
  78         public void saveJsons(Topic topic, List<JSONObject> jsons) {
 
  79                 List<Document> documents = new ArrayList<>(jsons.size());
 
  80                 for (JSONObject json : jsons) {
 
  81                         //convert org.json JSONObject to MongoDB Document
 
  82                         Document doc = Document.parse(json.toString());
 
  84                         String id = topic.getMessageId(json); //id can be null
 
  91                 String collectionName = topic.getName().replaceAll("[^a-zA-Z0-9]","");//remove - _ .
 
  92                 MongoCollection<Document> collection = mongoCollectionMap.computeIfAbsent(collectionName, k -> database.getCollection(k));
 
  93                 collection.insertMany(documents);
 
  95                 log.debug("saved text to topic = {}, topic total count = {} ", topic, collection.countDocuments());