All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
co.cask.cdap.etl.mock.realtime.LookupSource Maven / Gradle / Ivy
/*
* Copyright © 2015-2016 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 co.cask.cdap.etl.mock.realtime;
import co.cask.cdap.api.annotation.Name;
import co.cask.cdap.api.annotation.Plugin;
import co.cask.cdap.api.data.format.StructuredRecord;
import co.cask.cdap.api.data.schema.Schema;
import co.cask.cdap.api.plugin.PluginClass;
import co.cask.cdap.api.plugin.PluginConfig;
import co.cask.cdap.api.plugin.PluginPropertyField;
import co.cask.cdap.etl.api.Emitter;
import co.cask.cdap.etl.api.Lookup;
import co.cask.cdap.etl.api.realtime.RealtimeContext;
import co.cask.cdap.etl.api.realtime.RealtimeSource;
import co.cask.cdap.etl.api.realtime.SourceState;
import co.cask.cdap.etl.proto.v2.ETLPlugin;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
/**
* Source used to test lookup functionality. Takes a set of fields as config and emits a single record with
* each field value being the result of lookup for that field.
*/
@Plugin(type = RealtimeSource.PLUGIN_TYPE)
@Name("Lookup")
public class LookupSource extends RealtimeSource {
public static final PluginClass PLUGIN_CLASS = getPluginClass();
private final Config config;
private Set fields;
private Schema schema;
private Lookup lookup;
public LookupSource(Config config) {
this.config = config;
}
@Override
public void initialize(RealtimeContext context) throws Exception {
super.initialize(context);
fields = new HashSet<>();
List schemaFields = new ArrayList<>(fields.size());
for (String fieldName : Splitter.on(',').split(config.fields)) {
fields.add(fieldName);
schemaFields.add(Schema.Field.of(fieldName, Schema.nullableOf(Schema.of(Schema.Type.STRING))));
}
schema = Schema.recordOf("lookupRecord", schemaFields);
lookup = context.provide(config.lookupName, new HashMap());
}
@Nullable
@Override
public SourceState poll(Emitter writer, SourceState currentState) throws Exception {
if (currentState.getState("done") == null) {
Map fieldValues = lookup.lookup(fields);
StructuredRecord.Builder recordBuilder = StructuredRecord.builder(schema);
for (Map.Entry entry : fieldValues.entrySet()) {
recordBuilder.set(entry.getKey(), entry.getValue());
}
writer.emit(recordBuilder.build());
currentState.setState("done", new byte[] { 1 });
}
return currentState;
}
/**
* Config for the source.
*/
public static class Config extends PluginConfig {
private String fields;
private String lookupName;
}
public static ETLPlugin getPlugin(Set fields, String lookupName) {
Map properties = new HashMap<>();
properties.put("fields", Joiner.on(',').join(fields));
properties.put("lookupName", lookupName);
return new ETLPlugin("Lookup", RealtimeSource.PLUGIN_TYPE, properties, null);
}
private static PluginClass getPluginClass() {
Map properties = new HashMap<>();
properties.put("fields", new PluginPropertyField("fields", "", "string", true, false));
properties.put("lookupName", new PluginPropertyField("lookupName", "", "string", true, false));
return new PluginClass(RealtimeSource.PLUGIN_TYPE, "Lookup", "", LookupSource.class.getName(),
"config", properties);
}
}