【发布时间】:2014-02-25 22:39:59
【问题描述】:
编辑 2
通过将RDD重新分区为8个分区间接解决了这个问题。遇到 avro 对象不是“java 可序列化”的障碍时发现了一个 sn-p here to delegate avro serialisation to kryo. 原来的问题仍然存在。
编辑 1: 删除地图函数中的局部变量引用
我正在编写一个驱动程序,以使用 parquet 和 avro 为 io/schema 在 spark 上运行计算繁重的工作。我似乎无法使用我所有的核心来获得火花。我究竟做错了什么 ?是因为我将键设置为 null 吗?
我只是想了解 hadoop 如何组织文件。 AFAIK,因为我的文件有千兆字节的原始数据,我应该期望看到与默认块和页面大小并行的东西。
ETL 我的输入进行处理的函数如下所示:
def genForum {
class MyWriter extends AvroParquetWriter[Topic](new Path("posts.parq"), Topic.getClassSchema) {
override def write(t: Topic) {
synchronized {
super.write(t)
}
}
}
def makeTopic(x: ForumTopic): Topic = {
// Ommited to save space
}
val writer = new MyWriter
val q =
DBCrawler.db.withSession {
Query(ForumTopics).filter(x => x.crawlState === TopicCrawlState.Done).list()
}
val sz = q.size
val c = new AtomicInteger(0)
q.par.foreach {
x =>
writer.write(makeTopic(x))
val count = c.incrementAndGet()
print(f"\r${count.toFloat * 100 / sz}%4.2f%%")
}
writer.close()
}
我的转换如下:
def sparkNLPTransformation() {
val sc = new SparkContext("local[8]", "forumAddNlp")
// io configuration
val job = new Job()
ParquetInputFormat.setReadSupportClass(job, classOf[AvroReadSupport[Topic]])
ParquetOutputFormat.setWriteSupportClass(job,classOf[AvroWriteSupport])
AvroParquetOutputFormat.setSchema(job, Topic.getClassSchema)
// configure annotator
val props = new Properties()
props.put("annotators", "tokenize,ssplit,pos,lemma,parse")
val an = DAnnotator(props)
// annotator function
def annotatePosts(ann : DAnnotator, top : Topic) : Topic = {
val new_p = top.getPosts.map{ x=>
val at = new Annotation(x.getPostText.toString)
ann.annotator.annotate(at)
val t = at.get(classOf[SentencesAnnotation]).map(_.get(classOf[TreeAnnotation])).toList
val r = SpecificData.get().deepCopy[Post](x.getSchema,x)
if(t.nonEmpty) r.setTrees(t)
r
}
val new_t = SpecificData.get().deepCopy[Topic](top.getSchema,top)
new_t.setPosts(new_p)
new_t
}
// transformation
val ds = sc.newAPIHadoopFile("forum_dataset.parq", classOf[ParquetInputFormat[Topic]], classOf[Void], classOf[Topic], job.getConfiguration)
val new_ds = ds.map(x=> ( null, annotatePosts(x._2) ) )
new_ds.saveAsNewAPIHadoopFile("annotated_posts.parq",
classOf[Void],
classOf[Topic],
classOf[ParquetOutputFormat[Topic]],
job.getConfiguration
)
}
【问题讨论】:
-
我已经得出结论,目前无法使用 Spark 拆分 parquet 文件,并且必须使用 Hadoop 作业通过设置 reducer 的数量来拆分文件(这可能很快,但很可怕黑客)。我问了一个类似的问题stackoverflow.com/questions/27194333/…
标签: scala hadoop parallel-processing bigdata apache-spark