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:

  1. Open either the Hive command line or the Hue interface.
  2. Create table structure: Use the CREATE TABLE statement to create a table and specify the column names and data types for the table.
  3. For example, create a table called “employees” with two columns: “name” as a string type and “age” as an integer type.
  4. Create a table called employees with columns for name and age.
  5. Importing data: There are various ways to import data.
  6. 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;
  7. 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;
  8. 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);
  9. 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.
  10. You can use the following command to retrieve all data from the table:
  11. 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.

bannerAds