Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #537: Kafka sink connector does not work with CUD ingestion strategy and Avro format data #582

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import java.util.Date
import java.util.concurrent.TimeUnit


class Neo4jValueConverter: MapValueConverter<Value>() {
class Neo4jValueConverter: MapValueConverter<Any>() {

companion object {
@JvmStatic private val UTC = ZoneId.of("UTC")
}

override fun setValue(result: MutableMap<String, Value?>?, fieldName: String, value: Any?) {
override fun setValue(result: MutableMap<String, Any?>?, fieldName: String, value: Any?) {
if (result != null) {
result[fieldName] = Values.value(value) ?: Values.NULL
result[fieldName] = value
}
}

override fun newValue(): MutableMap<String, Value?> {
override fun newValue(): MutableMap<String, Any?> {
return mutableMapOf()
}

override fun setDecimalField(result: MutableMap<String, Value?>?, fieldName: String, value: BigDecimal) {
override fun setDecimalField(result: MutableMap<String, Any?>?, fieldName: String, value: BigDecimal) {
val doubleValue = value.toDouble()
val fitsScale = doubleValue != Double.POSITIVE_INFINITY
&& doubleValue != Double.NEGATIVE_INFINITY
Expand All @@ -38,26 +38,24 @@ class Neo4jValueConverter: MapValueConverter<Value>() {
}
}

override fun setTimestampField(result: MutableMap<String, Value?>?, fieldName: String, value: Date) {
override fun setTimestampField(result: MutableMap<String, Any?>?, fieldName: String, value: Date) {
val localDate = value.toInstant().atZone(UTC).toLocalDateTime()
setValue(result, fieldName, localDate)

}

override fun setTimeField(result: MutableMap<String, Value?>?, fieldName: String, value: Date) {
override fun setTimeField(result: MutableMap<String, Any?>?, fieldName: String, value: Date) {
val time = LocalTime.ofNanoOfDay(TimeUnit.MILLISECONDS.toNanos(value.time))
setValue(result, fieldName, time)
}

override fun setDateField(result: MutableMap<String, Value?>?, fieldName: String, value: Date) {
override fun setDateField(result: MutableMap<String, Any?>?, fieldName: String, value: Date) {
val localDate = value.toInstant().atZone(UTC).toLocalDate()
setValue(result, fieldName, localDate)
}

override fun setStructField(result: MutableMap<String, Value?>?, fieldName: String, value: Struct) {
val converted = convert(value)
.mapValues { it.value?.asObject() }
.toMutableMap() as MutableMap<Any?, Any?>
setMap(result, fieldName, null, converted)
override fun setStructField(result: MutableMap<String, Any?>?, fieldName: String, value: Struct) {
val converted = convert(value).toMutableMap() as MutableMap<Any?, Any?>
setValue(result, fieldName, converted)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,27 @@ class Neo4jSinkTaskTest {
task.initialize(mock(SinkTaskContext::class.java))
}

private val PERSON_SCHEMA = SchemaBuilder.struct().name("com.example.Person")
.field("firstName", Schema.STRING_SCHEMA)
.field("lastName", Schema.STRING_SCHEMA)
.field("age", Schema.OPTIONAL_INT32_SCHEMA)
.field("bool", Schema.OPTIONAL_BOOLEAN_SCHEMA)
.field("short", Schema.OPTIONAL_INT16_SCHEMA)
.field("byte", Schema.OPTIONAL_INT8_SCHEMA)
.field("long", Schema.OPTIONAL_INT64_SCHEMA)
.field("float", Schema.OPTIONAL_FLOAT32_SCHEMA)
.field("double", Schema.OPTIONAL_FLOAT64_SCHEMA)
.field("modified", Timestamp.SCHEMA)
.build()

private val PLACE_SCHEMA = SchemaBuilder.struct().name("com.example.Place")
.field("name", Schema.STRING_SCHEMA)
.field("latitude", Schema.FLOAT32_SCHEMA)
.field("longitude", Schema.FLOAT32_SCHEMA)
.field("modified", Timestamp.SCHEMA)
.build()

private val PERSON_SCHEMA = SchemaBuilder.struct().name("com.example.Person")
.field("firstName", Schema.STRING_SCHEMA)
.field("lastName", Schema.STRING_SCHEMA)
.field("age", Schema.OPTIONAL_INT32_SCHEMA)
.field("bool", Schema.OPTIONAL_BOOLEAN_SCHEMA)
.field("short", Schema.OPTIONAL_INT16_SCHEMA)
.field("byte", Schema.OPTIONAL_INT8_SCHEMA)
.field("long", Schema.OPTIONAL_INT64_SCHEMA)
.field("float", Schema.OPTIONAL_FLOAT32_SCHEMA)
.field("double", Schema.OPTIONAL_FLOAT64_SCHEMA)
.field("modified", Timestamp.SCHEMA)
.field("place", PLACE_SCHEMA)
.build()

@Test
fun `test array of struct`() {
val firstTopic = "neotopic"
Expand Down Expand Up @@ -1351,6 +1353,54 @@ class Neo4jSinkTaskTest {
}
}

@Test
fun `should successfully parse nested timestamps`() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just timestamps, or more general sub-maps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is general sub-maps

val firstTopic = "neotopic"
val secondTopic = "foo"
val props = mutableMapOf<String, String>()
props[Neo4jConnectorConfig.SERVER_URI] = neo4j.boltUrl
props["${Neo4jSinkConnectorConfig.TOPIC_CYPHER_PREFIX}$firstTopic"] = "CREATE (n:PersonExt {modified: event.place.modified})"
props["${Neo4jSinkConnectorConfig.TOPIC_CYPHER_PREFIX}$secondTopic"] = "CREATE (n:Person {modified: event.place.modified})"
props[Neo4jConnectorConfig.AUTHENTICATION_TYPE] = AuthenticationType.NONE.toString()
props[Neo4jConnectorConfig.BATCH_SIZE] = 2.toString()
props[SinkTask.TOPICS_CONFIG] = "$firstTopic,$secondTopic"

val place = Struct(PLACE_SCHEMA)
.put("name", "San Mateo (CA)")
.put("latitude", 37.5629917.toFloat())
.put("longitude", -122.3255254.toFloat())
.put("modified", Date(1474661402123L))
val struct = Struct(PERSON_SCHEMA)
.put("firstName", "Alex")
.put("lastName", "Smith")
.put("bool", true)
.put("short", 1234.toShort())
.put("byte", (-32).toByte())
.put("long", 12425436L)
.put("float", 2356.3.toFloat())
.put("double", -2436546.56457)
.put("age", 21)
.put("modified", Date(1474661402123L))
.put("place", place)

task.start(props)
val input = listOf(SinkRecord(firstTopic, 1, null, null, PERSON_SCHEMA, struct, 42),
SinkRecord(firstTopic, 1, null, null, PERSON_SCHEMA, struct, 42),
SinkRecord(firstTopic, 1, null, null, PERSON_SCHEMA, struct, 42),
SinkRecord(firstTopic, 1, null, null, PERSON_SCHEMA, struct, 42),
SinkRecord(firstTopic, 1, null, null, PERSON_SCHEMA, struct, 42),
SinkRecord(secondTopic, 1, null, null, PERSON_SCHEMA, struct, 43))
task.put(input)
session.beginTransaction().use {
val personCount = it.run("MATCH (p:Person) RETURN COUNT(p) as COUNT").single()["COUNT"].asLong()
val expectedPersonCount = input.filter { it.topic() == secondTopic }.size
assertEquals(expectedPersonCount, personCount.toInt())
val personExtCount = it.run("MATCH (p:PersonExt) RETURN COUNT(p) as COUNT").single()["COUNT"].asLong()
val expectedPersonExtCount = input.filter { it.topic() == firstTopic }.size
assertEquals(expectedPersonExtCount, personExtCount.toInt())
}
}

private fun countFooPersonEntities(expected: Int) {
val personCount = session.run("MATCH (p:Person) RETURN count(p) as count")
.single()["count"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class Neo4jValueConverterNestedStructTest {
.put("tns", tnsList)
}

fun getExpectedMap(): Map<String, Value> {
return JSONUtils.readValue<Map<String, Any?>>(data).mapValues(::convertDateNew).mapValues { Values.value(it.value) }
fun getExpectedMap(): Map<String, Any?> {
return JSONUtils.readValue<Map<String, Any?>>(data).mapValues(::convertDateNew)
}

fun convertDate(it: Map.Entry<String,Any?>) : Any? =
Expand Down
Loading
Loading