Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a new id strategy in the Sink Connector #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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.
*
* Original Work: Apache License, Version 2.0, Copyright 2017 Hans-Peter Grahsl.
*/

package com.mongodb.kafka.connect.sink.processor.id.strategy;

public class BsonOidProvidedInKeyStrategy extends BsonOidProvidedStrategy {

public BsonOidProvidedInKeyStrategy() {
super(ProvidedIn.KEY);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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.
*
* Original Work: Apache License, Version 2.0, Copyright 2017 Hans-Peter Grahsl.
*/

package com.mongodb.kafka.connect.sink.processor.id.strategy;

public class BsonOidProvidedInValueStrategy extends BsonOidProvidedStrategy {

public BsonOidProvidedInValueStrategy() {
super(ProvidedIn.VALUE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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.
*
* Original Work: Apache License, Version 2.0, Copyright 2017 Hans-Peter Grahsl.
*/

package com.mongodb.kafka.connect.sink.processor.id.strategy;

import static com.mongodb.kafka.connect.sink.MongoSinkTopicConfig.ID_FIELD;

import java.util.Optional;

import org.apache.kafka.connect.errors.DataException;
import org.apache.kafka.connect.sink.SinkRecord;

import org.bson.BsonObjectId;
import org.bson.BsonString;
import org.bson.BsonDocument;
import org.bson.BsonNull;
import org.bson.BsonValue;
import org.bson.types.ObjectId;

import com.mongodb.kafka.connect.sink.converter.SinkDocument;

abstract class BsonOidProvidedStrategy implements IdStrategy {

protected enum ProvidedIn {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about headers?

KEY,
VALUE
}

private ProvidedIn where;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private ProvidedIn where;
private final ProvidedIn where;


BsonOidProvidedStrategy(final ProvidedIn where) {
this.where = where;
}

@Override
public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) {
Optional<BsonDocument> optionalDoc = Optional.empty();
if (where.equals(ProvidedIn.KEY)) {
optionalDoc = doc.getKeyDoc();
}

if (where.equals(ProvidedIn.VALUE)) {
optionalDoc = doc.getValueDoc();
}

BsonValue id =
optionalDoc
.map(d -> d.get(ID_FIELD))
.orElseThrow(
() ->
new DataException(
"Provided id strategy is used but the document structure either contained"
+ " no _id field or it was null"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clarify: no top-level field


if (id instanceof BsonNull) {
throw new DataException(
"Provided id strategy used but the document structure contained an _id of type BsonNull");
}
return new BsonObjectId(new ObjectId((((BsonString)id).getValue())));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# MongoDB Kafka BsonObjectId

This implementaion allows the MongoDB Kafka sink connector to convert an existing _id that is string as ObjectId

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This implementaion allows the MongoDB Kafka sink connector to convert an existing _id that is string as ObjectId
This implementation allows the MongoDB Kafka sink connector to convert an existing _id that is string as ObjectId


# Usage

If the id is provided as part of the value you can use:
```
"post.processor.chain": "com.mongodb.kafka.connect.sink.processor.DocumentIdAdder",
"document.id.strategy": "com.mongodb.kafka.connect.sink.processor.id.strategy.BsonOidProvidedInValueStrategy",
"document.id.strategy.overwrite.existing": true,
```

If the id is provided as part of the key you can use:
```
"post.processor.chain": "com.mongodb.kafka.connect.sink.processor.DocumentIdAdder",
"document.id.strategy": "com.mongodb.kafka.connect.sink.processor.id.strategy.BsonOidProvidedInKeyStrategy",
"document.id.strategy.overwrite.existing": true,
```