6 - Create Table
6 - Create Table
This chapter explains how to create a table and how to insert data into it. The
conventions of creating a table in HIVE is quite similar to creating a table using SQL.
Syntax
Example
Let us assume you need to create a table named employee using CREATE TABLE
statement. The following table lists the fields and their data types in employee table:
1 Eid int
2 Name String
3 Salary Float
4 Designation string
The following data is a Comment, Row formatted fields such as Field terminator,
Lines terminator, and Stored File type.
https://www.tutorialspoint.com/hive/hive_create_table.htm 1/6
4/2/24, 7:29 AM Hive - Create Table
The following query creates a table named employee using the above data.
hive> CREATE TABLE IF NOT EXISTS employee ( eid int, name String,
salary String, destination String)
COMMENT ‘Employee details’
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘\t’
LINES TERMINATED BY ‘\n’
STORED AS TEXTFILE;
If you add the option IF NOT EXISTS, Hive ignores the statement in case the table
already exists.
OK
Time taken: 5.905 seconds
hive>
JDBC Program
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
https://www.tutorialspoint.com/hive/hive_create_table.htm 2/6
4/2/24, 7:29 AM Hive - Create Table
Class.forName(driverName);
// get connection
Connection con = DriverManager.getConnection("jdbc:hive://localhost:100
// create statement
Statement stmt = con.createStatement();
// execute statement
stmt.executeQuery("CREATE TABLE IF NOT EXISTS "
+" employee ( eid int, name String, "
+" salary String, destignation String)"
+" COMMENT ‘Employee details’"
+" ROW FORMAT DELIMITED"
+" FIELDS TERMINATED BY ‘\t’"
+" LINES TERMINATED BY ‘\n’"
+" STORED AS TEXTFILE;");
Save the program in a file named HiveCreateDb.java. The following commands are
used to compile and execute this program.
$ javac HiveCreateDb.java
$ java HiveCreateDb
Output
While inserting data into Hive, it is better to use LOAD DATA to store bulk records.
There are two ways to load data: one is from local file system and second is from
https://www.tutorialspoint.com/hive/hive_create_table.htm 3/6
4/2/24, 7:29 AM Hive - Create Table
Syntax
Example
We will insert the following data into the table. It is a text file named sample.txt in
/home/user directory.
The following query loads the given text into the table.
OK
Time taken: 15.905 seconds
hive>
JDBC Program
Given below is the JDBC program to load given data into the table.
https://www.tutorialspoint.com/hive/hive_create_table.htm 4/6
4/2/24, 7:29 AM Hive - Create Table
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
// get connection
Connection con = DriverManager.getConnection("jdbc:hive://localhost:100
// create statement
Statement stmt = con.createStatement();
// execute statement
stmt.executeQuery("LOAD DATA LOCAL INPATH '/home/user/sample.txt'" + "O
System.out.println("Load Data into employee successful");
con.close();
}
}
Save the program in a file named HiveLoadData.java. Use the following commands
to compile and execute this program.
$ javac HiveLoadData.java
$ java HiveLoadData
Output:
https://www.tutorialspoint.com/hive/hive_create_table.htm 5/6
4/2/24, 7:29 AM Hive - Create Table
https://www.tutorialspoint.com/hive/hive_create_table.htm 6/6