How to create a table and input data in Hive?
Creating a table and inputting data in Hive can be accomplished by following these steps:
- Open either the Hive command line or the Hue interface.
- Create table structure: Use the CREATE TABLE statement to create a table and specify the column names and data types for the table.
- For example, create a table called “employees” with two columns: “name” as a string type and “age” as an integer type.
- Create a table called employees with columns for name and age.
- Importing data: There are various ways to import data.
- Import data from a local file: Use the LOAD DATA LOCAL INPATH statement to import data from a local file into a table. For example, if the local file /path/to/employees.csv contains employee information with each line in the format name, age, you can use the following command to import the data: LOAD DATA LOCAL INPATH ‘/path/to/employees.csv’ INTO TABLE employees;
- Import data from HDFS file: Use the LOAD DATA INPATH statement to import data from an HDFS file into a table. For example, if the HDFS file /user/hive/employees.csv contains employee information with each line formatted as name, age, you can use the following command to import the data: LOAD DATA INPATH ‘/user/hive/employees.csv’ INTO TABLE employees;
- Insert data directly: Use the INSERT INTO statement to insert data directly. For example, you can insert an employee record using the following command:
INSERT INTO employees VALUES (‘John’, 25); - Verify data import: You can use a SELECT statement to query the data in the table to confirm if the data has been successfully imported.
- You can use the following command to retrieve all data from the table:
- Show all employees.
The above are the basic steps for creating a table and entering data in Hive. Depending on the specific situation, choose to import data from local files, HDFS files, or directly insert data.