Java Code Examples for org.apache.beam.sdk.options.ValueProvider#get()

The following examples show how to use org.apache.beam.sdk.options.ValueProvider#get() . 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: DualInputNestedvalueProviderTest.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedValueProviderRuntime() throws Exception {
  TestOptions options = PipelineOptionsFactory.as(TestOptions.class);
  ValueProvider<String> bar = options.getBar();
  ValueProvider<Integer> foo = options.getFoo();
  ValueProvider<String> barfoo =
      DualInputNestedValueProvider.of(
          bar,
          foo,
          new SerializableFunction<TranslatorInput<String, Integer>, String>() {
            @Override
            public String apply(TranslatorInput<String, Integer> input) {
              return input.getX() + (input.getY() + 1);
            }
          });
  assertFalse(barfoo.isAccessible());
  expectedException.expect(RuntimeException.class);
  expectedException.expectMessage("Value only available at runtime");
  barfoo.get();
}
 
Example 2
Source File: DisplayData.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Create a display item for the specified key and {@link ValueProvider}. */
public static ItemSpec<?> item(String key, @Nullable ValueProvider<?> value) {
  if (value == null) {
    return item(key, Type.STRING, null);
  }
  if (value.isAccessible()) {
    Object got = value.get();
    if (got == null) {
      return item(key, Type.STRING, null);
    }
    Type type = inferType(got);
    if (type != null) {
      return item(key, type, got);
    }
  }
  // General case: not null and type not inferable. Fall back to toString of the VP itself.
  return item(key, Type.STRING, String.valueOf(value));
}
 
Example 3
Source File: BigQueryIO.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the table to write, or {@code null} if writing with {@code tableFunction}.
 *
 * <p>If the table's project is not specified, use the executing project.
 */
@Nullable
ValueProvider<TableReference> getTableWithDefaultProject(BigQueryOptions bqOptions) {
  ValueProvider<TableReference> table = getTable();
  if (table == null) {
    return table;
  }

  if (!table.isAccessible()) {
    LOG.info(
        "Using a dynamic value for table input. This must contain a project"
            + " in the table reference: {}",
        table);
    return table;
  }
  if (Strings.isNullOrEmpty(table.get().getProjectId())) {
    // If user does not specify a project we assume the table to be located in
    // the default project.
    TableReference tableRef = table.get();
    tableRef.setProjectId(bqOptions.getProject());
    return NestedValueProvider.of(
        StaticValueProvider.of(BigQueryHelpers.toJsonString(tableRef)),
        new JsonTableRefToTableRef());
  }
  return table;
}
 
Example 4
Source File: DynamicOneFilePerWindow.java    From dlp-dataflow-deidentification with Apache License 2.0 4 votes vote down vote up
public DynamicOneFilePerWindow(ValueProvider<String> filenamePrefix, Integer numShards) {
  if (filenamePrefix.isAccessible()) this.filenamePrefix = filenamePrefix.get();
  else this.filenamePrefix = "output";
  this.numShards = numShards;
}
 
Example 5
Source File: WriteOneFilePerWindow.java    From dlp-dataflow-deidentification with Apache License 2.0 4 votes vote down vote up
public WriteOneFilePerWindow(ValueProvider<String> filenamePrefix, Integer numShards) {
  if (filenamePrefix.isAccessible()) this.filenamePrefix = filenamePrefix.get();
  else this.filenamePrefix = "output";
  this.numShards = numShards;
}
 
Example 6
Source File: FileIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Defines a default {@link FileNaming} which will use the prefix and suffix supplied to create
 * a name based on the window, pane, number of shards, shard index, and compression. Removes
 * window when in the {@link GlobalWindow} and pane info when it is the only firing of the pane.
 */
public static FileNaming defaultNaming(
    final ValueProvider<String> prefix, final ValueProvider<String> suffix) {
  return (window, pane, numShards, shardIndex, compression) -> {
    checkArgument(window != null, "window can not be null");
    checkArgument(pane != null, "pane can not be null");
    checkArgument(compression != null, "compression can not be null");
    StringBuilder res = new StringBuilder(prefix.get());
    if (window != GlobalWindow.INSTANCE) {
      if (res.length() > 0) {
        res.append("-");
      }
      checkArgument(
          window instanceof IntervalWindow,
          "defaultNaming() supports only windows of type %s, " + "but got window %s of type %s",
          IntervalWindow.class.getSimpleName(),
          window,
          window.getClass().getSimpleName());
      IntervalWindow iw = (IntervalWindow) window;
      res.append(iw.start().toString()).append("-").append(iw.end().toString());
    }
    boolean isOnlyFiring = pane.isFirst() && pane.isLast();
    if (!isOnlyFiring) {
      if (res.length() > 0) {
        res.append("-");
      }
      res.append(pane.getIndex());
    }
    if (res.length() > 0) {
      res.append("-");
    }
    String numShardsStr = String.valueOf(numShards);
    // A trillion shards per window per pane ought to be enough for everybody.
    DecimalFormat df =
        new DecimalFormat("000000000000".substring(0, Math.max(5, numShardsStr.length())));
    res.append(df.format(shardIndex)).append("-of-").append(df.format(numShards));
    res.append(suffix.get());
    res.append(compression.getSuggestedSuffix());
    return res.toString();
  };
}
 
Example 7
Source File: BigQueryIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/** See {@link Read#getTable()}. */
@Nullable
public TableReference getTable() {
  ValueProvider<TableReference> provider = getTableProvider();
  return provider == null ? null : provider.get();
}
 
Example 8
Source File: BigtableIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/** Returns the table being read from. */
@Nullable
public String getTableId() {
  ValueProvider<String> tableId = getBigtableConfig().getTableId();
  return tableId != null && tableId.isAccessible() ? tableId.get() : null;
}