Spark SQL – Working With Variables

This post will show you how to use Scala with Spark SQL to define variables and assign values to them.

Variables are just reserved memory locations where values can be stored. This means that when you create a variable, you reserve some memory for it. The compiler allocates memory and decides what can be stored in reserved memory based on the data type of a variable. As a result, you can store integers, decimals, or characters in variables by assigning multiple data types to them. Additionally, the data type of the variable will be determined by the value assigned to it.

Let’s see practically how to declare the variables and assign values to them.

scala> var x=20
x: Int = 20

scala> var y=30
y: Int = 30

scala> x+y
res6: Int = 50

scala> x-y
res7: Int = -10

scala> x*y
res8: Int = 600

scala> (x+y)/(y-x)
res9: Int = 5

Variables X and Y were identified as integers in the preceding example based on the values provided to them. See the below examples-

scala> var x : String = “Hello”
x: String = Hello

scala> var y : String = “World”
y: String = World

scala> println(x,y)

In the above example, we specified the data-types for the variables “x” and “y”.

Hope you find this article helpful.

Please follow us for more interesting updates.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s