|
| 1 | +using Apache.Arrow.Ipc; |
| 2 | +using Apache.Arrow.Types; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using Parquet.Thrift; |
| 6 | +using System; |
| 7 | + |
| 8 | +namespace Utilities |
| 9 | +{ |
| 10 | + public static class ParquetMetadataAnalyzers |
| 11 | + { |
| 12 | + public static string ApacheArrowToJSON(string base64) |
| 13 | + { |
| 14 | + try |
| 15 | + { |
| 16 | + byte[] bytes = Convert.FromBase64String(base64); |
| 17 | + using (ArrowStreamReader reader = new ArrowStreamReader(bytes)) |
| 18 | + { |
| 19 | + reader.ReadNextRecordBatch(); |
| 20 | + return JsonConvert.SerializeObject(reader.Schema, Formatting.Indented); |
| 21 | + |
| 22 | + var metadata = new JObject(); |
| 23 | + var schema = new JObject(); |
| 24 | + |
| 25 | + var fields = new JArray(); |
| 26 | + if (reader.Schema?.Fields != null) |
| 27 | + { |
| 28 | + foreach (var _field in reader.Schema.Fields) |
| 29 | + { |
| 30 | + var field = new JObject(); |
| 31 | + field[nameof(_field.Value.Name)] = _field.Value.Name; |
| 32 | + field[nameof(_field.Value.IsNullable)] = _field.Value.IsNullable; |
| 33 | + field[nameof(_field.Value.DataType)] = JObject.Parse(JsonConvert.SerializeObject(_field.Value.DataType)); |
| 34 | + |
| 35 | + if (_field.Value.HasMetadata) |
| 36 | + { |
| 37 | + metadata = new JObject(); |
| 38 | + foreach (var _fieldMetadata in _field.Value.Metadata) |
| 39 | + { |
| 40 | + metadata[_fieldMetadata.Key] = _fieldMetadata.Value; |
| 41 | + } |
| 42 | + field[nameof(metadata)] = metadata; |
| 43 | + } |
| 44 | + |
| 45 | + fields.Add(field); |
| 46 | + } |
| 47 | + } |
| 48 | + schema[nameof(fields)] = fields; |
| 49 | + |
| 50 | + metadata = new JObject(); |
| 51 | + if (reader.Schema?.Metadata != null) |
| 52 | + { |
| 53 | + foreach (var _metadata in reader.Schema.Metadata) |
| 54 | + { |
| 55 | + metadata[_metadata.Key] = _metadata.Value; |
| 56 | + } |
| 57 | + } |
| 58 | + schema[nameof(metadata)] = metadata; |
| 59 | + |
| 60 | + return schema.ToString(Formatting.Indented); |
| 61 | + } |
| 62 | + } |
| 63 | + catch (Exception ex) |
| 64 | + { |
| 65 | + return $"Something went wrong while processing the schema:{Environment.NewLine}{Environment.NewLine}{ex.ToString()}"; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static string ThriftMetadataToJSON(FileMetaData thriftMetadata) |
| 70 | + { |
| 71 | + try |
| 72 | + { |
| 73 | + var jsonObject = new JObject(); |
| 74 | + jsonObject[nameof(thriftMetadata.Version)] = thriftMetadata.Version; |
| 75 | + jsonObject[nameof(thriftMetadata.Num_rows)] = thriftMetadata.Num_rows; |
| 76 | + jsonObject[nameof(thriftMetadata.Created_by)] = thriftMetadata.Created_by; |
| 77 | + |
| 78 | + var schemas = new JArray(); |
| 79 | + foreach (var schema in thriftMetadata.Schema) |
| 80 | + { |
| 81 | + if ("schema".Equals(schema.Name) && schemas.Count == 0) |
| 82 | + continue; |
| 83 | + |
| 84 | + var schemaObject = new JObject(); |
| 85 | + schemaObject[nameof(schema.Field_id)] = schema.Field_id; |
| 86 | + schemaObject[nameof(schema.Name)] = schema.Name; |
| 87 | + schemaObject[nameof(schema.Type)] = schema.Type.ToString(); |
| 88 | + schemaObject[nameof(schema.Type_length)] = schema.Type_length; |
| 89 | + schemaObject[nameof(schema.LogicalType)] = schema.LogicalType?.ToString(); |
| 90 | + schemaObject[nameof(schema.Scale)] = schema.Scale; |
| 91 | + schemaObject[nameof(schema.Precision)] = schema.Precision; |
| 92 | + schemaObject[nameof(schema.Repetition_type)] = schema.Repetition_type.ToString(); |
| 93 | + schemaObject[nameof(schema.Converted_type)] = schema.Converted_type.ToString(); |
| 94 | + |
| 95 | + schemas.Add(schemaObject); |
| 96 | + } |
| 97 | + jsonObject[nameof(thriftMetadata.Schema)] = schemas; |
| 98 | + |
| 99 | + return jsonObject.ToString(Formatting.Indented); |
| 100 | + } |
| 101 | + catch (Exception ex) |
| 102 | + { |
| 103 | + return $"Something went wrong while processing the schema:{Environment.NewLine}{Environment.NewLine}{ex.ToString()}"; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public static string PandasSchemaToJSON(string pandas) |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + //Pandas is already json; so just make it pretty. |
| 112 | + return JValue.Parse(pandas).ToString(Formatting.Indented); |
| 113 | + } |
| 114 | + catch (Exception) |
| 115 | + { |
| 116 | + //malformed json detected |
| 117 | + return pandas; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments