📖 Scenario: You are working with Hadoop's Hive to organize data for a small online bookstore. You want to create a database and tables to store information about books and authors.
🎯 Goal: Create a Hive database called bookstore. Then create two tables inside this database: authors and books. The authors table should have columns author_id (int) and author_name (string). The books table should have columns book_id (int), title (string), and author_id (int) to link to the authors.
📋 What You'll Learn
Create a Hive database named bookstore
Create a table named authors with columns author_id (int) and author_name (string)
Create a table named books with columns book_id (int), title (string), and author_id (int)
Use the bookstore database before creating tables
💡 Why This Matters
🌍 Real World
Organizing data in Hive databases and tables helps manage large datasets in big data projects like online bookstores.
💼 Career
Knowing how to create and manage Hive databases and tables is essential for data engineers and analysts working with Hadoop ecosystems.
Progress0 / 4 steps
1
Create the bookstore database
Write a Hive query to create a database called bookstore.
Hadoop
Hint
Use the CREATE DATABASE statement followed by the database name.
2
Select the bookstore database
Write a Hive query to use the database bookstore before creating tables.
Hadoop
Hint
Use the USE statement to select the database.
3
Create the authors and books tables
Write Hive queries to create the authors table with columns author_id (int) and author_name (string), and the books table with columns book_id (int), title (string), and author_id (int).
Hadoop
Hint
Use CREATE TABLE with column names and types inside parentheses.
4
Display the tables in the bookstore database
Write a Hive query to show all tables in the bookstore database.
Hadoop
Hint
Use the SHOW TABLES; command to list tables in the current database.
Practice
(1/5)
1. What is the main purpose of creating a database in Hadoop?
easy
A. To run queries faster
B. To store data in rows and columns
C. To organize data into separate groups
D. To delete old data automatically
Solution
Step 1: Understand the role of a database
A database is used to organize data logically into separate groups or collections.
Step 2: Differentiate database from tables
Tables store data in rows and columns inside a database, but the database itself is for grouping.
Final Answer:
To organize data into separate groups -> Option C
Quick Check:
Database = Organize data groups [OK]
Hint: Databases group data; tables hold the data [OK]
Common Mistakes:
Confusing database with table
Thinking database stores data directly
Assuming database speeds up queries automatically
2. Which of the following is the correct syntax to create a database named sales_db in Hive?
easy
A. CREATE DATABASE sales_db;
B. MAKE DATABASE sales_db;
C. CREATE DB sales_db;
D. NEW DATABASE sales_db;
Solution
Step 1: Recall Hive syntax for database creation
The correct command to create a database in Hive is CREATE DATABASE database_name;.
Step 2: Check each option
Only CREATE DATABASE sales_db; matches the correct syntax exactly.
Final Answer:
CREATE DATABASE sales_db; -> Option A
Quick Check:
CREATE DATABASE = correct syntax [OK]
Hint: Use CREATE DATABASE, not MAKE or NEW [OK]
Common Mistakes:
Using MAKE or NEW instead of CREATE
Abbreviating DATABASE as DB
Missing semicolon at end
3. What will be the result of running this Hive query?
CREATE TABLE employees (id INT, name STRING, salary FLOAT);
medium
A. Creates a table named employees with three columns
B. Throws a syntax error due to missing database name
C. Creates a database named employees
D. Creates a table with no columns
Solution
Step 1: Analyze the CREATE TABLE statement
The query uses correct syntax to create a table named employees with columns id, name, and salary.
Step 2: Check for errors or missing parts
Database name is optional if default database is used; no syntax error here.
Final Answer:
Creates a table named employees with three columns -> Option A
Column definitions must be enclosed in parentheses after the table name.
Step 2: Check the given query
The query misses parentheses around id INT, name STRING, causing syntax error.
Final Answer:
Missing parentheses around column definitions -> Option B
Quick Check:
Columns need parentheses [OK]
Hint: Always use parentheses for columns in CREATE TABLE [OK]
Common Mistakes:
Forgetting parentheses
Assuming table name is wrong
Thinking data types are invalid
5. You want to create a database company_db and inside it a table departments with columns dept_id INT and dept_name STRING. Which sequence of commands is correct?
hard
A. CREATE TABLE company_db.departments (dept_id INT, dept_name STRING);