Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/7971#discussion_r39197650
--- Diff: core/src/test/scala/org/apache/spark/SparkContextSuite.scala ---
@@ -215,6 +224,102 @@ class SparkContextSuite extends SparkFunSuite with
LocalSparkContext {
}
}
+ test("Generic Avro Records as input") {
+ withTempDir { dir =>
+ try {
+ sc = new SparkContext(new
SparkConf().setAppName("test").setMaster("local"))
+ val schemaStr = """
+ |{
+ | "type" : "record",
+ | "name" : "test_schema",
+ | "fields" : [{
+ | "name" : "string",
+ | "type" : "string",
+ | "doc" : "some string of characters"
+ | }, {
+ | "name": "int",
+ | "type": "int",
+ | "doc": "some random integer"
+ | }]
+ |}
+ |
+ """.stripMargin
+ val schema = new Schema.Parser().parse(schemaStr)
+ val outputFile = new
File(s"${dir.getCanonicalPath}/avro_test.avro")
+ val datumWriter = new GenericDatumWriter[GenericRecord](schema)
+ val dataFileWriter = new DataFileWriter[GenericRecord](datumWriter)
+ dataFileWriter.create(schema, outputFile)
+ for (i <- 1 to 10) {
+ val avroRec = new GenericData.Record(schema)
+ avroRec.put("string", Random.nextString(10))
+ avroRec.put("int", 10)
+ dataFileWriter.append(avroRec)
+ }
+ dataFileWriter.close()
+ val sum = sc.avroFile(dir.getCanonicalPath,
schema).map(_.get("int")
+ .asInstanceOf[Int]).sum()
+ assert(sum == 100)
--- End diff --
Would be nice to also check the actual contents of read Avro records.
While trying to add such a test case, I found that string fields in Avro
files are always materialized as Avro `Utf8`, even if I called
```scala
GenericData.setStringType(schema, GenericData.StringType.String)
```
Can we make this behavior configurable?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]