Java Code Examples for org.apache.spark.api.java.function.ForeachFunction

The following examples show how to use org.apache.spark.api.java.function.ForeachFunction. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: sylph   Source File: SparkStreamingSqlAnalyse.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void selectQuery(SelectQuery statement)
{
    builder.addHandler(sparkSession -> {
        Dataset<Row> df = sparkSession.sql(statement.toString());
        df.foreach((ForeachFunction<Row>) row -> System.out.println(row.mkString(",")));
        //df.show();
    });
}
 
Example 2
Source Project: beam   Source File: TranslationContext.java    License: Apache License 2.0 5 votes vote down vote up
/** Starts the pipeline. */
public void startPipeline() {
  try {
    SparkStructuredStreamingPipelineOptions options =
        serializablePipelineOptions.get().as(SparkStructuredStreamingPipelineOptions.class);
    int datasetIndex = 0;
    for (Dataset<?> dataset : leaves) {
      if (options.isStreaming()) {
        // TODO: deal with Beam Discarding, Accumulating and Accumulating & Retracting	outputmodes
        // with DatastreamWriter.outputMode
        DataStreamWriter<?> dataStreamWriter = dataset.writeStream();
        // spark sets a default checkpoint dir if not set.
        if (options.getCheckpointDir() != null) {
          dataStreamWriter =
              dataStreamWriter.option("checkpointLocation", options.getCheckpointDir());
        }
        // TODO: Do not await termination here.
        dataStreamWriter.foreach(new NoOpForeachWriter<>()).start().awaitTermination();
      } else {
        if (options.getTestMode()) {
          LOG.debug("**** dataset {} catalyst execution plans ****", ++datasetIndex);
          dataset.explain(true);
        }
        // apply a dummy fn just to apply foreach action that will trigger the pipeline run in
        // spark
        dataset.foreach((ForeachFunction) t -> {});
      }
    }
  } catch (StreamingQueryException e) {
    throw new RuntimeException("Pipeline execution failed: " + e);
  }
}