|
| 1 | +package org.neo4j.spark.converter |
| 2 | + |
| 3 | +import org.apache.spark.sql.catalyst.InternalRow |
| 4 | +import org.apache.spark.sql.catalyst.expressions.{GenericRow, GenericRowWithSchema, UnsafeRow} |
| 5 | +import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, ArrayData, DateTimeUtils, MapData} |
| 6 | +import org.apache.spark.sql.types._ |
| 7 | +import org.apache.spark.unsafe.types.UTF8String |
| 8 | +import org.neo4j.driver.internal._ |
| 9 | +import org.neo4j.driver.types.{IsoDuration, Node, Relationship} |
| 10 | +import org.neo4j.driver.{Value, Values} |
| 11 | +import org.neo4j.spark.service.SchemaService |
| 12 | +import org.neo4j.spark.util.Neo4jUtil |
| 13 | + |
| 14 | +import java.time._ |
| 15 | +import java.time.format.DateTimeFormatter |
| 16 | +import scala.annotation.tailrec |
| 17 | +import scala.collection.JavaConverters._ |
| 18 | + |
| 19 | +trait DataConverter[T] { |
| 20 | + def convert(value: Any, dataType: DataType = null): T |
| 21 | + |
| 22 | + @tailrec |
| 23 | + private[converter] final def extractStructType(dataType: DataType): StructType = dataType match { |
| 24 | + case structType: StructType => structType |
| 25 | + case mapType: MapType => extractStructType(mapType.valueType) |
| 26 | + case arrayType: ArrayType => extractStructType(arrayType.elementType) |
| 27 | + case _ => throw new UnsupportedOperationException(s"$dataType not supported") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +object SparkToNeo4jDataConverter { |
| 32 | + def apply(): SparkToNeo4jDataConverter = new SparkToNeo4jDataConverter() |
| 33 | +} |
| 34 | + |
| 35 | +class SparkToNeo4jDataConverter extends DataConverter[Value] { |
| 36 | + override def convert(value: Any, dataType: DataType): Value = { |
| 37 | + value match { |
| 38 | + case date: java.sql.Date => convert(date.toLocalDate, dataType) |
| 39 | + case timestamp: java.sql.Timestamp => convert(timestamp.toLocalDateTime, dataType) |
| 40 | + case intValue: Int if dataType == DataTypes.DateType => convert(DateTimeUtils |
| 41 | + .toJavaDate(intValue), dataType) |
| 42 | + case longValue: Long if dataType == DataTypes.TimestampType => convert(DateTimeUtils |
| 43 | + .toJavaTimestamp(longValue), dataType) |
| 44 | + case unsafeRow: UnsafeRow => { |
| 45 | + val structType = extractStructType(dataType) |
| 46 | + val row = new GenericRowWithSchema(unsafeRow.toSeq(structType).toArray, structType) |
| 47 | + convert(row) |
| 48 | + } |
| 49 | + case struct: GenericRow => { |
| 50 | + def toMap(struct: GenericRow): Value = { |
| 51 | + Values.value( |
| 52 | + struct.schema.fields.map( |
| 53 | + f => f.name -> convert(struct.getAs(f.name), f.dataType) |
| 54 | + ).toMap.asJava) |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + struct.getAs[UTF8String]("type").toString match { |
| 59 | + case SchemaService.POINT_TYPE_2D => Values.point(struct.getAs[Number]("srid").intValue(), |
| 60 | + struct.getAs[Number]("x").doubleValue(), |
| 61 | + struct.getAs[Number]("y").doubleValue()) |
| 62 | + case SchemaService.POINT_TYPE_3D => Values.point(struct.getAs[Number]("srid").intValue(), |
| 63 | + struct.getAs[Number]("x").doubleValue(), |
| 64 | + struct.getAs[Number]("y").doubleValue(), |
| 65 | + struct.getAs[Number]("z").doubleValue()) |
| 66 | + case SchemaService.DURATION_TYPE => Values.isoDuration(struct.getAs[Number]("months").longValue(), |
| 67 | + struct.getAs[Number]("days").longValue(), |
| 68 | + struct.getAs[Number]("seconds").longValue(), |
| 69 | + struct.getAs[Number]("nanoseconds").intValue()) |
| 70 | + case SchemaService.TIME_TYPE_OFFSET => Values.value(OffsetTime.parse(struct.getAs[UTF8String]("value").toString)) |
| 71 | + case SchemaService.TIME_TYPE_LOCAL => Values.value(LocalTime.parse(struct.getAs[UTF8String]("value").toString)) |
| 72 | + case _ => toMap(struct) |
| 73 | + } |
| 74 | + } catch { |
| 75 | + case _: Throwable => toMap(struct) |
| 76 | + } |
| 77 | + } |
| 78 | + case unsafeArray: ArrayData => { |
| 79 | + val sparkType = dataType match { |
| 80 | + case arrayType: ArrayType => arrayType.elementType |
| 81 | + case _ => dataType |
| 82 | + } |
| 83 | + val javaList = unsafeArray.toSeq[AnyRef](sparkType) |
| 84 | + .map(elem => convert(elem, sparkType)) |
| 85 | + .asJava |
| 86 | + Values.value(javaList) |
| 87 | + } |
| 88 | + case unsafeMapData: MapData => { // Neo4j only supports Map[String, AnyRef] |
| 89 | + val mapType = dataType.asInstanceOf[MapType] |
| 90 | + val map: Map[String, AnyRef] = (0 until unsafeMapData.numElements()) |
| 91 | + .map(i => (unsafeMapData.keyArray().getUTF8String(i).toString, unsafeMapData.valueArray().get(i, mapType.valueType))) |
| 92 | + .toMap[String, AnyRef] |
| 93 | + .mapValues(innerValue => convert(innerValue, mapType.valueType)) |
| 94 | + .toMap[String, AnyRef] |
| 95 | + Values.value(map.asJava) |
| 96 | + } |
| 97 | + case string: UTF8String => convert(string.toString) |
| 98 | + case _ => Values.value(value) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +object Neo4jToSparkDataConverter { |
| 104 | + def apply(): Neo4jToSparkDataConverter = new Neo4jToSparkDataConverter() |
| 105 | +} |
| 106 | + |
| 107 | +class Neo4jToSparkDataConverter extends DataConverter[Any] { |
| 108 | + override def convert(value: Any, dataType: DataType): Any = { |
| 109 | + if (dataType != null && dataType == DataTypes.StringType && value != null && !value.isInstanceOf[String]) { |
| 110 | + convert(Neo4jUtil.mapper.writeValueAsString(value), dataType) |
| 111 | + } else { |
| 112 | + value match { |
| 113 | + case node: Node => { |
| 114 | + val map = node.asMap() |
| 115 | + val structType = extractStructType(dataType) |
| 116 | + val fields = structType |
| 117 | + .filter(field => field.name != Neo4jUtil.INTERNAL_ID_FIELD && field.name != Neo4jUtil.INTERNAL_LABELS_FIELD) |
| 118 | + .map(field => convert(map.get(field.name), field.dataType)) |
| 119 | + InternalRow.fromSeq(Seq(convert(node.id()), convert(node.labels())) ++ fields) |
| 120 | + } |
| 121 | + case rel: Relationship => { |
| 122 | + val map = rel.asMap() |
| 123 | + val structType = extractStructType(dataType) |
| 124 | + val fields = structType |
| 125 | + .filter(field => field.name != Neo4jUtil.INTERNAL_REL_ID_FIELD |
| 126 | + && field.name != Neo4jUtil.INTERNAL_REL_TYPE_FIELD |
| 127 | + && field.name != Neo4jUtil.INTERNAL_REL_SOURCE_ID_FIELD |
| 128 | + && field.name != Neo4jUtil.INTERNAL_REL_TARGET_ID_FIELD) |
| 129 | + .map(field => convert(map.get(field.name), field.dataType)) |
| 130 | + InternalRow.fromSeq(Seq(convert(rel.id()), |
| 131 | + convert(rel.`type`()), |
| 132 | + convert(rel.startNodeId()), |
| 133 | + convert(rel.endNodeId())) ++ fields) |
| 134 | + } |
| 135 | + case d: IsoDuration => { |
| 136 | + val months = d.months() |
| 137 | + val days = d.days() |
| 138 | + val nanoseconds: Integer = d.nanoseconds() |
| 139 | + val seconds = d.seconds() |
| 140 | + InternalRow.fromSeq(Seq(UTF8String.fromString(SchemaService.DURATION_TYPE), months, days, seconds, nanoseconds, UTF8String.fromString(d.toString))) |
| 141 | + } |
| 142 | + case zt: ZonedDateTime => DateTimeUtils.instantToMicros(zt.toInstant) |
| 143 | + case dt: LocalDateTime => DateTimeUtils.instantToMicros(dt.toInstant(ZoneOffset.UTC)) |
| 144 | + case d: LocalDate => d.toEpochDay.toInt |
| 145 | + case lt: LocalTime => { |
| 146 | + InternalRow.fromSeq(Seq( |
| 147 | + UTF8String.fromString(SchemaService.TIME_TYPE_LOCAL), |
| 148 | + UTF8String.fromString(lt.format(DateTimeFormatter.ISO_TIME)) |
| 149 | + )) |
| 150 | + } |
| 151 | + case t: OffsetTime => { |
| 152 | + InternalRow.fromSeq(Seq( |
| 153 | + UTF8String.fromString(SchemaService.TIME_TYPE_OFFSET), |
| 154 | + UTF8String.fromString(t.format(DateTimeFormatter.ISO_TIME)) |
| 155 | + )) |
| 156 | + } |
| 157 | + case p: InternalPoint2D => { |
| 158 | + val srid: Integer = p.srid() |
| 159 | + InternalRow.fromSeq(Seq(UTF8String.fromString(SchemaService.POINT_TYPE_2D), srid, p.x(), p.y(), null)) |
| 160 | + } |
| 161 | + case p: InternalPoint3D => { |
| 162 | + val srid: Integer = p.srid() |
| 163 | + InternalRow.fromSeq(Seq(UTF8String.fromString(SchemaService.POINT_TYPE_3D), srid, p.x(), p.y(), p.z())) |
| 164 | + } |
| 165 | + case l: java.util.List[_] => { |
| 166 | + val elementType = if (dataType != null) dataType.asInstanceOf[ArrayType].elementType else null |
| 167 | + ArrayData.toArrayData(l.asScala.map(e => convert(e, elementType)).toArray) |
| 168 | + } |
| 169 | + case map: java.util.Map[_, _] => { |
| 170 | + if (dataType != null) { |
| 171 | + val mapType = dataType.asInstanceOf[MapType] |
| 172 | + ArrayBasedMapData(map.asScala.map(t => (convert(t._1, mapType.keyType), convert(t._2, mapType.valueType)))) |
| 173 | + } else { |
| 174 | + ArrayBasedMapData(map.asScala.map(t => (convert(t._1), convert(t._2)))) |
| 175 | + } |
| 176 | + } |
| 177 | + case s: String => UTF8String.fromString(s) |
| 178 | + case _ => value |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments