|
1 | 1 | package zingg.spark.core.preprocess.stopwords; |
2 | 2 |
|
3 | | -import static org.apache.spark.sql.functions.regexp_replace; |
| 3 | +import static org.apache.spark.sql.functions.callUDF; |
| 4 | +import static org.apache.spark.sql.functions.lit; |
4 | 5 |
|
5 | 6 | import java.io.Serializable; |
6 | 7 |
|
|
10 | 11 | import org.apache.spark.sql.Dataset; |
11 | 12 | import org.apache.spark.sql.Row; |
12 | 13 | import org.apache.spark.sql.types.DataType; |
| 14 | +import org.apache.spark.sql.types.DataTypes; |
13 | 15 |
|
14 | 16 | import zingg.common.client.FieldDefinition; |
15 | 17 | import zingg.common.client.ZFrame; |
16 | 18 | import zingg.common.core.context.IContext; |
17 | 19 | import zingg.common.core.preprocess.stopwords.StopWordsRemover; |
18 | 20 | import zingg.spark.client.SparkFrame; |
19 | 21 | import org.apache.spark.sql.SparkSession; |
| 22 | +import zingg.spark.core.util.SparkFnRegistrar; |
20 | 23 |
|
21 | 24 | public class SparkStopWordsRemover extends StopWordsRemover<SparkSession,Dataset<Row>,Row,Column,DataType> implements Serializable { |
22 | 25 |
|
23 | 26 | private static final long serialVersionUID = 1L; |
24 | 27 | protected static String name = "zingg.spark.preprocess.SparkStopWordsRemover"; |
25 | 28 | public static final Log LOG = LogFactory.getLog(SparkStopWordsRemover.class); |
| 29 | + |
| 30 | + private String udfName; |
26 | 31 |
|
27 | 32 | public SparkStopWordsRemover(){ |
28 | 33 | } |
29 | 34 |
|
30 | 35 | public SparkStopWordsRemover(IContext<SparkSession, Dataset<Row>, Row, Column,DataType> context) { |
31 | 36 | super(context); |
| 37 | + registerUDF(); |
32 | 38 | } |
33 | 39 |
|
34 | 40 | public SparkStopWordsRemover(IContext<SparkSession, Dataset<Row>, Row, Column,DataType> context, FieldDefinition fd) { |
35 | 41 | super(context,fd); |
| 42 | + registerUDF(); |
36 | 43 | } |
37 | 44 |
|
38 | 45 | @Override |
39 | 46 | protected ZFrame<Dataset<Row>, Row, Column> removeStopWordsFromDF(ZFrame<Dataset<Row>, Row, Column> ds, |
40 | 47 | String fieldName, String pattern) { |
41 | | - Dataset<Row> dfAfterRemoval = ds.df().withColumn(fieldName, regexp_replace(ds.df().col(fieldName), pattern, "")); |
| 48 | + Dataset<Row> dfAfterRemoval = ds.df().withColumn(fieldName,callUDF(udfName, ds.df().col(fieldName),lit(pattern))); |
42 | 49 |
|
43 | 50 | return new SparkFrame(dfAfterRemoval); |
44 | 51 | } |
45 | 52 |
|
| 53 | + protected void registerUDF() { |
| 54 | + RemoveStopWordsUDF removeStopWordsUDF = new RemoveStopWordsUDF(); |
| 55 | + // Each field will have different pattern |
| 56 | + this.udfName = removeStopWordsUDF.getName(); |
| 57 | + // register the UDF |
| 58 | + SparkSession zSession = getContext().getSession(); |
| 59 | + |
| 60 | + SparkFnRegistrar.registerUDF2(zSession, udfName, removeStopWordsUDF, DataTypes.StringType); |
| 61 | + } |
| 62 | + |
46 | 63 | @Override |
47 | 64 | public void init() { |
| 65 | + registerUDF(); |
48 | 66 | } |
49 | 67 |
|
50 | 68 | } |
0 commit comments