@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); UUID uuid = new UUID(random.nextLong(), random.nextLong()); if (idx != -1) { row.setValue(idx, uuid.toString()); } else { row.add(column, uuid.toString()); } } return rows; } }
/** * Adds or sets the value to the beginning. * * @param index at which the column need to be inserted. * @param name of the field to be either set or added to record. * @param value to be added. */ public void addOrSetAtIndex(int index, String name, Object value) { int idx = find(name); if (idx != -1) { setValue(idx, value); } else { if (index < columns.size() && index < values.size()) { columns.add(index, name); values.add(index, value); } } } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx == -1) { row.add(column, value); continue; } Object object = row.getValue(idx); if (object == null) { row.setValue(idx, value); } else { if (object instanceof String) { if (((String) object).isEmpty()) { row.setValue(idx, value); } } else if (object instanceof JSONObject) { if (JSONObject.NULL.equals(object)) { row.setValue(idx, value); } } } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { if (object != null) { String value = (String) object; row.setValue(idx, value.toLowerCase()); } } } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { if (object != null) { String value = (String) object; row.setValue(idx, value.toUpperCase()); } } } } return rows; } }
/** * Adds or sets the value. * * @param name of the field to be either set or added to record. * @param value to be added. */ public void addOrSet(String name, Object value) { int idx = find(name); if (idx != -1) { setValue(idx, value); } else { add(name, value); } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object != null && object instanceof String) { String[] lines = ((String) object).split(regex); for (String line : lines) { Row r = new Row(row); r.setValue(idx, line); results.add(r); } } else { throw new DirectiveExecutionException( String.format("%s : Invalid type '%s' of column '%s'. Should be of type String.", toString(), object != null ? object.getClass().getName() : "null", column) ); } } } return results; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { try { row.setValue(idx, URLEncoder.encode((String) object, "UTF-8")); } catch (UnsupportedEncodingException e) { // Doesn't affect the row and it doesn't stop processing. } } else { throw new DirectiveExecutionException( String.format("%s : Invalid type '%s' of column '%s'. Should be of type String.", toString(), object != null ? object.getClass().getName() : "null", column) ); } } else { throw new DirectiveExecutionException(toString() + " : Column '" + column + "' does not exist in the row."); } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { try { row.setValue(idx, URLDecoder.decode((String) object, "UTF-8")); } catch (UnsupportedEncodingException e) { // Doesn't affect the row and it doesn't stop processing. } } else { throw new DirectiveExecutionException( String.format("%s : Invalid type '%s' of column '%s'. Should be of type String.", toString(), object != null ? object.getClass().getName() : "null", column) ); } } else { throw new DirectiveExecutionException(toString() + " : Column '" + column + "' does not exist in the row."); } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(col); if (idx != -1) { Object object = row.getValue(idx); if (object == null || (object instanceof String && ((String) object).trim().isEmpty())) { continue; } try { row.setValue(idx, convertType(type, object)); } catch (DirectiveExecutionException e) { throw e; } catch (Exception e) { throw new DirectiveExecutionException(String.format("Invalid data: Column '%s' can not be converted to '%s'", col, type)); } } } return rows; }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { Row masked = new Row(row); int idx = row.find(column); if (idx != -1) { masked.setValue(idx, maskShuffle((String) row.getValue(idx), 0)); } else { throw new DirectiveExecutionException(toString() + " : '" + column + "' column is not defined. Please check the wrangling step." ); } results.add(masked); } return results; }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { if (object != null) { String value = (String) object; row.setValue(idx, WordUtils.capitalizeFully(value)); } } } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(col); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { if (object != null) { String value = (String) object; row.setValue(idx, Trimmer.ltrim(value)); } } } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { if (object != null) { String value = (String) object; row.setValue(idx, Trimmer.trim(value)); } } } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object instanceof String) { if (object != null) { String value = (String) object; row.setValue(idx, Trimmer.rtrim(value)); } } } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { String value = TypeConvertor.toString(row.getValue(idx)); if (value == null) { continue; } row.setValue(idx, maskNumber(value, mask)); } else { row.add(column, new String("")); } } return rows; }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { for (String column : columns) { int idx = row.find(column); if (idx != -1) { Object v = row.getValue(idx); // Operates only on String types. try { if (v instanceof String) { String value = (String) v; // Safely converts to String. Unix4jCommandBuilder builder = Unix4j.echo(value).sed(pattern); if (builder.toExitValue() == 0) { row.setValue(idx, builder.toStringResult()); } } } catch (Exception e) { // If there is any issue, we pass it on without any transformation. } } results.add(row); } } return results; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); // If the data in the cell is null or is already of date format, then // continue to next row. if (object == null || object instanceof ZonedDateTime) { continue; } long longValue = getLongValue(object); ZonedDateTime zonedDateTime = getZonedDateTime(longValue, timeUnit, ZoneId.ofOffset("UTC", ZoneOffset.UTC)); row.setValue(idx, zonedDateTime); } } return rows; }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int sidx = row.find(source.value()); if (sidx == -1) { throw new DirectiveExecutionException(MSG.get("column.not.found", toString(), source.value())); } int didx = row.find(destination.value()); // If source and destination are same, then it's a nop. if (didx == sidx) { continue; } if (didx == -1) { // if destination column doesn't exist then add it. row.add(destination.value(), row.getValue(sidx)); } else { // if destination column exists, and force is set to false, then throw exception, else // overwrite it. if (!force) { throw new DirectiveExecutionException(toString() + " : Destination column '" + destination.value() + "' does not exist in the row. Use 'force' option to add new column."); } row.setValue(didx, row.getValue(sidx)); } } return rows; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { // Move the fields from the row into the context. ELContext ctx = new ELContext(context); ctx.set("this", row); for(String var : el.variables()) { ctx.set(var, row.getValue(var)); } // Execution of the script / expression based on the row data // mapped into context. try { ELResult result = el.execute(ctx); int idx = row.find(this.column); if (idx == -1) { row.add(this.column, result.getObject()); } else { row.setValue(idx, result.getObject()); } } catch (ELException e) { throw new DirectiveExecutionException(e.getMessage()); } } return rows; } }