package org.springblade.sink.mongodb;

import cn.hutool.db.nosql.mongo.MongoFactory;
import com.mongodb.client.MongoDatabase;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.bson.Document;

/**
 * @author: lianghanqiang
 * @description: api log 输出 sink
 * @since: 10/27/21 -- 3:22 PM
 */
@Slf4j
public class ApiLogWriter extends RichSinkFunction<String> {

	private MongoDatabase mongoDatabase;

	@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) {
		log.info(value);
		Document document = new Document();
		document.append("data","data");
		this.mongoDatabase.getCollection("log").insertOne(document);
	}

	@Override
	public void close() throws Exception {
		super.close();
	}
}
