From commits-return-22199-archive-asf-public=cust-asf.ponee.io@pulsar.apache.org Mon Feb 11 00:40:56 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 2E88418072F for ; Mon, 11 Feb 2019 01:40:56 +0100 (CET) Received: (qmail 60560 invoked by uid 500); 11 Feb 2019 00:40:55 -0000 Mailing-List: contact commits-help@pulsar.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pulsar.apache.org Delivered-To: mailing list commits@pulsar.apache.org Received: (qmail 60551 invoked by uid 99); 11 Feb 2019 00:40:55 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Feb 2019 00:40:55 +0000 From: GitBox To: commits@pulsar.apache.org Subject: [GitHub] sijie commented on a change in pull request #3561: Add a Pulsar IO MongoDB Message-ID: <154984565467.31190.12597205251198816778.gitbox@gitbox.apache.org> Date: Mon, 11 Feb 2019 00:40:54 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit sijie commented on a change in pull request #3561: Add a Pulsar IO MongoDB URL: https://github.com/apache/pulsar/pull/3561#discussion_r255365503 ########## File path: pulsar-io/mongo/src/main/java/org/apache/pulsar/io/mongodb/MongoSink.java ########## @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.io.mongodb; + +import com.mongodb.async.client.MongoClient; +import com.mongodb.async.client.MongoClients; +import com.mongodb.async.client.MongoCollection; +import com.mongodb.async.client.MongoDatabase; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.functions.api.Record; +import org.apache.pulsar.io.core.Sink; +import org.apache.pulsar.io.core.SinkContext; +import org.apache.pulsar.io.core.annotations.Connector; +import org.apache.pulsar.io.core.annotations.IOType; +import org.bson.BSONException; +import org.bson.Document; +import org.bson.json.JsonParseException; + +import java.util.Map; + +/** + * The base class for MongoDB sinks. + * Users need to implement extractKeyValue function to use this sink. + * This class assumes that the input will be JSON documents. + */ +@Connector( + name = "mongo", + type = IOType.SINK, + help = "A sink connector that sends pulsar messages to mongodb", + configClass = MongoConfig.class +) +@Slf4j +public class MongoSink implements Sink { + + private MongoConfig mongoConfig; + + private MongoClient mongoClient; + + + @Override + public void open(Map config, SinkContext sinkContext) throws Exception { + log.info("Open MongoDB Sink"); + mongoConfig = MongoConfig.load(config); + mongoConfig.validate(); + + mongoClient = MongoClients.create(mongoConfig.getMongoUri()); + } + + @Override + public void write(Record record) { + log.info("Received record: " + new String(record.getValue())); + try { + final byte[] docAsBytes = record.getValue(); + final Document doc = Document.parse(new String(docAsBytes)); Review comment: new String(docAsBytes, UTF_8); ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services