Spark SQL에서 열을 내림차순으로 정렬하는 방법은 무엇입니까?
시도 df.orderBy("col1").show(10)
했지만 오름차순으로 정렬되었습니다. df.sort("col1").show(10)
내림차순으로도 정렬됩니다. 나는 stackoverflow를 살펴 보았고 내가 찾은 답변은 모두 구식이거나 RDD에 언급되었습니다 . Spark에서 기본 데이터 프레임을 사용하고 싶습니다.
Spark SQL 함수를 가져 와서 열을 정렬 할 수도 있습니다.
import org.apache.spark.sql.functions._
df.orderBy(asc("col1"))
또는
import org.apache.spark.sql.functions._
df.sort(desc("col1"))
sqlContext.implicits._ 가져 오기
import sqlContext.implicits._
df.orderBy($"col1".desc)
또는
import sqlContext.implicits._
df.sort($"col1".desc)
이 글은의 org.apache.spark.sql.DataFrame
에 대한 sort
방법 :
df.sort($"col1", $"col2".desc)
결과를 정렬 할 열에 대한 메모 $
및 .desc
내부 sort
.
PySpark 만
PySpark에서 동일한 작업을 수행하려고 할 때이 게시물을 보았습니다. 가장 쉬운 방법은 매개 변수 ascending = False를 추가하는 것입니다.
df.orderBy("col1", ascending=False).show(10)
참조 : http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html#pyspark.sql.DataFrame.orderBy
import org.apache.spark.sql.functions.desc
df.orderBy(desc("columnname1"),desc("columnname2"),asc("columnname3"))
df.sort($"ColumnName".desc).show()
Java의 경우 :
를 사용 DataFrames
하면 조인 (여기서는 내부 조인)을 적용하는 동안 각 DF에서 고유 한 요소를 다음과 같이 선택한 후 정렬 할 수 있습니다 (ASC에서).
Dataset<Row> d1 = e_data.distinct().join(s_data.distinct(), "e_id").orderBy("salary");
여기서 e_id
ASC 급여으로 정렬하는 동안 적용되는 가입 된 열이다.
또한 Spark SQL을 다음과 같이 사용할 수 있습니다.
SQLContext sqlCtx = spark.sqlContext();
sqlCtx.sql("select * from global_temp.salary order by salary desc").show();
어디
- 스파크-> SparkSession
- 급여-> GlobalTemp보기.
참고 URL : https://stackoverflow.com/questions/30332619/how-to-sort-by-column-in-descending-order-in-spark-sql
'development' 카테고리의 다른 글
유닉스에서 현재 디렉토리와 그 아래의 모든 것을 어떻게 제거합니까? (0) | 2020.08.07 |
---|---|
PHP에서 여러 파일 업로드 (0) | 2020.08.07 |
Mac에서 Ruby를 1.9.x로 업데이트하는 방법은 무엇입니까? (0) | 2020.08.07 |
Jenkins가 Windows에서 서비스로 시작될 때 더 많은 힙 공간을 제공하는 방법은 무엇입니까? (0) | 2020.08.07 |
C ++ 생성자가있는 기본 매개 변수 (0) | 2020.08.06 |