scala Spark DataFrame groupBy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42544071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:07:13  来源:igfitidea点击:

Spark DataFrame groupBy

scalaapache-sparkgroup-byapache-spark-sql

提问by AKC

I have Spark Java that looked like this. Code pulls data from oracle table using JDBC and displays the groupby output.

我有看起来像这样的 Spark Java。代码使用 JDBC 从 oracle 表中提取数据并显示 groupby 输出。

DataFrame jdbcDF = sqlContext.read().format("jdbc").options(options).load();
jdbcDF.show();   
jdbcDF.groupBy("VA_HOSTNAME").count().show();

Long ll = jdbcDF.count();
System.out.println("ll="+ll);

When I ran the code, jdbcDF.show();is working, whereas the groupByand count are not printing anything and no errors were thrown.

当我运行代码时, jdbcDF.show();正在工作,而groupBycount 没有打印任何内容,也没有抛出任何错误。

My column name is correct. I tried by printing that column and it worked, but when groupByit's not working.

我的列名是正确的。我尝试打印该列并且它起作用了,但是当groupBy它不起作用时。

Can someone help me with DataFrameoutput? I am using spark 1.6.3.

有人可以帮我DataFrame输出吗?我正在使用火花 1.6.3。

回答by mrsrinivas

You can try

你可以试试

import org.apache.spark.sql.functions.count

jdbcDF.groupBy("VA_HOSTNAME").agg(count("*")).show()