Created
May 14, 2025 13:54
-
-
Save mdrakiburrahman/8b92173383dc99d1204bfed4e1665db1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package microsoft.opentelemetrycollector.otlp.json.v1 | |
import org.apache.spark.sql.SparkSession | |
import org.apache.spark.sql.types.StructType | |
// @formatter:off | |
/** Represents a schema for a Logs payload. | |
* | |
* @param spark | |
* The SparkSession for schema inference. | |
* | |
* The source of truth is stored in the OpenTelemetry Protocol (OTLP) specification: | |
* | |
* >>> Protobuf: https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/logs/v1/logs.proto | |
* >>> Sample: https://github.com/open-telemetry/opentelemetry-proto/blob/main/examples/logs.json | |
*/ | |
// @formatter:on | |
class LogsSchema(spark: SparkSession) { | |
/** Sample data. | |
*/ | |
private val Sample = Seq( | |
("""{ | |
"resourceLogs":[ | |
{ | |
"resource":{ | |
"attributes":[ | |
{ | |
"key":"service.name", | |
"value":{ | |
"stringValue":"my.service" | |
} | |
} | |
] | |
}, | |
"scopeLogs":[ | |
{ | |
"scope":{ | |
"name":"my.library", | |
"version":"1.0.0", | |
"attributes":[ | |
{ | |
"key":"my.scope.attribute", | |
"value":{ | |
"stringValue":"some scope attribute" | |
} | |
} | |
] | |
}, | |
"logRecords":[ | |
{ | |
"timeUnixNano":"1544712660300000000", | |
"observedTimeUnixNano":"1544712660300000000", | |
"severityNumber":10, | |
"severityText":"Information", | |
"traceId":"5B8EFFF798038103D269B633813FC60C", | |
"spanId":"EEE19B7EC3C1B174", | |
"body":{ | |
"stringValue":"Example log record" | |
}, | |
"attributes":[ | |
{ | |
"key":"string.attribute", | |
"value":{ | |
"stringValue":"some string" | |
} | |
}, | |
{ | |
"key":"boolean.attribute", | |
"value":{ | |
"boolValue":true | |
} | |
}, | |
{ | |
"key":"int.attribute", | |
"value":{ | |
"intValue":"10" | |
} | |
}, | |
{ | |
"key":"double.attribute", | |
"value":{ | |
"doubleValue":637.704 | |
} | |
}, | |
{ | |
"key":"array.attribute", | |
"value":{ | |
"arrayValue":{ | |
"values":[ | |
{ | |
"stringValue":"many" | |
}, | |
{ | |
"stringValue":"values" | |
} | |
] | |
} | |
} | |
}, | |
{ | |
"key":"map.attribute", | |
"value":{ | |
"kvlistValue":{ | |
"values":[ | |
{ | |
"key":"some.map.key", | |
"value":{ | |
"stringValue":"some value" | |
} | |
} | |
] | |
} | |
} | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
""") | |
) | |
/** @inheritdoc | |
*/ | |
override val schema = { | |
import spark.implicits._ | |
StructType(spark.read.json(Sample.toDS).schema) | |
} | |
} | |
/** Companion object for [[LogsSchema]]. | |
*/ | |
object LogsSchema { | |
/** Constructor. | |
* | |
* @param spark | |
* The SparkSession for schema inference. | |
*/ | |
def apply(spark: SparkSession): LogsSchema = | |
new LogsSchema(spark) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Schema in Spark: