io.cdap.plugin.batch.source.KVTableSource Maven / Gradle / Ivy
/*
* Copyright © 2015-2019 Cask Data, 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.
*/
package io.cdap.plugin.batch.source;
import com.google.common.collect.Maps;
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.annotation.Requirements;
import io.cdap.cdap.api.data.format.StructuredRecord;
import io.cdap.cdap.api.data.schema.Schema;
import io.cdap.cdap.api.dataset.lib.KeyValue;
import io.cdap.cdap.api.dataset.lib.KeyValueTable;
import io.cdap.cdap.etl.api.Emitter;
import io.cdap.cdap.etl.api.PipelineConfigurer;
import io.cdap.plugin.common.BatchReadableWritableConfig;
import io.cdap.plugin.common.Properties;
import java.util.Map;
/**
* CDAP Key Value Table Dataset Batch Source.
*/
@Plugin(type = "batchsource")
@Name("KVTable")
@Description("Reads the entire contents of a KeyValueTable. Outputs records with a 'key' field and a 'value' field. " +
"Both fields are of type bytes.")
@Requirements(datasetTypes = KeyValueTable.TYPE)
public class KVTableSource extends BatchReadableSource {
private static final Schema SCHEMA = Schema.recordOf(
"keyValue",
Schema.Field.of("key", Schema.of(Schema.Type.BYTES)),
Schema.Field.of("value", Schema.of(Schema.Type.BYTES))
);
/**
* Config class for KVTableSource
*/
public static class KVTableConfig extends BatchReadableWritableConfig {
public KVTableConfig(String name) {
super(name);
}
}
private final KVTableConfig kvTableConfig;
@Override
public void configurePipeline(PipelineConfigurer pipelineConfigurer) {
super.configurePipeline(pipelineConfigurer);
pipelineConfigurer.getStageConfigurer().setOutputSchema(SCHEMA);
}
public KVTableSource(KVTableConfig kvTableConfig) {
super(kvTableConfig);
this.kvTableConfig = kvTableConfig;
}
@Override
protected Map getProperties() {
Map properties = Maps.newHashMap(kvTableConfig.getProperties().getProperties());
properties.put(Properties.BatchReadableWritable.NAME, kvTableConfig.getName());
properties.put(Properties.BatchReadableWritable.TYPE, KeyValueTable.class.getName());
return properties;
}
@Override
public void transform(KeyValue input, Emitter emitter) throws Exception {
emitter.emit(StructuredRecord.builder(SCHEMA).set("key", input.getKey()).set("value", input.getValue()).build());
}
}