Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

How to use sqlContext? Getting an error!

Solved Go to solution

How to use sqlContext? Getting an error!

New Contributor

sqlcontext.png Please find snapshot attached for the issue.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to use sqlContext? Getting an error!

@sudhir reddy

SqlContext is not available by default in Spark2 shell.

Create a sqlContext using below statement after launching the spark-shell and then you can read the json using this sqlContext.

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

Let me know if this helps.

10 REPLIES 10

Re: How to use sqlContext? Getting an error!

Super Mentor

@sudhir reddy

Are you using Spark2?

Have you set the Env variable as following before opening the shell?

# export SPARK_MAJOR_VERSION=2
# spark-shell

.

Re: How to use sqlContext? Getting an error!

New Contributor

Yes, using spark2. It is by default using Spark2. What else should I try? @Jay Kumar SenSharma

Re: How to use sqlContext? Getting an error!

Super Mentor

@sudhir reddy

Can you please share the complete output along with the command that you ran?

Re: How to use sqlContext? Getting an error!

New Contributor

val rd = sqlContext.read.json("/user/root/Example2.JSON"); //Used this command.

<console>:23: error: not found: value sqlContext val rd = sqlContext.read.json("/user/root/Example2.JSON"); ^ //Got this output @Jay Kumar SenSharm

Re: How to use sqlContext? Getting an error!

Super Mentor

@sudhir reddy

How are you logging in to the Shell? Which command are you using to login in to the spark shell ? Which mode?

For example when you run "spark-shell" then you will see lots of output ... we need that complete output.

Example

# spark-shell <br>

.

As much information you can provide about your issue ... there are possibilities that your issue will get resolved faster. Else there will be more interactions to know which HDP version are you using ? What is the logging happening? ...etc

Re: How to use sqlContext? Getting an error!

@sudhir reddy

SqlContext is not available by default in Spark2 shell.

Create a sqlContext using below statement after launching the spark-shell and then you can read the json using this sqlContext.

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

Let me know if this helps.

Re: How to use sqlContext? Getting an error!

New Contributor

Thanks! It worked.

Re: How to use sqlContext? Getting an error!

New Contributor
val sc: SparkContext // An existing SparkContext.
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val rd = sqlContext.read.json("user/root/Example2.JSON")

Re: How to use sqlContext? Getting an error!

New Contributor

Thank you!