Thursday, August 17, 2023

Wordcount in Pyspark

 Wordcount in Pyspark:

 from pyspark.sql import SparkSession

spark=SparkSession.builder.appName("word_count").master("local[*]").getOrCreate() # 1. Read the text file into an RDD text_rdd= spark.sparkContext.textFile("E:\\source_files\\file1.txt") x= text_rdd.collect() print("base RDD reading the file contents line by line",x) #base RDD reading the file contents line by line ['Bangalore is Karnataka ', 'Chennai is Tamilnadu'] #2. Split the Lines into Words words = text_rdd.flatMap(lambda line: line.split()) y= words.collect() print("flatMap transformation Output",y) #flatMap transformation Output ['Bangalore', 'is', 'Karnataka', 'Chennai', 'is', 'Tamilnadu'] #3. Map each word to a key-value Pair with a count of 1 : (Create a list of Tuples by adding 1 to each word) word_counts = words.map( lambda word: (word,1) ) z=word_counts.collect() print("map transformation Output",z) #map transformation Output [('Bangalore', 1), ('is', 1), ('Karnataka', 1), ('Chennai', 1), ('is', 1), ('Tamilnadu', 1)] #4.Use reduceByKey function to count the occurenece of eah word. word_count_result = word_counts.reduceByKey(lambda a,b: (a+b) ) k = word_count_result.collect() print("reduceByKey transformation Output",k) #reduceByKey transformation Output [('is', 2), ('Karnataka', 1), ('Chennai', 1), ('Bangalore', 1), ('Tamilnadu', 1)] word_count_result.toDF().show() #word_count_result.saveAsTextFile("D:\\word_count_outputnew") ''' Output in Columnar Structure but with column heaaders as _1 and _2 +---------+---+ | _1| _2| +---------+---+ | is| 2| |Karnataka| 1| | Chennai| 1| |Bangalore| 1| |Tamilnadu| 1| +---------+---+ D:\word_count_outputnew\part-00000 ('is', 2) ('Karnataka', 1) ('Chennai', 1) D:\word_count_outputnew\part-00001 ('Bangalore', 1) ('Tamilnadu', 1) ''' for p,q in word_count_result.collect(): print(f"{p}: {q}") ''' is: 2 Karnataka: 1 Chennai: 1 Bangalore: 1 Tamilnadu: 1 ''' #one_liner of the above analysis text_rdd.flatMap(lambda line: line.split()).map( lambda word: (word,1) ).reduceByKey(lambda a,b: (a+b) ).toDF().show()




Sunday, July 23, 2023

EXISTS vs NOT EXISTS - Correlated Nested Query

 

--COR- RELATED NESTED QUERY:

--RAJANI schema

set lines 100

 

select * from emp;

 

       EID ENAME                          ADRRESS                      

---------- ------------------------------ ------------------------------

         1 Ravi                           Chandigarh                   

         2 Varun                          Delhi                         

         3 Nitin                          Pune                         

         4 Ravin                          Bangalore                    

         5 Ammy                           Chandigarh   

 

select * from project;

 

 

       EID PID        PNAME                          LOCATION           

---------- ---------- ------------------------------ --------------------

         1 P1         IOT                            Bangalore          

         5 P2         BIG DATA                       Delhi              

         3 P3         Retail                         Mumbai             

         4 P4         Android                        Hyderabad   

 

COR- RELATED NESTED QUERY:

In Subqueries we need a keyword like in/not in / Any /All operators is used to link between outer and inner query.

 

But in Corrreplated nested query we use  EXISTS / NOT EXISTS keyword to join between Outer and inner queries .

 

 

Q. Find the details of employees who is  working / assigned ( Use Exists) at least any one of the project. 

  

EXISTS Example:

 

select * from emp where

         exists ( select eid from project

                  where emp.eid =project.eid)

 

 

       EID ENAME                          ADRRESS                      

---------- ------------------------------ ------------------------------

         1 Ravi                           Chandigarh                   

         5 Ammy                           Chandigarh                   

         3 Nitin                          Pune                         

         4 Ravin                          Bangalore                    

 

 Steps :

1.  Outer query will execute first .

2.  It will take eid 1,2,3,4,5 from emp table one by one and compare with eid of project table for availability.

3.  Only eid 1,3,4,5 will match/available  in project table.

4. Hence for eid 1,3,4,5 the exists condition will become TRUE and rows will be retrived from emp table.

5. While comparing for eid= 2 from emp table (outer table)  with project table (inner table),

   there is no matching eid presentin project table ,

   so the inner query will return false for eid = 2 from project table.

5. Logically exists keyword will be replaced ( return) 'false' in this case for eid =2.

4. Hence except eid =2, all other records from emp table will be retrieved.

 

NOT EXISTS Example:

 

Q: Find the details of employees who is not working / not assigned ( Use NOT Exists)  any one of the project.  

 

select * from emp where

         not exists ( select eid from project

                  where emp.eid =project.eid)


        EID ENAME                          ADRRESS                      

---------- ------------------------------ ------------------------------

         2 Varun                          Delhi                        

  

 Steps :

1.  Outer query will execute first .

2.  It will take eid 1,2,3,4,5 from emp table one by one and compare with eid of project table for non-availability.

3.  eid 1,3,4,5 will match/available  in project table. So not exists condition become false .

4.  Hence for eid 1,3,4,5 the not exists condition will become FALSE and hence no rows will be retrived from emp table.

5.  While comparing for eid= 2 from emp table (outer table)  with project table (inner table),

   there is no matching  eid presentin project table, so the inner query will return TRUE  for eid = 2 from project table.

6. Logically not exists keyword will be replaced ( return)  'TRUE' in this case for eid =2.

7. Hence only  eid =2  record  from emp table will be retrieved..