Skip to content

Commit

Permalink
Apply suggestions from @AnastasiiaSergienko
Browse files Browse the repository at this point in the history
  • Loading branch information
morazow committed Oct 28, 2020
1 parent b63f292 commit 27b5f79
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/main/scala/com/exasol/avro/AvroRow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@ import org.apache.avro.util.Utf8
object AvroRow {

/**
* Converts an Avro record into an internal {@link Row}.
* Converts an Avro record into an internal [[com.exasol.common.data.Row]].
*
* @param avroRecord a generic Avro record
* @return a Row representation of the given Avro record
*/
def apply(avroRecord: GenericRecord): Row = {
val size = avroRecord.getSchema.getFields.size
val values = Array.ofDim[Any](size)
val fields = avroRecord.getSchema().getFields()
for { index <- 0 until fields.size } {
val fieldSchema = fields.get(index).schema()
val fieldValue = getAvroValue(avroRecord.get(index), fieldSchema)
if (isPrimitiveAvroType(fieldSchema.getType())) {
values.update(index, fieldValue)
} else {
values.update(index, JsonMapper.toJson(fieldValue))
}
val size = fields.size()
val values = Array.ofDim[Any](size)
for { i <- 0 until size } {
values.update(i, getAvroFieldValue(fields.get(i).schema(), avroRecord.get(i)))
}
Row(values.toSeq)
}

private[this] def getAvroFieldValue(schema: Schema, value: Any): Any = {
val fieldValue = getAvroValue(value, schema)
if (isPrimitiveAvroType(schema.getType())) {
fieldValue
} else {
JsonMapper.toJson(fieldValue)
}
}

private[this] def isPrimitiveAvroType(avroType: Schema.Type): Boolean =
avroType match {
case Schema.Type.ARRAY => false
Expand Down

0 comments on commit 27b5f79

Please sign in to comment.