在Spark中加入2个数据框后选择特定的列

时间:2018-09-24 00:33:14

标签: apache-spark pyspark

我加入了2个数据框,现在试图从两个数据框中获取包含列的报告。 我尝试使用.select(cols = String *),但是它不起作用。

here中描述的方法似乎也无法解决我的问题。

下面是代码。 val full_report是我需要获取列的地方。

import org.apache.spark.sql.types._

object read_data {


  def main (args:Array[String]) {

    val spark = org.apache.spark.sql.SparkSession.builder
      .master("local")
      .appName("Spark CSV Reader")
      .getOrCreate;


    val customSchema = StructType(Array(
      StructField("order_id", IntegerType, true),
      StructField("parent_order_uuid", StringType, true),
      StructField("company", StringType, true),
      StructField("country_id", IntegerType, true)))

    val readogp = spark.read.format("csv")
      .option("header", "false")
      .schema(customSchema)
      .load("/home/cloudera/Desktop/ogp_csv.csv")

    readogp.show()

    val read_country = spark.read.format("csv")
      .option("header", "true")
      .load("/home/cloudera/Desktop/country.csv")

    read_country.show()

    println("************************************************************************")

   val full_report  = readogp.join(read_country, readogp.col("country_id") === read_country.col("country_id"))
       .select(readogp.select("order_id" + "parent_order_id"))  

    full.show()

  }

}

请让我知道如何克服这一障碍。

1 个答案:

答案 0 :(得分:1)

加入数据框后,可以使用以下语法获取特定的列。

根据您的示例:

val full_report_df  = readogp.join(read_country, readogp.col("country_id") == read_country.col("country_id"))

val full_report = full_report_df.select("order_id","parent_order_id")  

// Below will show 10 records for 2 columns order_id, parent_order_id.

full_report.show(10,false)  
相关问题