org.elasticsearch.hadoop.serialization.bulk.UpdateBulkFactory Maven / Gradle / Ivy
The newest version!
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.
*/
package org.elasticsearch.hadoop.serialization.bulk;
import java.util.List;
import org.elasticsearch.hadoop.EsHadoopIllegalStateException;
import org.elasticsearch.hadoop.cfg.ConfigurationOptions;
import org.elasticsearch.hadoop.cfg.Settings;
import org.elasticsearch.hadoop.serialization.bulk.MetadataExtractor.Metadata;
import org.elasticsearch.hadoop.util.Assert;
import org.elasticsearch.hadoop.util.EsMajorVersion;
import org.elasticsearch.hadoop.util.StringUtils;
class UpdateBulkFactory extends AbstractBulkFactory {
private final int RETRY_ON_FAILURE;
private final String RETRY_HEADER;
private final String SCRIPT_2X;
private final String SCRIPT_5X;
private final String SCRIPT_LANG_5X;
private final String SCRIPT_1X;
private final String SCRIPT_LANG_1X;
private final boolean HAS_SCRIPT, HAS_LANG, HAS_SCRIPT_UPSERT;
private final boolean UPSERT;
public UpdateBulkFactory(Settings settings, MetadataExtractor metaExtractor, EsMajorVersion esMajorVersion) {
this(settings, false, metaExtractor, esMajorVersion);
}
public UpdateBulkFactory(Settings settings, boolean upsert, MetadataExtractor metaExtractor, EsMajorVersion esMajorVersion) {
super(settings, metaExtractor, esMajorVersion);
UPSERT = upsert;
RETRY_ON_FAILURE = settings.getUpdateRetryOnConflict();
RETRY_HEADER = getRequestParameterNames().retryOnConflict + RETRY_ON_FAILURE + "";
HAS_SCRIPT = settings.hasUpdateScript();
HAS_SCRIPT_UPSERT = settings.hasScriptUpsert();
HAS_LANG = StringUtils.hasText(settings.getUpdateScriptLang());
SCRIPT_LANG_5X = ",\"lang\":\"" + settings.getUpdateScriptLang() + "\"";
SCRIPT_LANG_1X = "\"lang\":\"" + settings.getUpdateScriptLang() + "\",";
if (HAS_SCRIPT) {
if (StringUtils.hasText(settings.getUpdateScriptInline())) {
// INLINE
String source = "inline";
if (esMajorVersion.onOrAfter(EsMajorVersion.V_6_X)) {
source = "source";
}
SCRIPT_5X = "{\"script\":{\"" + source + "\":\"" + settings.getUpdateScriptInline() + "\"";
SCRIPT_2X = SCRIPT_5X;
SCRIPT_1X = "\"script\":\"" + settings.getUpdateScriptInline() + "\"";
} else if (StringUtils.hasText(settings.getUpdateScriptFile())) {
// FILE
SCRIPT_5X = "{\"script\":{\"file\":\"" + settings.getUpdateScriptFile() + "\"";
SCRIPT_2X = SCRIPT_5X;
SCRIPT_1X = "\"script_file\":\"" + settings.getUpdateScriptFile() + "\"";
} else if (StringUtils.hasText(settings.getUpdateScriptStored())) {
// STORED
SCRIPT_5X = "{\"script\":{\"stored\":\"" + settings.getUpdateScriptStored() + "\"";
SCRIPT_2X = "{\"script\":{\"id\":\"" + settings.getUpdateScriptStored() + "\"";
SCRIPT_1X = "\"script_id\":\"" + settings.getUpdateScriptStored() + "\"";
} else {
throw new EsHadoopIllegalStateException("No update script found...");
}
} else {
SCRIPT_5X = null;
SCRIPT_2X = null;
SCRIPT_1X = null;
}
}
@Override
protected String getOperation() {
return ConfigurationOptions.ES_OPERATION_UPDATE;
}
@Override
protected void otherHeader(List