Query regarding access the value in a tuple


This is regarding accessing a tuple.
I have noticed that in the videos some places we are accessing a value in a tuple using split function and this looks very clear to me.example is shown below- #Customer id with max revenue
val ordersPerDayPerCustomer = ordersJoinOrderItems.map(rec => ((rec._2._2.split(",")(1), rec._2._2.split(",")(2)), rec._2._1.split(",")(4).toFloat))

However In some places we are not using the split function to access a value in tuple.
val revenuePerDayPerCustomerMap = revenuePerDayPerCustomer.map(rec => (rec._1._1, (rec._1._2, rec._2)))

Kindly explain.


we use split(",")(0) when we want to get a field from a list and we use . _ 1 ._ 1 when we want to get a field from a tuple.
i will explain a bit more.
lets say you are joining two tables by order id and you have fields like say (key, value), where key is say order id and value say (order id , price, categoryid) your tuple will look like this ( 100,(100,35.79,77)). here you have a tuple with first element (100)as Int and second element(100,35.79,77) as list. so to access first element(100) you need to use x.__1 and to access second element , which is a list of 3 values (100,35.79,77) you need to use x.__2. now inside the second element if you want to access the 3rd value , you will use . x._2.split(",")(2).

suppose you have a nested tuple ( 100,((1,2,3),(a,b,c))
to access 100 : x.__1
To access 1,2,3: x.__2.__1 , to access 2 from 1,2,3: x.__2 ._1.split(",")(1)
to access a,b,c : x._2._2, to access c from a,b,c: x._2._2.split(",")(2)
when you use tuple._2 , element starts from 1 . where as if you use split(’,") elements starts from 0.

underscore is not printing here so where you see dotnumeric value , please assume it as dotunderscorenumber .

i hope its clear.

1 Like

Thanks Avinash for the detailed explanation.