Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port UDFs to Java #52

Merged
merged 2 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.highperformancespark.examples.dataframe;

import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.expressions.MutableAggregationBuffer;
import org.apache.spark.sql.expressions.UserDefinedAggregateFunction;
import org.apache.spark.sql.types.*;

public class JavaUDFs {

public static void setupUDFs(SQLContext sqlContext) {
sqlContext.udf().register("strlen", (String s) -> s.length(), DataTypes.StringType);
}

public static void setupUDAFs(SQLContext sqlContext) {

class Avg extends UserDefinedAggregateFunction {

@Override
public StructType inputSchema() {
StructType inputSchema =
new StructType(new StructField[]{new StructField("value", DataTypes.DoubleType, true, Metadata.empty())});
return inputSchema;
}

@Override
public StructType bufferSchema() {
StructType bufferSchema =
new StructType(new StructField[]{
new StructField("count", DataTypes.LongType, true, Metadata.empty()),
new StructField("sum", DataTypes.DoubleType, true, Metadata.empty())
});

return bufferSchema;
}

@Override
public DataType dataType() {
return DataTypes.DoubleType;
}

@Override
public boolean deterministic() {
return true;
}

@Override
public void initialize(MutableAggregationBuffer buffer) {
buffer.update(0, 0L);
buffer.update(1, 0.0);
}

@Override
public void update(MutableAggregationBuffer buffer, Row input) {
buffer.update(0, buffer.getLong(0) + 1);
buffer.update(1, buffer.getDouble(1) + input.getDouble(0));
}

@Override
public void merge(MutableAggregationBuffer buffer1, Row buffer2) {
buffer1.update(0, buffer1.getLong(0) + buffer2.getLong(0));
buffer1.update(1, buffer1.getDouble(1) + buffer2.getDouble(1));
}

@Override
public Object evaluate(Row buffer) {
return buffer.getDouble(1) / buffer.getLong(0);
}
}

Avg average = new Avg();
sqlContext.udf().register("ourAvg", average);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object UDFs {
}

def evaluate(buffer: Row): Any = {
math.pow(buffer.getDouble(1), 1.toDouble / buffer.getLong(0))
buffer.getDouble(1) / buffer.getLong(0)
}
}
// Optionally register
Expand Down