forked from confluentinc/kafka-connect-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port ES Scripted Upsert to 11.1.0 from confluentinc#759
Signed-off-by: Nicholas Cole <nicholas.cole@walmart.com>
- Loading branch information
1 parent
cc26466
commit 3e66135
Showing
7 changed files
with
337 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/io/confluent/connect/elasticsearch/util/ScriptParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2018 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.connect.elasticsearch.util; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.elasticsearch.script.Script; | ||
|
||
import java.util.Map; | ||
|
||
public class ScriptParser { | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
public static Script parseScript(String scriptJson) throws JsonProcessingException { | ||
|
||
Map<String, Object> map = ScriptParser.parseSchemaStringAsJson(scriptJson); | ||
|
||
return Script.parse(map); | ||
} | ||
|
||
private static Map<String, Object> parseSchemaStringAsJson(String scriptJson) | ||
throws JsonProcessingException { | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
Map<String, Object> scriptConverted; | ||
|
||
scriptConverted = objectMapper.readValue( | ||
scriptJson, new TypeReference<Map<String, Object>>(){}); | ||
|
||
return scriptConverted; | ||
} | ||
|
||
public static Script parseScriptWithParams(String scriptJson, String jsonPayload) | ||
throws JsonProcessingException { | ||
|
||
Map<String, Object> map = ScriptParser.parseSchemaStringAsJson(scriptJson); | ||
|
||
Map<String, Object> fields = objectMapper.readValue(jsonPayload, | ||
new TypeReference<Map<String, Object>>() {}); | ||
|
||
map.put("params", fields); | ||
|
||
return Script.parse(map); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/io/confluent/connect/elasticsearch/validator/ScriptValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2018 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.connect.elasticsearch.validator; | ||
|
||
import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.WRITE_METHOD_CONFIG; | ||
import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.WriteMethod.SCRIPTED_UPSERT; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import io.confluent.connect.elasticsearch.util.ScriptParser; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.common.config.ConfigException; | ||
import org.elasticsearch.script.Script; | ||
|
||
public class ScriptValidator implements ConfigDef.Validator, ConfigDef.Recommender { | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void ensureValid(String name, Object value) { | ||
|
||
if (value == null) { | ||
return; | ||
} | ||
|
||
String script = (String) value; | ||
|
||
try { | ||
Script parsedScript = ScriptParser.parseScript(script); | ||
|
||
if (parsedScript.getIdOrCode() == null) { | ||
throw new ConfigException(name, script, "The specified script is missing code"); | ||
} else if (parsedScript.getLang() == null) { | ||
throw new ConfigException(name, script, "The specified script is missing lang"); | ||
} | ||
|
||
} catch (JsonProcessingException jsonProcessingException) { | ||
throw new ConfigException( | ||
name, script, "The specified script is not a valid Elasticsearch painless script"); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "A valid script that is able to be parsed"; | ||
} | ||
|
||
@Override | ||
public List<Object> validValues(String name, Map<String, Object> parsedConfig) { | ||
if (!parsedConfig.get(WRITE_METHOD_CONFIG).equals(SCRIPTED_UPSERT)) { | ||
return new ArrayList<>(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean visible(String name, Map<String, Object> parsedConfig) { | ||
return parsedConfig.get(WRITE_METHOD_CONFIG).equals(SCRIPTED_UPSERT.name()); | ||
} | ||
} |
Oops, something went wrong.