randymay
randymay

Reputation: 443

How to process a nested Key Value Pair in Spark / Scala data import

I am new to Spark and Scala, so please forgive the noobness. What I have is a text file which is in this format:

328;ADMIN HEARNG;[street#939 W El Camino,city#Chicago,state#IL]

I have been able to create the RDD using the sc.textFile command, and I can process each section using this command:

val department_record = department_rdd.map(record => record.split(";"))

As you can see, though, the 3rd element is a nested key / value pair, and so far, I have been unable to work with it. What I am looking for is a way to transform the data from the above to an RDD that looks like this:

|ID |NAME        |STREET         |CITY   |STATE|

|328|ADMIN HEARNG|939 W El Camino|Chicago|IL   |

Any help is appreciated.

Upvotes: 0

Views: 605

Answers (2)

stack0114106
stack0114106

Reputation: 8791

DF solution:

scala> val df = Seq(("328;ADMIN HEARNG;[street#939 W El Camino,city#Chicago,state#IL]"),
     |   ("400;ADMIN HEARNG;[street#800 First Street,city#San Francisco,state#CA]")).toDF("dept")
df: org.apache.spark.sql.DataFrame = [dept: string]

scala> val df2 =df.withColumn("arr",split('dept,";")).withColumn("address",split(regexp_replace('arr(2),"\\[|\\]",""),"#"))
df2: org.apache.spark.sql.DataFrame = [dept: string, arr: array<string> ... 1 more field]

scala> df2.select('arr(0) as "id",'arr(1) as "name",split('address(1),",")(0) as "street",split('address(2),",")(0) as "city",'address(3) as "state").show
+---+------------+----------------+-------------+-----+
| id|        name|          street|         city|state|
+---+------------+----------------+-------------+-----+
|328|ADMIN HEARNG| 939 W El Camino|      Chicago|   IL|
|400|ADMIN HEARNG|800 First Street|San Francisco|   CA|
+---+------------+----------------+-------------+-----+


scala>

Upvotes: 0

Leo C
Leo C

Reputation: 22449

You can split the address field at , into an Array, strip away the enclosing bracket and split again at # to extract the wanted address components, as shown below:

val department_rdd = sc.parallelize(Seq(
  "328;ADMIN HEARNG;[street#939 W El Camino,city#Chicago,state#IL]",
  "400;ADMIN HEARNG;[street#800 First Street,city#San Francisco,state#CA]"
))

val department_record = department_rdd.
  map(_.split(";")).
  map{ case Array(id, name, address) =>
    val addressArr = address.split(",").
      map(_.replaceAll("^\\[|\\]$", "").split("#"))
    (id, name, addressArr(0)(1), addressArr(1)(1), addressArr(2)(1))
  }

department_record.collect
// res1: Array[(String, String, String, String, String)] = Array(
//   (328,ADMIN HEARNG,939 W El Camino,Chicago,IL),
//   (400,ADMIN HEARNG,800 First Street,San Francisco,CA)
// )

In case you want to convert to a DataFrame, simply apply toDF():

department_record.toDF("id", "name", "street", "city", "state").show
// +---+------------+----------------+-------------+-----+
// | id|        name|          street|         city|state|
// +---+------------+----------------+-------------+-----+
// |328|ADMIN HEARNG| 939 W El Camino|      Chicago|   IL|
// |400|ADMIN HEARNG|800 First Street|San Francisco|   CA|
// +---+------------+----------------+-------------+-----+

Upvotes: 0

Related Questions