如何使用java api在Apache Spark Dataset中使用desc进行排序?
作者:互联网
我正在使用spark会话读取文件,然后拆分单词并计算单词的迭代次数.我需要以desc顺序显示数据
SparkSession sparkSession = SparkSession
.builder()
.appName("Java Spark SQL basic example")
.config("spark.master", "local")
.getOrCreate();
JavaRDD<Word> textFile = sparkSession
.read()
.textFile("/Users/myname/Documents/README.txt")
.javaRDD()
.flatMap(s -> Arrays.asList(s.split("[\\s.]")).iterator())
.map(w -> {
Word word = new Word();
word.setWord(w.replace(",", ""));
return word;
});
Dataset<Row> df = sparkSession.createDataFrame(textFile, Word.class);
df.groupBy("word").count().orderBy(org.apache.spark.sql.functions.col("count").desc()).show();
当我使用org.apache.spark.sql.functions.col(“count”)时,它工作正常,但无法按照https://spark.apache.org/docs/2.1.1/api/java/org/apache/spark/sql/functions.html#desc(java.lang.String)中的定义执行
df.sort(asc(“dept”),desc(“age”))
也是How to sort by column in descending order in Spark SQL?
没用.我想这是斯卡拉. Java中的等价物是什么?
解决方法:
在Java中,您必须以这种方式导入包:
import static org.apache.spark.sql.functions.*
标签:java,apache-spark,apache-spark-sql,apache-spark-dataset 来源: https://codeday.me/bug/20190722/1501354.html