|
| 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 System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using Microsoft.Spark.Interop; |
| 8 | +using Microsoft.Spark.Interop.Ipc; |
| 9 | + |
| 10 | +namespace Microsoft.Spark.Sql.Avro |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Functions for serialization and deserialization of data in Avro format. |
| 14 | + /// </summary> |
| 15 | + public static class Functions |
| 16 | + { |
| 17 | + private static IJvmBridge Jvm { get; } = SparkEnvironment.JvmBridge; |
| 18 | + private static readonly Lazy<string> s_avroClassName = |
| 19 | + new Lazy<string>(() => |
| 20 | + { |
| 21 | + Version sparkVersion = SparkEnvironment.SparkVersion; |
| 22 | + return sparkVersion.Major switch |
| 23 | + { |
| 24 | + 2 => "org.apache.spark.sql.avro.package", |
| 25 | + 3 => "org.apache.spark.sql.avro.functions", |
| 26 | + _ => throw new NotSupportedException($"Spark {sparkVersion} not supported.") |
| 27 | + }; |
| 28 | + }); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Converts a binary column of avro format into its corresponding catalyst value. The specified |
| 32 | + /// schema must match the read data, otherwise the behavior is undefined: it may fail or return |
| 33 | + /// arbitrary result. |
| 34 | + /// </summary> |
| 35 | + /// <param name="data">The binary column.</param> |
| 36 | + /// <param name="jsonFormatSchema">The avro schema in JSON string format.</param> |
| 37 | + /// <returns>Column object</returns> |
| 38 | + [Since(Versions.V2_4_0)] |
| 39 | + public static Column FromAvro(Column data, string jsonFormatSchema) => |
| 40 | + new Column( |
| 41 | + (JvmObjectReference)Jvm.CallStaticJavaMethod( |
| 42 | + s_avroClassName.Value, |
| 43 | + "from_avro", |
| 44 | + data, |
| 45 | + jsonFormatSchema)); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Converts a binary column of avro format into its corresponding catalyst value. The specified |
| 49 | + /// schema must match the read data, otherwise the behavior is undefined: it may fail or return |
| 50 | + /// arbitrary result. To deserialize the data with a compatible and evolved schema, the expected Avro |
| 51 | + /// schema can be set via the option avroSchema. |
| 52 | + /// </summary> |
| 53 | + /// <param name="data">The binary column.</param> |
| 54 | + /// <param name="jsonFormatSchema">The avro schema in JSON string format.</param> |
| 55 | + /// <param name="options">Options to control how the Avro record is parsed.</param> |
| 56 | + /// <returns>Column object</returns> |
| 57 | + [Since(Versions.V3_0_0)] |
| 58 | + public static Column FromAvro( |
| 59 | + Column data, |
| 60 | + string jsonFormatSchema, |
| 61 | + Dictionary<string, string> options) => |
| 62 | + new Column( |
| 63 | + (JvmObjectReference)Jvm.CallStaticJavaMethod( |
| 64 | + s_avroClassName.Value, |
| 65 | + "from_avro", |
| 66 | + data, |
| 67 | + jsonFormatSchema, |
| 68 | + options)); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Converts a column into binary of avro format. |
| 72 | + /// </summary> |
| 73 | + /// <param name="data">The data column.</param> |
| 74 | + /// <returns>Column object</returns> |
| 75 | + [Since(Versions.V2_4_0)] |
| 76 | + public static Column ToAvro(Column data) => |
| 77 | + new Column((JvmObjectReference)Jvm.CallStaticJavaMethod(s_avroClassName.Value, "to_avro", data)); |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Converts a column into binary of avro format. |
| 81 | + /// </summary> |
| 82 | + /// <param name="data">The data column.</param> |
| 83 | + /// <param name="jsonFormatSchema">User-specified output avro schema in JSON string format.</param> |
| 84 | + /// <returns>Column object</returns> |
| 85 | + [Since(Versions.V3_0_0)] |
| 86 | + public static Column ToAvro(Column data, string jsonFormatSchema) => |
| 87 | + new Column( |
| 88 | + (JvmObjectReference)Jvm.CallStaticJavaMethod( |
| 89 | + s_avroClassName.Value, |
| 90 | + "to_avro", |
| 91 | + data, |
| 92 | + jsonFormatSchema)); |
| 93 | + } |
| 94 | +} |
0 commit comments