我有一些文档文件,我尝试读取数据,然后使用 zipWithIndex() 函数按照索引压缩:
JavaRDD<String> rawTextRDD = sc.textFile("demo.txt");
JavaPairRDD<String, Long> zipIndex = rawTextRDD.zipWithIndex();
zipIndex 的值之后是一个包含键值对的 JavaPairRDD,看起来像 ["This is the beautiful picture", 0], ["This is another picture", 1]。
但是现在,我想使用以下方法将 zipIndex 转换为 DataFrame:
DataFrame docDF = sqlContext.createDataFrame(zipIndex, TextId.class);
函数 createDataFrame 不接受参数 zipIndex(TextId 是一个具有 2 个属性的类:String text 和 int docId)。
我也有一个Scala的代码,运行的很好。请引用:
val rawTextRDD = sc.textFile("demo.txt")
val docDF = rawTextRDD.zipWithIndex.toDF("text", "docId")
如果有解决办法。请帮我解决。谢谢
最佳答案
试试这个:DataFrame docDF = sqlContext.createDataFrame(zipIndex.values(), TextId.class);
关于使用 Java 将 JavaPairRDD 转换为 Apache Spark 中的 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33200943/