Search in sources :

Example 6 with JDBCOptions

use of org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions in project OpenLineage by OpenLineage.

the class LogicalRelationDatasetBuilderTest method testApply.

@ParameterizedTest
@ValueSource(strings = { "postgresql://postgreshost:5432/sparkdata", "jdbc:oracle:oci8:@sparkdata", "jdbc:oracle:thin@sparkdata:1521:orcl", "mysql://localhost/sparkdata" })
void testApply(String connectionUri) {
    OpenLineage openLineage = new OpenLineage(OpenLineageClient.OPEN_LINEAGE_CLIENT_URI);
    String jdbcUrl = "jdbc:" + connectionUri;
    String sparkTableName = "my_spark_table";
    JDBCRelation relation = new JDBCRelation(new StructType(new StructField[] { new StructField("name", StringType$.MODULE$, false, null) }), new Partition[] {}, new JDBCOptions(jdbcUrl, sparkTableName, Map$.MODULE$.<String, String>newBuilder().$plus$eq(Tuple2.apply("driver", Driver.class.getName())).result()), session);
    QueryExecution qe = mock(QueryExecution.class);
    when(qe.optimizedPlan()).thenReturn(new LogicalRelation(relation, Seq$.MODULE$.<AttributeReference>newBuilder().$plus$eq(new AttributeReference("name", StringType$.MODULE$, false, null, ExprId.apply(1L), Seq$.MODULE$.<String>empty())).result(), Option.empty(), false));
    OpenLineageContext context = OpenLineageContext.builder().sparkContext(mock(SparkContext.class)).openLineage(openLineage).queryExecution(qe).build();
    LogicalRelationDatasetBuilder visitor = new LogicalRelationDatasetBuilder<>(context, DatasetFactory.output(openLineage), false);
    List<OutputDataset> datasets = visitor.apply(new SparkListenerJobStart(1, 1, Seq$.MODULE$.empty(), null));
    assertEquals(1, datasets.size());
    OutputDataset ds = datasets.get(0);
    assertEquals(connectionUri, ds.getNamespace());
    assertEquals(sparkTableName, ds.getName());
    assertEquals(URI.create(connectionUri), ds.getFacets().getDataSource().getUri());
    assertEquals(connectionUri, ds.getFacets().getDataSource().getName());
}
Also used : StructType(org.apache.spark.sql.types.StructType) SparkListenerJobStart(org.apache.spark.scheduler.SparkListenerJobStart) AttributeReference(org.apache.spark.sql.catalyst.expressions.AttributeReference) JDBCRelation(org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation) Driver(org.postgresql.Driver) QueryExecution(org.apache.spark.sql.execution.QueryExecution) LogicalRelation(org.apache.spark.sql.execution.datasources.LogicalRelation) StructField(org.apache.spark.sql.types.StructField) JDBCOptions(org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions) OpenLineage(io.openlineage.client.OpenLineage) OutputDataset(io.openlineage.client.OpenLineage.OutputDataset) OpenLineageContext(io.openlineage.spark.api.OpenLineageContext) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

JDBCOptions (org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions)6 AttributeReference (org.apache.spark.sql.catalyst.expressions.AttributeReference)4 LogicalRelation (org.apache.spark.sql.execution.datasources.LogicalRelation)4 JDBCRelation (org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation)4 StructField (org.apache.spark.sql.types.StructField)4 StructType (org.apache.spark.sql.types.StructType)4 Test (org.junit.jupiter.api.Test)4 OpenLineage (io.openlineage.client.OpenLineage)3 Driver (org.postgresql.Driver)3 DatasetIdentifier (io.openlineage.spark.agent.util.DatasetIdentifier)2 SneakyThrows (lombok.SneakyThrows)2 SparkSession (org.apache.spark.sql.SparkSession)2 JDBCTableCatalog (org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog)2 Metadata (org.apache.spark.sql.types.Metadata)2 ImmutableMap (com.google.cloud.spark.bigquery.repackaged.com.google.common.collect.ImmutableMap)1 OutputDataset (io.openlineage.client.OpenLineage.OutputDataset)1 CreateHiveTableAsSelectCommandVisitor (io.openlineage.spark.agent.lifecycle.plan.CreateHiveTableAsSelectCommandVisitor)1 OptimizedCreateHiveTableAsSelectCommandVisitor (io.openlineage.spark.agent.lifecycle.plan.OptimizedCreateHiveTableAsSelectCommandVisitor)1 OpenLineageContext (io.openlineage.spark.api.OpenLineageContext)1 Path (java.nio.file.Path)1