在 spark 中过滤数据框并保存为 avro

Filtering dataframe in spark and saving as avro

我正在尝试将数据框保存为 avro 文件。我读入了一个包含许多嵌套层的 xml 文件。它将其存储为数据框。数据帧已成功存储。 xml 有很多名称 space headers,例如@nso、@ns1、@ns2 等。这些成为数据帧中的 headers。当我尝试将其保存为 avro 文件时,出现以下错误:"Exception in thread "main" org.apache.avro.SchemaParseException: Illegal initial character: @ns0"

val conf = new SparkConf()
         .setMaster("local[2]")
         .setAppName("conversion")
val sc = new SparkContext(conf)

val sqlContext = new SQLContext(sc)

val df = sqlContext.read
  .format("com.databricks.spark.xml")
  .option("rowTag", "Stuff")
  .load("sample.xml")

df.printSchema()
df.show()

df.write
  .format("com.databricks.spark.avro")
  .save("output")

一个有效的 Avro 名称 has to start with a letter or an underscore 因此您可以重命名从属性生成的列或指定替代前缀。 spark-csv 允许您使用 attributePrefix 属性:

配置属性前缀
val df = sqlContext.read
  .format("com.databricks.spark.xml")
  .option("rowTag", "Stuff")
  .option("attributePrefix", "attr_")  // or some other prefix of your choice
  .load("sample.xml")