Posts

Showing posts with the label Spark SQL

Featured Post

Python: Built-in Functions vs. For & If Loops – 5 Programs Explained

Image
Python’s built-in functions make coding fast and efficient. But understanding how they work under the hood is crucial to mastering Python. This post shows five Python tasks, each implemented in two ways: Using built-in functions Using for loops and if statements ✅ 1. Sum of a List ✅ Using Built-in Function: numbers = [ 10 , 20 , 30 , 40 ] total = sum (numbers) print ( "Sum:" , total) 🔁 Using For Loop: numbers = [ 10 , 20 , 30 , 40 ] total = 0 for num in numbers: total += num print ( "Sum:" , total) ✅ 2. Find Maximum Value ✅ Using Built-in Function: values = [ 3 , 18 , 7 , 24 , 11 ] maximum = max (values) print ( "Max:" , maximum) 🔁 Using For and If: values = [ 3 , 18 , 7 , 24 , 11 ] maximum = values[ 0 ] for val in values: if val > maximum: maximum = val print ( "Max:" , maximum) ✅ 3. Count Vowels in a String ✅ Using Built-ins: text = "hello world" vowel_count = sum ( 1 for ch in text if ch i...

Spark SQL Query how to write it in Ten steps

Image
Spark SQL example The post tells how to write SQL query in Spark and explained in ten steps.This example demonstrates how to use sqlContext.sql to create and load two tables and select rows from the tables into two DataFrames. The next steps use the DataFrame API to filter the rows for salaries greater than 150,000 from one of the tables and shows the resulting DataFrame. Then the two DataFrames are joined to create a third DataFrame. Finally the new DataFrame is saved to a Hive table. 1. At the command line, copy the Hue sample_07 and sample_08 CSV files to HDFS: $ hdfs dfs -put HUE_HOME/apps/beeswax/data/sample_07.csv /user/hdfs $ hdfs dfs -put HUE_HOME/apps/beeswax/data/sample_08.csv /user/hdfs where HUE_HOME defaultsto /opt/cloudera/parcels/CDH/lib/hue (parcel installation) or /usr/lib/hue (package installation). 2. Start spark-shell: $ spark-shell 3. Create Hive tables sample_07 and sample_08: scala> sqlContext.sql("CREATE TABLE sample_07 (code string...