- PySpark Cookbook
- Denny Lee Tomasz Drabas
- 97字
- 2021-06-18 19:06:39
.sortByKey(...) transformation
The sortByKey(asc) transformation orders (key, value) RDD by key and returns an RDD in ascending or descending order. Look at the following code snippet:
# Takes the origin code and delays, remove header
# runs a group by origin code via reduceByKey()
# sorting by the key (origin code)
(
flights
.zipWithIndex()
.filter(lambda (row, idx): idx > 0)
.map(lambda (row, idx): row)
.map(lambda c: (c[3], int(c[1])))
.reduceByKey(lambda x, y: x + y)
.sortByKey()
.take(50)
)
This will produce this output:
# Output
[(u'ABE', 5113),
(u'ABI', 5128),
(u'ABQ', 64422),
(u'ABY', 1554),
(u'ACT', 392),
...]
推薦閱讀
- Node.js+Webpack開發(fā)實戰(zhàn)
- 程序員面試白皮書
- Visual C++實例精通
- Learning Bayesian Models with R
- 程序員考試案例梳理、真題透解與強化訓練
- SEO智慧
- Learning Apache Kafka(Second Edition)
- Python:Master the Art of Design Patterns
- 前端HTML+CSS修煉之道(視頻同步+直播)
- Spring Boot+Vue全棧開發(fā)實戰(zhàn)
- Beginning C++ Game Programming
- FFmpeg開發(fā)實戰(zhàn):從零基礎到短視頻上線
- Emotional Intelligence for IT Professionals
- Unity Android Game Development by Example Beginner's Guide
- C語言進階:重點、難點與疑點解析