Hive 简明教程
Hive - Create Database
Hive 是一种数据库技术,用于定义可分析结构化数据的数据库和数据表。结构化数据分析的主题是将数据以表格形式进行存储,并传递查询以进行分析。本章解释了如何创建 Hive 数据库。Hive 包含一个名为 default 的默认数据库。
Hive is a database technology that can define databases and tables to analyze structured data. The theme for structured data analysis is to store the data in a tabular manner, and pass queries to analyze it. This chapter explains how to create Hive database. Hive contains a default database named default.
Create Database Statement
Create Database 是一款用于在 Hive 中创建数据库的语句。Hive 中的数据库是 namespace 或一系列数据表。此语句的 syntax 如下:
Create Database is a statement used to create a database in Hive. A database in Hive is a namespace or a collection of tables. The syntax for this statement is as follows:
CREATE DATABASE|SCHEMA [IF NOT EXISTS] <database name>
此处,IF NOT EXISTS 是一个可选项,用于通知用户是否已存在同名数据库。我们可以在此命令中使用 SCHEMA 代替 DATABASE。执行以下查询可创建名为 userdb 的数据库:
Here, IF NOT EXISTS is an optional clause, which notifies the user that a database with the same name already exists. We can use SCHEMA in place of DATABASE in this command. The following query is executed to create a database named userdb:
hive> CREATE DATABASE [IF NOT EXISTS] userdb;
or
or
hive> CREATE SCHEMA userdb;
使用以下查询可验证数据库列表:
The following query is used to verify a databases list:
hive> SHOW DATABASES;
default
userdb
JDBC Program
以下为创建数据库的 JDBC 程序。
The JDBC program to create a database is given below.
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveCreateDb {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
// Register driver and create driver instance
Class.forName(driverName);
// get connection
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
Statement stmt = con.createStatement();
stmt.executeQuery("CREATE DATABASE userdb");
System.out.println(“Database userdb created successfully.”);
con.close();
}
}
将程序保存到名为 HiveCreateDb.java 的文件中。使用以下命令编译并执行此程序。
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