Java Code Examples for com.google.cloud.dataflow.sdk.values.KV#getValue()

The following examples show how to use com.google.cloud.dataflow.sdk.values.KV#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: MergeBranches.java    From dockerflow with Apache License 2.0 6 votes vote down vote up
@Override
public KV<String, WorkflowArgs> apply(Iterable<KV<String, WorkflowArgs>> input) {
  String key = null;
  WorkflowArgs retval = null;

  // Merge arguments
  for (KV<String, WorkflowArgs> kv : input) {

    // Modify a copy
    WorkflowArgs wa = new WorkflowArgs(kv.getValue());

    // First time, nothing to merge
    if (retval == null) {
      key = kv.getKey();
      retval = wa;
    // Find differences and merge
    } else {
      retval.gatherArgs(wa);
    }
  }
  return KV.of(key, retval);
}
 
Example 2
Source File: FlinkPartialReduceFunction.java    From flink-dataflow with Apache License 2.0 6 votes vote down vote up
@Override
public void combine(Iterable<KV<K, VI>> elements, Collector<KV<K, VA>> out) throws Exception {

	final Iterator<KV<K, VI>> iterator = elements.iterator();
	// create accumulator using the first elements key
	KV<K, VI> first = iterator.next();
	K key = first.getKey();
	VI value = first.getValue();
	VA accumulator = keyedCombineFn.createAccumulator(key);
	accumulator = keyedCombineFn.addInput(key, accumulator, value);

	while(iterator.hasNext()) {
		value = iterator.next().getValue();
		accumulator = keyedCombineFn.addInput(key, accumulator, value);
	}

	out.collect(KV.of(key, accumulator));
}
 
Example 3
Source File: FileToState.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a Key-Value pair of (FilePath, FileContent) to (GCPResource, GCPResourceState).
 * The FilePath is a list of Strings which represents the location of a file.
 * The FileContent is the content of the file described by the FilePath.
 * The path is used to obtain the resource, and the content describes the state of that resource.
 * @param processContext The ProcessContext object that contains processContext-specific
 * methods and objects.
 */
@Override
public void processElement(ProcessContext processContext) {
  KV<List<String>, String> input = processContext.element();
  List<String> filePath = input.getKey();
  String fileContent = input.getValue();

  String orgName = filePath.size() > 0 ? filePath.get(0) : null;
  String projectId = filePath.size() > 1 ? filePath.get(1) : null;
  String policyFileName = filePath.size() > 2 ? filePath.get(2) : null;
  GCPProject project = new GCPProject(projectId, orgName);

  if (filePath.size() == 3 && GCPResourcePolicy.getPolicyFile().equals(policyFileName)) {
    // only project policies are supported for now.
    // filePath.size() must be 3 and of the form org_id/project_id/POLICY_FILE.
    Gson gson = new Gson();
    try {
      List<PolicyBinding> bindings = Arrays.asList(
          gson.fromJson(fileContent, PolicyBinding[].class));
      GCPResourceState policy = new GCPResourcePolicy(project, bindings);
      processContext.output(KV.of((GCPResource) project, policy));
      return;
    } catch (JsonSyntaxException jse) {
      addToSideOutput(
          processContext,
          project,
          String.format("Invalid policy json %s/%s/%s", orgName, projectId, policyFileName));
    }
  }
  addToSideOutput(
      processContext,
      project,
      String.format("Invalid policy filepath %s/%s/%s", orgName, projectId, policyFileName));
}
 
Example 4
Source File: UnmatchedStatesMessenger.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(ProcessContext context)
    throws Exception {
  KV<String, Iterable<GCPResource>> element = context.element();

  for (GCPResource resource : element.getValue()) {
    context.output(element.getKey() + ":" + resource.getId());
  }
}
 
Example 5
Source File: AutoComplete.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(ProcessContext c) {
  StringBuilder str = new StringBuilder();
  KV<String, List<CompletionCandidate>> elem = c.element();

  str.append(elem.getKey() +" @ "+ c.window() +" -> ");
  for(CompletionCandidate cand: elem.getValue()) {
    str.append(cand.toString() + " ");
  }
  System.out.println(str.toString());
  c.output(str.toString());
}
 
Example 6
Source File: FlinkReduceFunction.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public void reduce(Iterable<KV<K, VA>> values, Collector<KV<K, VO>> out) throws Exception {
	Iterator<KV<K, VA>> it = values.iterator();

	KV<K, VA> current = it.next();
	K k = current.getKey();
	VA accumulator = current.getValue();

	while (it.hasNext()) {
		current = it.next();
		keyedCombineFn.mergeAccumulators(k, ImmutableList.of(accumulator, current.getValue()) );
	}

	out.collect(KV.of(k, keyedCombineFn.extractOutput(k, accumulator)));
}
 
Example 7
Source File: GroupByNullKeyTest.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(ProcessContext c) {
	KV<Integer, String> record = c.element();
	long now = System.currentTimeMillis();
	int timestamp = record.getKey();
	String userName = record.getValue();
	if (userName != null) {
		// Sets the implicit timestamp field to be used in windowing.
		c.outputWithTimestamp(userName, new Instant(timestamp + now));
	}
}
 
Example 8
Source File: LoadBooks.java    From cloud-bigtable-examples with Apache License 2.0 5 votes vote down vote up
public void processElement(ProcessContext c) {
  KV<String, Integer> ngram = c.element();
  byte[] key = ngram.getKey().getBytes(STRING_ENCODING);
  int count = ngram.getValue();
  byte[] data = Bytes.toBytes(count);
  c.output(new Put(key).addColumn(FAMILY, COUNT_QUALIFIER, data));
}
 
Example 9
Source File: WordCount.java    From flink-dataflow with Apache License 2.0 4 votes vote down vote up
@Override
public String apply(KV<String, Long> input) {
	return input.getKey() + ": " + input.getValue();
}