/** * Finds if the row has a column. * * @param row in which a column needs to be checked. * @param column name of the column to be checked. * @return true if column is not null and exists, false otherwise. */ public static boolean hascolumn(Row row, String column) { if (column == null) { return false; } return row.find(column) != -1 ? true : false; }
/** * Gets value based on the column name. * * @param col name of the column for which the value is retrieved. * @return value associated with column. */ public Object getValue(String col) { if (col != null && !col.isEmpty()) { int idx = find(col); if (idx != -1) { return values.get(idx); } } return null; }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { for (String column : columns) { int idx = row.find(column.trim()); if (idx != -1) { row.remove(idx); } } } return rows; } }
@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 { List<Row> results = new ArrayList<>(); for (Row row : rows) { int idx1 = row.find(col1); int idx2 = row.find(col2); if (idx1 != -1 && idx2 != -1) { StringBuilder builder = new StringBuilder(); builder.append(row.getValue(idx1)); builder.append(delimiter); builder.append(row.getValue(idx2)); row.add(dest, builder.toString()); } results.add(row); } return results; } }
private ZonedDateTime getDate(Row row, String colName) throws DirectiveExecutionException { // If one of the column contains now, then we return // the current date. if (colName.equalsIgnoreCase("now")) { return date; } // Else attempt to find the column. int idx = row.find(colName); if (idx == -1) { throw new DirectiveExecutionException(toString() + " : '" + colName + "' column is not defined in the row."); } Object o = row.getValue(idx); if (o == null || !(o instanceof ZonedDateTime)) { return null; } return (ZonedDateTime) o; } }
@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; } }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { for (Row row : rows) { int idx = row.find(source); if (idx != -1) { Object object = row.getValue(idx); if (object == null || !(object instanceof Double)) { continue; } Double value = (Double) object; String format = this.format.format(value); row.addOrSet(destination, format); } } 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 { for (Row row : rows) { int sidx = row.find(left); int didx = row.find(right); if (sidx == -1) { throw new DirectiveExecutionException(MSG.get("column.not.found", toString(), left)); } if (didx == -1) { throw new DirectiveExecutionException(MSG.get("column.not.found", toString(), right)); } row.setColumn(sidx, right); row.setColumn(didx, left); } 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, ErrorRowException { for (Row row : rows) { int idx = row.find(source.value()); int idxnew = row.find(target.value()); if (idx != -1) { if (idxnew == -1) { row.setColumn(idx, target.value()); } else { throw new DirectiveExecutionException( String.format( "%s : %s column already exists. Apply the directive 'drop %s' before renaming %s to %s.", toString(), target.value(), target.value(), source.value(), source.value() ) ); } } } return rows; } }
@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; }