在 Spark 中一次遍历整个数据集?
Iterate through a whole dataset at once in Spark?
我有一个大数据集,其中包含每个国家/地区每年的人口统计数据。我将 Apache Spark 与 Scala 和 Parquet 一起使用。结构是每年一列(即“1965”)。我希望能够在集合中 select 行值。
架构如下:
columns: Array[String] = Array(country, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010)
我希望能够根据人口水平过滤我的数据集,无论是哪一年。比如获取人口超过500的国家名称和年份
SELECT * FROM table WHERE population > 5000000.
Result: Cuba, 1962
如何构造我的数据框以允许此类查询?
您只需旋转 table。
这是一篇好文章:
https://databricks.com/blog/2018/11/01/sql-pivot-converting-rows-to-columns.html
如何旋转数据框:
case class Demographic(country: String,
population: Long,
year: Int)
// Creating a strongly-typed dataset.
val dataset = Seq(
Demographic("US",20*math.pow(10,3).toLong, 1675),
Demographic("US", 3*math.pow(10,8).toLong, 2018),
Demographic("CH", math.pow(10,9).toLong, 2015))
.toDF.as[Demographic]
// Now filtering is easy using a lambda expression.
dataset.filter(demo => demo.population > 5000)
我有一个大数据集,其中包含每个国家/地区每年的人口统计数据。我将 Apache Spark 与 Scala 和 Parquet 一起使用。结构是每年一列(即“1965”)。我希望能够在集合中 select 行值。
架构如下:
columns: Array[String] = Array(country, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010)
我希望能够根据人口水平过滤我的数据集,无论是哪一年。比如获取人口超过500的国家名称和年份
SELECT * FROM table WHERE population > 5000000.
Result: Cuba, 1962
如何构造我的数据框以允许此类查询?
您只需旋转 table。
这是一篇好文章: https://databricks.com/blog/2018/11/01/sql-pivot-converting-rows-to-columns.html
如何旋转数据框:
case class Demographic(country: String,
population: Long,
year: Int)
// Creating a strongly-typed dataset.
val dataset = Seq(
Demographic("US",20*math.pow(10,3).toLong, 1675),
Demographic("US", 3*math.pow(10,8).toLong, 2018),
Demographic("CH", math.pow(10,9).toLong, 2015))
.toDF.as[Demographic]
// Now filtering is easy using a lambda expression.
dataset.filter(demo => demo.population > 5000)