package org.springblade.sink.mongodb;

import cn.hutool.db.nosql.mongo.MongoFactory;
import com.ldt.entity.alarm.IPAlarm;
import com.mongodb.client.MongoDatabase;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.bson.Document;


/**
 * @author: lianghanqiang
 * @description: 自定义 sink for mongodb
 * @since: 10/26/21 -- 11:12 AM
 */
public class MongoSink extends RichSinkFunction<String> {

	private MongoDatabase mongoDatabase;

	/**
	 * 链接mongo
	 */
	@Override
	public void open(Configuration parameters) throws Exception {
		this.mongoDatabase = MongoFactory.getDS("master").getDb("ldt");
		super.open(parameters);
	}

	/**
	 * 将数据写入
	 */
	@Override
	public void invoke(String value, Context context) {
		System.out.println(value);
		Document document = new Document();
		document.append("data","data");
		this.mongoDatabase.getCollection("log").insertOne(document);
	}

	/**
	 * 释放资源
	 */
	@Override
	public void close() throws Exception {
		super.close();
	}
}
