filter a Map in spark using scala

Requirement :

There is a Map in scala which is being used by one of your application ,which have all the configurations for your spark app. you need to remove some of conf in Map.

Solution :

Step 1: Creation of Map

Map is the combination of key value pair. 

Let’s create one Map named as myconf

 var my_conf=Map("url"->"bigdataprogrammers.com","type"->"tutorials","lang"->"scala","page"->"scala-tutorials")

here keys are url,type ,lang and page and corresponding values are written next to this symbol (->) 

Step 2: Accessing values  of Map

once you ‘ve created the Map , you can access the values based on keys .

for example if you want to access url

you can write like my_conf(“url”)  where the key is url, this will return the value , in this case value is “bigdataprogrammmers.com”

see the below screenshot:

Step 3 : Filtering some key,values

Many times you may not need all the keys ,and want to filter out some configuration, so you can use filter in map ,using below command :

 my_conf.filter(x => (x._1=="page"))

In above code x is the tuple and we have two values in it , the first is a key and second one is value . To access the key we need to write x._1 and ,because we want to remove on the basis of key , we can use one conditional expression which gives bool result .And then return the new Map based on condition. Here we have removed the pair which has “page” key.

Step 4 : Assign it to new Map

You can assign it into new Map say new_conf

 new_conf=my_conf.filter(x => (x._1!="page"))

Now If you access on the basis of key “page”, It won’t be available as shown in above screenshot.

 

Sharing is caring!

Subscribe to our newsletter
Loading

Leave a Reply