Java Code Examples for org.apache.beam.sdk.values.Row#getValue()

The following examples show how to use org.apache.beam.sdk.values.Row#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RowJson.java    From beam with Apache License 2.0 6 votes vote down vote up
private void writeRow(Row row, Schema schema, JsonGenerator gen) throws IOException {
  gen.writeStartObject();
  for (int i = 0; i < schema.getFieldCount(); ++i) {
    Field field = schema.getField(i);
    Object value = row.getValue(i);
    if (dropNullsOnWrite && value == null && field.getType().getNullable()) {
      continue;
    }
    gen.writeFieldName(field.getName());
    if (field.getType().getNullable() && value == null) {
      gen.writeNull();
      continue;
    }
    writeValue(gen, field.getType(), value);
  }
  gen.writeEndObject();
}
 
Example 2
Source File: RowCoderGenerator.java    From beam with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static void encodeDelegate(
    Coder[] coders, Row value, OutputStream outputStream, boolean hasNullableFields)
    throws IOException {
  checkState(value.getFieldCount() == value.getSchema().getFieldCount());

  // Encode the field count. This allows us to handle compatible schema changes.
  VAR_INT_CODER.encode(value.getFieldCount(), outputStream);
  // Encode a bitmap for the null fields to save having to encode a bunch of nulls.
  NULL_LIST_CODER.encode(scanNullFields(value, hasNullableFields), outputStream);
  for (int idx = 0; idx < value.getFieldCount(); ++idx) {
    Object fieldValue = value.getValue(idx);
    if (value.getValue(idx) != null) {
      coders[idx].encode(fieldValue, outputStream);
    }
  }
}
 
Example 3
Source File: Cast.java    From beam with Apache License 2.0 6 votes vote down vote up
public static Row castRow(Row input, Schema inputSchema, Schema outputSchema) {
  if (input == null) {
    return null;
  }

  Row.Builder output = Row.withSchema(outputSchema);
  for (int i = 0; i < outputSchema.getFieldCount(); i++) {
    Schema.Field outputField = outputSchema.getField(i);

    int fromFieldIdx = inputSchema.indexOf(outputField.getName());
    Schema.Field inputField = inputSchema.getField(fromFieldIdx);

    Object inputValue = input.getValue(fromFieldIdx);
    Object outputValue = castValue(inputValue, inputField.getType(), outputField.getType());

    output.addValue(outputValue);
  }

  return output.build();
}
 
Example 4
Source File: ClickHouseWriter.java    From beam with Apache License 2.0 6 votes vote down vote up
static void writeRow(ClickHouseRowBinaryStream stream, TableSchema schema, Row row)
    throws IOException {
  for (TableSchema.Column column : schema.columns()) {
    if (!column.materializedOrAlias()) {
      Object value = row.getValue(column.name());

      if (column.columnType().nullable()) {
        writeNullableValue(stream, column.columnType(), value);
      } else {
        if (value == null) {
          value = column.defaultValue();
        }

        writeValue(stream, column.columnType(), value);
      }
    }
  }
}
 
Example 5
Source File: PubsubMessageToRow.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Get the value for a field from a given payload in the order they're specified in the flat
 * schema.
 */
private Object getValueForFieldFlatSchema(Schema.Field field, Instant timestamp, Row payload) {
  String fieldName = field.getName();
  if (TIMESTAMP_FIELD.equals(fieldName)) {
    return timestamp;
  } else {
    return payload.getValue(fieldName);
  }
}
 
Example 6
Source File: RowCoderGenerator.java    From beam with Apache License 2.0 5 votes vote down vote up
private static BitSet scanNullFields(Row row, boolean hasNullableFields) {
  BitSet nullFields = new BitSet(row.getFieldCount());
  if (hasNullableFields) {
    for (int idx = 0; idx < row.getFieldCount(); ++idx) {
      if (row.getValue(idx) == null) {
        nullFields.set(idx);
      }
    }
  }
  return nullFields;
}
 
Example 7
Source File: SchemaAggregateFn.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public OutputT apply(Row row) {
  Row selected = rowSelector.select(row);
  if (fieldAggregation.needsFlattening) {
    selected = flatteningSelector.select(selected);
  }
  if (extractBaseValue
      && selected.getSchema().getField(0).getType().getTypeName().isLogicalType()) {
    return (OutputT) selected.getBaseValue(0, Object.class);
  }
  return selected.getValue(0);
}
 
Example 8
Source File: OneOfType.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Value toInputType(Row base) {
  EnumerationType.Value caseType = null;
  Object oneOfValue = null;
  for (int i = 0; i < base.getFieldCount(); ++i) {
    Object value = base.getValue(i);
    if (value != null) {
      checkArgument(caseType == null, "More than one field set in union " + this);
      caseType = enumerationType.valueOf(oneOfSchema.getField(i).getName());
      oneOfValue = value;
    }
  }
  checkNotNull(oneOfValue, "No value set in union" + this);
  return createValue(caseType, oneOfValue);
}
 
Example 9
Source File: BigQueryUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Convert a BigQuery TableRow to a Beam Row. */
public static TableRow toTableRow(Row row) {
  TableRow output = new TableRow();
  for (int i = 0; i < row.getFieldCount(); i++) {
    Object value = row.getValue(i);
    Field schemaField = row.getSchema().getField(i);
    output = output.set(schemaField.getName(), fromBeamField(schemaField.getType(), value));
  }
  return output;
}
 
Example 10
Source File: BeamSortRel.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Row row1, Row row2) {
  for (int i = 0; i < fieldsIndices.size(); i++) {
    int fieldIndex = fieldsIndices.get(i);
    int fieldRet = 0;

    FieldType fieldType = row1.getSchema().getField(fieldIndex).getType();
    SqlTypeName sqlTypeName = CalciteUtils.toSqlTypeName(fieldType);
    // whether NULL should be ordered first or last(compared to non-null values) depends on
    // what user specified in SQL(NULLS FIRST/NULLS LAST)
    boolean isValue1Null = (row1.getValue(fieldIndex) == null);
    boolean isValue2Null = (row2.getValue(fieldIndex) == null);
    if (isValue1Null && isValue2Null) {
      continue;
    } else if (isValue1Null && !isValue2Null) {
      fieldRet = -1 * (nullsFirst.get(i) ? -1 : 1);
    } else if (!isValue1Null && isValue2Null) {
      fieldRet = 1 * (nullsFirst.get(i) ? -1 : 1);
    } else {
      switch (sqlTypeName) {
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case FLOAT:
        case DOUBLE:
        case VARCHAR:
        case DATE:
        case TIMESTAMP:
          Comparable v1 = row1.getBaseValue(fieldIndex, Comparable.class);
          Comparable v2 = row2.getBaseValue(fieldIndex, Comparable.class);
          fieldRet = v1.compareTo(v2);
          break;
        default:
          throw new UnsupportedOperationException(
              "Data type: " + sqlTypeName + " not supported yet!");
      }
    }

    fieldRet *= (orientation.get(i) ? 1 : -1);

    if (fieldRet != 0) {
      return fieldRet;
    }
  }
  return 0;
}