use of io.cdap.cdap.etl.spark.function.CountingFunction in project cdap by caskdata.
the class BaseRDDCollection method compute.
@Override
public <U> SparkCollection<U> compute(StageSpec stageSpec, SparkCompute<T, U> compute) throws Exception {
String stageName = stageSpec.getName();
PipelineRuntime pipelineRuntime = new SparkPipelineRuntime(sec);
SparkExecutionPluginContext sparkPluginContext = new BasicSparkExecutionPluginContext(sec, jsc, datasetContext, pipelineRuntime, stageSpec);
compute.initialize(sparkPluginContext);
JavaRDD<T> countedInput = rdd.map(new CountingFunction<T>(stageName, sec.getMetrics(), Constants.Metrics.RECORDS_IN, null));
SparkConf sparkConf = jsc.getConf();
return wrap(compute.transform(sparkPluginContext, countedInput).map(new CountingFunction<U>(stageName, sec.getMetrics(), Constants.Metrics.RECORDS_OUT, sec.getDataTracer(stageName))));
}
use of io.cdap.cdap.etl.spark.function.CountingFunction in project cdap by caskdata.
the class RDDCollection method join.
@SuppressWarnings("unchecked")
@Override
public SparkCollection<T> join(JoinRequest joinRequest) {
Map<String, Dataset> collections = new HashMap<>();
String stageName = joinRequest.getStageName();
Function<StructuredRecord, StructuredRecord> recordsInCounter = new CountingFunction<>(stageName, sec.getMetrics(), Constants.Metrics.RECORDS_IN, sec.getDataTracer(stageName));
StructType leftSparkSchema = DataFrames.toDataType(joinRequest.getLeftSchema());
Dataset<Row> left = toDataset(((JavaRDD<StructuredRecord>) rdd).map(recordsInCounter), leftSparkSchema);
collections.put(joinRequest.getLeftStage(), left);
List<Column> leftJoinColumns = joinRequest.getLeftKey().stream().map(left::col).collect(Collectors.toList());
/*
This flag keeps track of whether there is at least one required stage in the join.
This is needed in case there is a join like:
A (optional), B (required), C (optional), D (required)
The correct thing to do here is:
1. A right outer join B as TMP1
2. TMP1 left outer join C as TMP2
3. TMP2 inner join D
Join #1 is a straightforward join between 2 sides.
Join #2 is a left outer because TMP1 becomes 'required', since it uses required input B.
Join #3 is an inner join even though it contains 2 optional datasets, because 'B' is still required.
*/
Integer joinPartitions = joinRequest.getNumPartitions();
boolean seenRequired = joinRequest.isLeftRequired();
Dataset<Row> joined = left;
List<List<Column>> listOfListOfLeftCols = new ArrayList<>();
for (JoinCollection toJoin : joinRequest.getToJoin()) {
SparkCollection<StructuredRecord> data = (SparkCollection<StructuredRecord>) toJoin.getData();
StructType sparkSchema = DataFrames.toDataType(toJoin.getSchema());
Dataset<Row> right = toDataset(((JavaRDD<StructuredRecord>) data.getUnderlying()).map(recordsInCounter), sparkSchema);
collections.put(toJoin.getStage(), right);
List<Column> rightJoinColumns = toJoin.getKey().stream().map(right::col).collect(Collectors.toList());
// UUID for salt column name to avoid name collisions
String saltColumn = UUID.randomUUID().toString();
if (joinRequest.isDistributionEnabled()) {
boolean isLeftStageSkewed = joinRequest.getLeftStage().equals(joinRequest.getDistribution().getSkewedStageName());
// Apply salt/explode transformations to each Dataset
if (isLeftStageSkewed) {
left = saltDataset(left, saltColumn, joinRequest.getDistribution().getDistributionFactor());
right = explodeDataset(right, saltColumn, joinRequest.getDistribution().getDistributionFactor());
} else {
left = explodeDataset(left, saltColumn, joinRequest.getDistribution().getDistributionFactor());
right = saltDataset(right, saltColumn, joinRequest.getDistribution().getDistributionFactor());
}
// Add the salt column to the join key
leftJoinColumns.add(left.col(saltColumn));
rightJoinColumns.add(right.col(saltColumn));
// Updating other values that will be used later in join
joined = left;
sparkSchema = sparkSchema.add(saltColumn, DataTypes.IntegerType, false);
leftSparkSchema = leftSparkSchema.add(saltColumn, DataTypes.IntegerType, false);
}
Column joinOn;
// Making effectively final to use in streams
List<Column> finalLeftJoinColumns = leftJoinColumns;
if (seenRequired) {
joinOn = IntStream.range(0, leftJoinColumns.size()).mapToObj(i -> eq(finalLeftJoinColumns.get(i), rightJoinColumns.get(i), joinRequest.isNullSafe())).reduce((a, b) -> a.and(b)).get();
} else {
// For the case when all joins are outer. Collect left keys at each level (each iteration)
// coalesce these keys at each level and compare with right
joinOn = IntStream.range(0, leftJoinColumns.size()).mapToObj(i -> {
collectLeftJoinOnCols(listOfListOfLeftCols, i, finalLeftJoinColumns.get(i));
return eq(getLeftJoinOnCoalescedColumn(finalLeftJoinColumns.get(i), i, listOfListOfLeftCols), rightJoinColumns.get(i), joinRequest.isNullSafe());
}).reduce((a, b) -> a.and(b)).get();
}
String joinType;
if (seenRequired && toJoin.isRequired()) {
joinType = "inner";
} else if (seenRequired && !toJoin.isRequired()) {
joinType = "leftouter";
} else if (!seenRequired && toJoin.isRequired()) {
joinType = "rightouter";
} else {
joinType = "outer";
}
seenRequired = seenRequired || toJoin.isRequired();
if (toJoin.isBroadcast()) {
right = functions.broadcast(right);
}
// we are forced to with spark.cdap.pipeline.aggregate.dataset.partitions.ignore = false
if (!ignorePartitionsDuringDatasetAggregation && joinPartitions != null && !toJoin.isBroadcast()) {
List<String> rightKeys = new ArrayList<>(toJoin.getKey());
List<String> leftKeys = new ArrayList<>(joinRequest.getLeftKey());
// number of partitions
if (joinRequest.isDistributionEnabled()) {
rightKeys.add(saltColumn);
leftKeys.add(saltColumn);
}
right = partitionOnKey(right, rightKeys, joinRequest.isNullSafe(), sparkSchema, joinPartitions);
// as intermediate joins will already be partitioned on the key
if (joined == left) {
joined = partitionOnKey(joined, leftKeys, joinRequest.isNullSafe(), leftSparkSchema, joinPartitions);
}
}
joined = joined.join(right, joinOn, joinType);
/*
Additionally if none of the datasets are required until now, which means all of the joines will outer.
In this case also we need to pass on the join columns as we need to compare using coalesce of all previous
columns with the right dataset
*/
if (toJoin.isRequired() || !seenRequired) {
leftJoinColumns = rightJoinColumns;
}
}
// select and alias fields in the expected order
List<Column> outputColumns = new ArrayList<>(joinRequest.getFields().size());
for (JoinField field : joinRequest.getFields()) {
Column column = collections.get(field.getStageName()).col(field.getFieldName());
if (field.getAlias() != null) {
column = column.alias(field.getAlias());
}
outputColumns.add(column);
}
Seq<Column> outputColumnSeq = JavaConversions.asScalaBuffer(outputColumns).toSeq();
joined = joined.select(outputColumnSeq);
Schema outputSchema = joinRequest.getOutputSchema();
JavaRDD<StructuredRecord> output = joined.javaRDD().map(r -> DataFrames.fromRow(r, outputSchema)).map(new CountingFunction<>(stageName, sec.getMetrics(), Constants.Metrics.RECORDS_OUT, sec.getDataTracer(stageName)));
return (SparkCollection<T>) wrap(output);
}
use of io.cdap.cdap.etl.spark.function.CountingFunction in project cdap by caskdata.
the class RDDCollection method join.
@SuppressWarnings("unchecked")
@Override
public SparkCollection<T> join(JoinExpressionRequest joinRequest) {
Function<StructuredRecord, StructuredRecord> recordsInCounter = new CountingFunction<>(joinRequest.getStageName(), sec.getMetrics(), Constants.Metrics.RECORDS_IN, sec.getDataTracer(joinRequest.getStageName()));
JoinCollection leftInfo = joinRequest.getLeft();
StructType leftSchema = DataFrames.toDataType(leftInfo.getSchema());
Dataset<Row> leftDF = toDataset(((JavaRDD<StructuredRecord>) rdd).map(recordsInCounter), leftSchema);
JoinCollection rightInfo = joinRequest.getRight();
SparkCollection<?> rightData = rightInfo.getData();
StructType rightSchema = DataFrames.toDataType(rightInfo.getSchema());
Dataset<Row> rightDF = toDataset(((JavaRDD<StructuredRecord>) rightData.getUnderlying()).map(recordsInCounter), rightSchema);
// if this is not a broadcast join, Spark will reprocess each side multiple times, depending on the number
// of partitions. If the left side has N partitions and the right side has M partitions,
// the left side gets reprocessed M times and the right side gets reprocessed N times.
// Cache the input to prevent confusing metrics and potential source re-reading.
// this is only necessary for inner joins, since outer joins are automatically changed to
// BroadcastNestedLoopJoins by Spark
boolean isInner = joinRequest.getLeft().isRequired() && joinRequest.getRight().isRequired();
boolean isBroadcast = joinRequest.getLeft().isBroadcast() || joinRequest.getRight().isBroadcast();
if (isInner && !isBroadcast) {
leftDF = leftDF.persist(StorageLevel.DISK_ONLY());
rightDF = rightDF.persist(StorageLevel.DISK_ONLY());
}
// register using unique names to avoid collisions.
String leftId = UUID.randomUUID().toString().replaceAll("-", "");
String rightId = UUID.randomUUID().toString().replaceAll("-", "");
leftDF.registerTempTable(leftId);
rightDF.registerTempTable(rightId);
/*
Suppose the join was originally:
select P.id as id, users.name as username
from purchases as P join users
on P.user_id = users.id or P.user_id = 0
After registering purchases as uuid0 and users as uuid1,
the query needs to be rewritten to replace the original names with the new generated ids,
as the query needs to be:
select P.id as id, uuid1.name as username
from uuid0 as P join uuid1
on P.user_id = uuid1.id or P.user_id = 0
*/
String sql = getSQL(joinRequest.rename(leftId, rightId));
LOG.debug("Executing join stage {} using SQL: \n{}", joinRequest.getStageName(), sql);
Dataset<Row> joined = sqlContext.sql(sql);
Schema outputSchema = joinRequest.getOutputSchema();
JavaRDD<StructuredRecord> output = joined.javaRDD().map(r -> DataFrames.fromRow(r, outputSchema)).map(new CountingFunction<>(joinRequest.getStageName(), sec.getMetrics(), Constants.Metrics.RECORDS_OUT, sec.getDataTracer(joinRequest.getStageName())));
return (SparkCollection<T>) wrap(output);
}
use of io.cdap.cdap.etl.spark.function.CountingFunction in project cdap by caskdata.
the class ComputeTransformFunction method call.
@Override
public JavaRDD<U> call(JavaRDD<T> data, Time batchTime) throws Exception {
SparkExecutionPluginContext sparkPluginContext = new SparkStreamingExecutionContext(sec, JavaSparkContext.fromSparkContext(data.context()), batchTime.milliseconds(), stageSpec);
String stageName = stageSpec.getName();
data = data.map(new CountingFunction<T>(stageName, sec.getMetrics(), "records.in", null));
return compute.transform(sparkPluginContext, data).map(new CountingFunction<U>(stageName, sec.getMetrics(), "records.out", sec.getDataTracer(stageName)));
}
use of io.cdap.cdap.etl.spark.function.CountingFunction in project cdap by caskdata.
the class StreamingSparkSinkFunction method call.
@Override
public void call(JavaRDD<T> data, Time batchTime) throws Exception {
if (data.isEmpty()) {
return;
}
final long logicalStartTime = batchTime.milliseconds();
MacroEvaluator evaluator = new DefaultMacroEvaluator(new BasicArguments(sec), logicalStartTime, sec.getSecureStore(), sec.getServiceDiscoverer(), sec.getNamespace());
final PluginContext pluginContext = new SparkPipelinePluginContext(sec.getPluginContext(), sec.getMetrics(), stageSpec.isStageLoggingEnabled(), stageSpec.isProcessTimingEnabled());
final PipelineRuntime pipelineRuntime = new SparkPipelineRuntime(sec, batchTime.milliseconds());
final String stageName = stageSpec.getName();
final SparkSink<T> sparkSink = pluginContext.newPluginInstance(stageName, evaluator);
boolean isPrepared = false;
boolean isDone = false;
try {
sec.execute(new TxRunnable() {
@Override
public void run(DatasetContext datasetContext) throws Exception {
SparkPluginContext context = new BasicSparkPluginContext(null, pipelineRuntime, stageSpec, datasetContext, sec.getAdmin());
sparkSink.prepareRun(context);
}
});
isPrepared = true;
final SparkExecutionPluginContext sparkExecutionPluginContext = new SparkStreamingExecutionContext(sec, JavaSparkContext.fromSparkContext(data.rdd().context()), logicalStartTime, stageSpec);
final JavaRDD<T> countedRDD = data.map(new CountingFunction<T>(stageName, sec.getMetrics(), "records.in", null)).cache();
sec.execute(new TxRunnable() {
@Override
public void run(DatasetContext context) throws Exception {
sparkSink.run(sparkExecutionPluginContext, countedRDD);
}
});
isDone = true;
sec.execute(new TxRunnable() {
@Override
public void run(DatasetContext datasetContext) throws Exception {
SparkPluginContext context = new BasicSparkPluginContext(null, pipelineRuntime, stageSpec, datasetContext, sec.getAdmin());
sparkSink.onRunFinish(true, context);
}
});
} catch (Exception e) {
LOG.error("Error while executing sink {} for the batch for time {}.", stageName, logicalStartTime, e);
} finally {
if (isPrepared && !isDone) {
sec.execute(new TxRunnable() {
@Override
public void run(DatasetContext datasetContext) throws Exception {
SparkPluginContext context = new BasicSparkPluginContext(null, pipelineRuntime, stageSpec, datasetContext, sec.getAdmin());
sparkSink.onRunFinish(false, context);
}
});
}
}
}
Aggregations