-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial supplied schema validation to prevent inconsistency betwe…
…en supplied schema (expected data format) and an actual data format, returned by a SQL query. Reorganize some code to make locations more logical. Always use generated Avro schema. Optional user provided schema used for `doc` fields retrieval.
- Loading branch information
Ruslan Altynnikov
committed
Feb 11, 2022
1 parent
2315e22
commit bcd97b7
Showing
14 changed files
with
485 additions
and
128 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
83 changes: 83 additions & 0 deletions
83
dbeam-core/src/main/java/com/spotify/dbeam/avro/AvroSchemaMetadataProvider.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/*- | ||
* -\-\- | ||
* DBeam Core | ||
* -- | ||
* Copyright (C) 2016 - 2018 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.dbeam.avro; | ||
|
||
import org.apache.avro.Schema; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class AvroSchemaMetadataProvider { | ||
|
||
private static Logger LOGGER = LoggerFactory.getLogger(AvroSchemaMetadataProvider.class); | ||
|
||
// provided schema has a priority over single arguments' values. | ||
private final Schema provided; | ||
private final String avroSchemaName; | ||
private final String avroSchemaNamespace; | ||
private final String avroDoc; | ||
|
||
public AvroSchemaMetadataProvider( | ||
final String avroSchemaName, final String avroSchemaNamespace, final String avroDoc) { | ||
this(null, avroSchemaName, avroSchemaNamespace, avroDoc); | ||
} | ||
|
||
public AvroSchemaMetadataProvider( | ||
final Schema provided, | ||
final String avroSchemaName, | ||
final String avroSchemaNamespace, | ||
final String avroDoc) { | ||
this.provided = provided; | ||
this.avroSchemaName = avroSchemaName; | ||
this.avroSchemaNamespace = avroSchemaNamespace; | ||
this.avroDoc = avroDoc; | ||
} | ||
|
||
public String avroDoc(final String defaultVal) { | ||
return (provided != null) ? provided.getDoc() : (avroDoc != null) ? avroDoc : defaultVal; | ||
} | ||
|
||
public String avroSchemaName(final String defaultVal) { | ||
if (provided != null) { | ||
String name = provided.getName(); | ||
return (name != null) ? name : defaultVal; | ||
} else { | ||
return avroSchemaName != null ? avroSchemaName : defaultVal; | ||
} | ||
} | ||
|
||
public String avroSchemaNamespace() { | ||
return (provided != null) ? provided.getNamespace() : avroSchemaNamespace; | ||
} | ||
|
||
public String getFieldDoc(final String fieldName, final String defaultVal) { | ||
if (provided != null) { | ||
final Schema.Field field = provided.getField(fieldName); | ||
if (field != null) { | ||
return field.doc(); | ||
} else { | ||
LOGGER.warn("Field [{}] not found in a provided schema", fieldName); | ||
return defaultVal; | ||
} | ||
} else { | ||
return defaultVal; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.