Create an SQL Query that selects a limit of 10 from table in pyspark


Can anyone help with the following:

I have a csv file with data about the passengers on the titanic. I will put the code that I have so far and then the problem I’m having after.

#Initialise Sparksession and print the shell line to ensure Spark is running 
# You should get a print out something similar to - <pyspark.sql.session.SparkSession object at 0x7f1d98711460>
from pyspark.sql import SparkSession


# Print the tables in the catalog
# spark_ex.catalog.listDatabases()

# Import statement for pandas as pd
# import pyspark.sql.functions as sqlfunc
# from pyspark.sql.types import *
# from pyspark.sql import *
# import pyspark.sql.functions as sqlfunc
# import numpy as np
import pandas as pd

# Read in the train.csv using pd.read_csv and assign it to a variable called 'df1'

# Create a second dataframe 'df2' by slicing 'df1' with .iloc on an array of [:, 0:4] for the first 4 columns
df2=pd.DataFrame(df1.iloc[:, 0:4]) 

# Pass in the df2 frame into a variable called 'spark_df' using your spark_ex and .createDataFrame

# Using your now created spark_df dataframe, name the table 'sample_titanic' using .registerTempTable
# spark_df.registerTempTable("sample_titanic")
spark_df.createOrReplaceTempView("sample_titanic") #used this as I read in documentation that the .register is out of use

# Print the spark_df using .show

I think that up to this point everything is ok.

But then I am asked to do this:

1# Create a SQL Query that selects a limit of 10 from your Sample Titanic table

2# Assign a variable that ingests the limit 10 query using spark_ex.sql

for 1, the only answer I can find that works is this one: spark_df.limit(10).collect() but from what I understand this is not an SQL Query but none of the SQL queries I have tried have worked.

Then for 2 I am totally stumped at what I’m being asked to do and how to do it.

Any help would be much appreciated.


There are four ways (or four APIs) to write Spark SQL code: SQL, Python, Scala, and R. I will only talk about SQL and Python here (Scala and R is pretty as similar as Python)

To use Python "interface", you can refer to this API document An example of this is'A', 'B').groupBy('A').sum('B').count()

To use SQL "interface", you first have to create a temporary view as you did with spark_df.createOrReplaceTempView("sample_titanic"). From now on, you can write queries like this

spark.sql('select A, B from sample_titanic')

Each of these would return a dataframe that you can use with Python "interface", like this

df2 ='A', 'B')
df3 = spark.sql('...')

Good luck with your assignments 🙂

Answered By – pltc

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published