Dictionary 如何在RDD映射转换中修改avro键/值模式
我正在尝试将一些Hadoop Map Reduce代码迁移到Spark,我怀疑当键或值的模式从输入更改为输出时,如何管理Map和Reduce转换 我有avro文件和指标记录,我想以某种方式处理。我已经有了这样的代码:Dictionary 如何在RDD映射转换中修改avro键/值模式,dictionary,apache-spark,schema,rdd,avro,Dictionary,Apache Spark,Schema,Rdd,Avro,我正在尝试将一些Hadoop Map Reduce代码迁移到Spark,我怀疑当键或值的模式从输入更改为输出时,如何管理Map和Reduce转换 我有avro文件和指标记录,我想以某种方式处理。我已经有了这样的代码: val myAvroJob = new Job() myAvroJob.setInputFormatClass(classOf[AvroKeyInputFormat[Indicator]]) myAvroJob.setOutputFormatClas
val myAvroJob = new Job()
myAvroJob.setInputFormatClass(classOf[AvroKeyInputFormat[Indicator]])
myAvroJob.setOutputFormatClass(classOf[AvroKeyOutputFormat[Indicator]])
myAvroJob.setOutputValueClass(classOf[NullWritable])
AvroJob.setInputValueSchema(myAvroJob, Schema.create(Schema.Type.NULL))
AvroJob.setInputKeySchema(myAvroJob, Indicator.SCHEMA$)
AvroJob.setOutputKeySchema(myAvroJob, Indicator.SCHEMA$)
val indicatorsRdd = sc.newAPIHadoopRDD(myAvroJob.getConfiguration,
classOf[AvroKeyInputFormat[Indicator]],
classOf[AvroKey[Indicator]],
classOf[NullWritable])
val myRecordOnlyRdd = indicatorsRdd.map(x => (doSomethingWith(x._1), NullWritable.get)
val indicatorPairRDD = new PairRDDFunctions(myRecordOnlyRdd)
indicatorPairRDD.saveAsNewAPIHadoopDataset(myAvroJob.getConfiguration)
但是,由于输入键和输出键的模式不变,所以此代码是有效的,始终是指示符。在hadoop Map Reduce中,您可以定义Map或Reduce函数,并从输入到输出修改模式。事实上,我有map函数,它处理每个指标记录并生成一个新记录soportera。我怎样才能在spark中做到这一点?可能来自同一个RDD,或者我必须定义两个不同的RDD,并以某种方式从一个传递到另一个
谢谢你的帮助。要回答我自己的问题。。。问题是您无法更改RDD类型,必须定义不同的RDD,因此我用上面的代码解决了它:
val myAvroJob = new Job()
myAvroJob.setInputFormatClass(classOf[AvroKeyInputFormat[SoporteCartera]])
myAvroJob.setOutputFormatClass(classOf[AvroKeyOutputFormat[Indicator]])
myAvroJob.setOutputValueClass(classOf[NullWritable])
AvroJob.setInputValueSchema(myAvroJob, Schema.create(Schema.Type.NULL))
AvroJob.setInputKeySchema(myAvroJob, SoporteCartera.SCHEMA$)
AvroJob.setOutputKeySchema(myAvroJob, Indicator.SCHEMA$)
val soporteCarteraRdd = sc.newAPIHadoopRDD(myAvroJob.getConfiguration,
classOf[AvroKeyInputFormat[SoporteCartera]],
classOf[AvroKey[SoporteCartera]],
classOf[NullWritable])
val indicatorsRdd = soporteCarteraRdd.map(x => (fromSoporteCarteraToIndicator(x._1), NullWritable.get))
val indicatorPairRDD = new PairRDDFunctions(indicatorsRdd)
indicatorPairRDD.saveAsNewAPIHadoopDataset(myAvroJob.getConfiguration)