
co.cask.directives.transformation.CharacterCut Maven / Gradle / Ivy
The newest version!
/*
* Copyright © 2017 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.directives.transformation;
import co.cask.cdap.api.annotation.Description;
import co.cask.cdap.api.annotation.Name;
import co.cask.cdap.api.annotation.Plugin;
import co.cask.wrangler.api.Arguments;
import co.cask.wrangler.api.Directive;
import co.cask.wrangler.api.DirectiveExecutionException;
import co.cask.wrangler.api.DirectiveParseException;
import co.cask.wrangler.api.ExecutorContext;
import co.cask.wrangler.api.Row;
import co.cask.wrangler.api.annotations.Categories;
import co.cask.wrangler.api.parser.ColumnName;
import co.cask.wrangler.api.parser.Text;
import co.cask.wrangler.api.parser.TokenType;
import co.cask.wrangler.api.parser.UsageDefinition;
import org.unix4j.Unix4j;
import java.util.List;
/**
* A directive implements unix cut directive.
*/
@Plugin(type = Directive.Type)
@Name(CharacterCut.NAME)
@Categories(categories = { "transform"})
@Description("UNIX-like 'cut' directive for splitting text.")
public class CharacterCut implements Directive {
public static final String NAME = "cut-character";
private String source;
private String destination;
private String range;
@Override
public UsageDefinition define() {
UsageDefinition.Builder builder = UsageDefinition.builder(NAME);
builder.define("source", TokenType.COLUMN_NAME);
builder.define("destination", TokenType.COLUMN_NAME);
builder.define("ranges", TokenType.TEXT);
return builder.build();
}
@Override
public void initialize(Arguments args) throws DirectiveParseException {
this.source = ((ColumnName) args.value("source")).value();
this.destination = ((ColumnName) args.value("destination")).value();
this.range = ((Text) args.value("ranges")).value();
}
@Override
public void destroy() {
// no-op
}
@Override
public List execute(List rows, ExecutorContext context) throws DirectiveExecutionException {
for (Row row : rows) {
int idx = row.find(source);
if (idx != -1) {
Object value = row.getValue(idx);
if (value instanceof String) {
String result = Unix4j.fromString((String) value).cut("-c", range).toStringResult();
row.addOrSet(destination, result);
} else {
throw new DirectiveExecutionException(
String.format("%s : Invalid type '%s' of column '%s'. Should be of type String.", toString(),
value.getClass().getName(), source)
);
}
} else {
throw new DirectiveExecutionException(toString() + " : Scope column '" + source + "' does not exist in the row.");
}
}
return rows;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy