selecting a range of elements in an array spark sql

后端 未结 8 553
闹比i
闹比i 2020-12-14 23:16

I use spark-shell to do the below operations.

Recently loaded a table with an array column in spark-sql .

Here is the DDL for the same:

相关标签:
8条回答
  • 2020-12-15 00:17

    You can use the function array to build a new Array out of the three values:

    import org.apache.spark.sql.functions._
    
    val input = sqlContext.sql("select emp_details from emp_details")
    
    val arr: Column = col("emp_details")
    val result = input.select(array(arr(0), arr(1), arr(2)) as "emp_details")
    
    val result.show()
    // +-------------------+
    // |        emp_details|
    // +-------------------+
    // |[Jon, Snow, Castle]|
    // |      [Ned, is, no]|
    // +-------------------+
    
    0 讨论(0)
  • 2020-12-15 00:20

    use selecrExpr() and split() function in apache spark.

    for example :

    fs.selectExpr("((split(emp_details, ','))[0]) as e1,((split(emp_details, ','))[1]) as e2,((split(emp_details, ','))[2]) as e3);
    
    0 讨论(0)
提交回复
热议问题