【问题标题】:Reading Avro table and schema stored in HDFS using Spark- Java使用 Spark-Java 读取存储在 HDFS 中的 Avro 表和模式
【发布时间】:2017-04-18 08:13:36
【问题描述】:

我正在尝试读取存储在 HDFS 中的 Avro 表,还指定了架构 它也存储在 HDFS 中。 目前我有这个似乎可行的解决方案:

    RDD<String> stringRDD = sparkContext.textFile(schemaPath, 1);
    String [] collect = (String []) stringRDD.collect();
    String schema = collect[0];
    Dataset<Row> df  =sqlContext.read().format("com.databricks.spark.avro").option("avroSchema", schema)
            .load(tablePath);

这是最好的方法吗? 例如,如果架构大到足以拥有 2 个分区怎么办?我应该使用 reduce() 合并所有这些吗?

干杯

【问题讨论】:

  • 为什么首先需要读取架构?可以在没有模式的情况下读取 avro(因为模式嵌入在 avro 中)。您是否尝试跳过架构选项?
  • Schema 也可以直接在类中描述(作为一个字段,手工创建),而不需要从外部文件中获取,是不是一种选择?

标签: java hadoop apache-spark hdfs avro


【解决方案1】:

我知道这个问题已经过去一年了,但我最近想做同样的事情,这个问题在谷歌上出现了。

所以,我可以使用 Hadoop 的 FileSystem 类来做到这一点:

import org.apache.avro.Schema;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;

String schemaPath = "/path/to/schema/in/hdfs.avsc";
FSDataInputStream schemaFile = FileSystem.get(sparkContext.hadoopConfiguration).open(new Path(schemaPath));
Schema schema = new Schema.Parser().parse(schemaFile);
String schemaString = schema.toString();

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    另一种使用 Spark 2.1.1 的方法

    import org.apache.avro.Schema
    val avroSchema = spark.sparkContext.wholeTextFiles(source).take(1)(0)._2
    val schema = new Schema.Parser().parse(avroSchema)
    

    【讨论】:

      猜你喜欢
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多