Cómo convertir DataFrame a RDD en Scala?


¿Puede alguien por favor compartir cómo se puede convertir un dataframe a un RDD?

Author: Prasad Khode, 2015-09-11

4 answers

Simplemente:

val rows: RDD[Row] = df.rdd
 40
Author: Jean Logeart,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-11 20:04:42

Use df.map(row => ...) para convertir el dataframe a un RDD si desea asignar una fila a un elemento RDD diferente. Por ejemplo

df.map(row => (row(1), row(2)))

Le da un RDD emparejado donde la primera columna del df es la clave y la segunda columna del df es el valor.

 3
Author: Random Certainty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-28 18:54:20

Estaba buscando mi respuesta y encontré este post.

La respuesta de Jean a absolutamente correcta,añadiendo que "df.rdd " devolverá un RDD [Filas]. Necesito aplicar split () una vez que obtengo RDD. Para eso necesitamos convertir RDD [Row} a RDD [String]

val opt=spark.sql("select tags from cvs").map(x=>x.toString()).rdd
 2
Author: Ishan Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-17 12:15:56

Fro me trabajo método aún más simple:

 // Data frame creation from csv
//-----------------------------------------------------------------------------------------------------------------
val sacramentoDF = sqlContext.read
  .format("com.databricks.spark.csv")
  .option("header", "true") // Use first line of all files as header
  .option("inferSchema", "true") // Automatically infer data types
  .load("src/main/data/Sacramento.csv")

// RDD creation from Data frame
//-----------------------------------------------------------------------------------------------------------------
val sacramentoRDD = sacramentoDF.rdd
 -1
Author: Janis Karklins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-20 15:20:39