SparkSQL – Creating and Dropping Tables

This tutorial will show you how to use sqlContext in Spark SQL to create and drop tables. Please review the previous sessions on “Spark” to gain a better understanding of “Spark SQL” and “sqlContext.”

scala> sqlContext.sql(
            “CREATE TABLE emp(
                empno int,
                ename string,
                job string,
                mgr int,
                hiredate string,
                sal double,
                comm double,
                deptno int)
            ROW FORMAT DELIMITED
            FIELDS TERMINATED BY ‘,’
            LINES TERMINATED BY ‘\n’
            “);

If you want to create in the traditional SQL approach, use the following command.

scala> sqlContext.sql(
                “CREATE TABLE emptest(
                   empno int,
                   ename string,
                   locid int)”
                   );

scala> sqlContext.sql(
               “drop table emp”) 

To avoid each line being executed, please type the above command in a single line as shown in the image.

TableCreationsSparkSQL

Hope you find this article helpful.

Subscribing to this site will allow you to receive quick updates on future articles.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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