如何将通用rdd转换为数据帧? [英] how to convert generic rdd to dataframe?

查看:91
本文介绍了如何将通用rdd转换为数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个采用rdd并将其另存为avro文件的方法.问题是,如果我使用特定类型,则可以执行.toDF(),但是不能在通用rdd上调用.toDF()!这是一个示例:

I am writing a method that takes an rdd and saves it as an avro file. The problem is that if I use a specific type than I can do .toDF() but I cannot call .toDF() on a generic rdd! Here is an example:

case class Person(name: String)

def f(x: RDD[Person]) = x.toDF()
def g[T](x: RDD[T]) = x.toDF()

f(p) //works
g(p) //fails!!

有人知道为什么我不能在通用rdd上调用.toDF()以及周围是否有办法吗?

Does anyone know why I can't call .toDF() on a generic rdd and if there is any way around it?

推荐答案

如果您使用的是Spark 2,

If you are using Spark 2,

import org.apache.spark.sql.Encoder

def g[T: Encoder](x: RDD[T]) = x.toDF()

将起作用.

toDFimplicit conversion

implicit def rddToDatasetHolder[T : Encoder](rdd: RDD[T]): DatasetHolder[T] = {
  DatasetHolder(_sqlContext.createDataset(rdd))
}

org.apache.spark.sql.SQLImplicits

要完成,签名应该相同.

To accomplish, the signature should be the same.

这篇关于如何将通用rdd转换为数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆