|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.Spark.Interop; |
| 6 | +using Microsoft.Spark.Interop.Ipc; |
| 7 | +using Microsoft.Spark.Sql; |
| 8 | +using Microsoft.Spark.Sql.Types; |
| 9 | + |
| 10 | +namespace Microsoft.Spark.ML.Feature |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// <see cref="SQLTransformer"/> implements the transformations which are defined by SQL statement. |
| 14 | + /// </summary> |
| 15 | + public class SQLTransformer : FeatureBase<SQLTransformer>, IJvmObjectReferenceProvider |
| 16 | + { |
| 17 | + private static readonly string s_sqlTransformerClassName = |
| 18 | + "org.apache.spark.ml.feature.SQLTransformer"; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Create a <see cref="SQLTransformer"/> without any parameters. |
| 22 | + /// </summary> |
| 23 | + public SQLTransformer() : base(s_sqlTransformerClassName) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Create a <see cref="SQLTransformer"/> with a UID that is used to give the |
| 29 | + /// <see cref="SQLTransformer"/> a unique ID. |
| 30 | + /// </summary> |
| 31 | + /// <param name="uid">An immutable unique ID for the object and its derivatives.</param> |
| 32 | + public SQLTransformer(string uid) : base(s_sqlTransformerClassName, uid) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + internal SQLTransformer(JvmObjectReference jvmObject) : base(jvmObject) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + JvmObjectReference IJvmObjectReferenceProvider.Reference => _jvmObject; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Executes the <see cref="SQLTransformer"/> and transforms the DataFrame to include the new |
| 44 | + /// column. |
| 45 | + /// </summary> |
| 46 | + /// <param name="source">The DataFrame to transform</param> |
| 47 | + /// <returns> |
| 48 | + /// New <see cref="DataFrame"/> object with the source <see cref="DataFrame"/> transformed. |
| 49 | + /// </returns> |
| 50 | + public DataFrame Transform(DataFrame source) => |
| 51 | + new DataFrame((JvmObjectReference)_jvmObject.Invoke("transform", source)); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Executes the <see cref="SQLTransformer"/> and transforms the schema. |
| 55 | + /// </summary> |
| 56 | + /// <param name="value">The Schema to be transformed</param> |
| 57 | + /// <returns> |
| 58 | + /// New <see cref="StructType"/> object with the schema <see cref="StructType"/> transformed. |
| 59 | + /// </returns> |
| 60 | + public StructType TransformSchema(StructType value) => |
| 61 | + new StructType( |
| 62 | + (JvmObjectReference)_jvmObject.Invoke( |
| 63 | + "transformSchema", |
| 64 | + DataType.FromJson(_jvmObject.Jvm, value.Json))); |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Gets the statement. |
| 68 | + /// </summary> |
| 69 | + /// <returns>Statement</returns> |
| 70 | + public string GetStatement() => (string)_jvmObject.Invoke("getStatement"); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Sets the statement to <see cref="SQLTransformer"/>. |
| 74 | + /// </summary> |
| 75 | + /// <param name="statement">SQL Statement</param> |
| 76 | + /// <returns> |
| 77 | + /// <see cref="SQLTransformer"/> with the statement set. |
| 78 | + /// </returns> |
| 79 | + public SQLTransformer SetStatement(string statement) => |
| 80 | + WrapAsSQLTransformer((JvmObjectReference)_jvmObject.Invoke("setStatement", statement)); |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Loads the <see cref="SQLTransformer"/> that was previously saved using Save. |
| 84 | + /// </summary> |
| 85 | + /// <param name="path">The path the previous <see cref="SQLTransformer"/> was saved to</param> |
| 86 | + /// <returns>New <see cref="SQLTransformer"/> object, loaded from path</returns> |
| 87 | + public static SQLTransformer Load(string path) => |
| 88 | + WrapAsSQLTransformer( |
| 89 | + SparkEnvironment.JvmBridge.CallStaticJavaMethod( |
| 90 | + s_sqlTransformerClassName, |
| 91 | + "load", |
| 92 | + path)); |
| 93 | + |
| 94 | + private static SQLTransformer WrapAsSQLTransformer(object obj) => |
| 95 | + new SQLTransformer((JvmObjectReference)obj); |
| 96 | + } |
| 97 | +} |
0 commit comments