All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.frameworkset.tran.mongodb.cdc.ChangeStreamPipeline Maven / Gradle / Ivy

Go to download

bboss etl,datastream,elasticsearch client with restful and java api without elasticsearch jar dependended.

There is a newer version: 7.3.0
Show newest version
/*
 *
 * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package org.frameworkset.tran.mongodb.cdc;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClientSettings;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;



/**
 * A change stream aggregation pipeline, used to modify the output of a MongoDB change stream.
 *
 * @see Modify Change Stream Output
 */
public class ChangeStreamPipeline {

    private List stages;

    public ChangeStreamPipeline(String json) {
        this.stages = parse(json);
    }

    public ChangeStreamPipeline(List stages) {
        this.stages = stages;
    }

    public ChangeStreamPipeline(Bson... stages) {
        this(Arrays.asList(stages));
    }

    public List getStages() {
        return stages;
    }

    /**
     * Creates a new pipeline that is a combination of the current and supplied pipeline stages in serial.
     *
     * @param pipeline the pipeline to add in serial.
     * @return the combined pipeline
     */
    public ChangeStreamPipeline then(ChangeStreamPipeline pipeline) {
        List stages = new ArrayList();
        stages.addAll(this.getStages());
        stages.addAll(pipeline.getStages());
        return new ChangeStreamPipeline(stages);
    }

    public String toString() {
        return format(stages);
    }

    private static String format(List stages) {
        return new BasicDBObject("stages", stages)
                .toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry())
                .getArray("stages")
                .getValues()
                .toString();
    }

    private static List parse(String json) {
        if (json == null || json.isEmpty()) {
            return Collections.emptyList();
        }

        // Top-level for `parse` must be a document not a list, hence this trick
        return Document.parse("{stages: " + json + "}")
                .getList("stages", Document.class);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy