Hive - Create Database & Drop Database
2017-01-20 16:16
513 查看
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.
Hive是一种数据库技术,可以定义数据库和表来分析结构化数据。 结构化数据分析的主题是以表格方式存储数据,并传递查询以对其进行分析。 本章介绍如何创建Hive数据库。 Hive包含一个名为default的默认数据库。
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:
or
The following query is used to verify a databases list:
Save the program in a file named HiveCreateDb.java. The following commands are used to compile and execute this program.
The following queries are used to drop a database. Let us assume that the database name is userdb.
The following query drops the database using CASCADE. It means dropping respective tables before dropping the database.
下面以级联的形式删除数据库,这意味着在删除数据库之前删除相应的表。
The following query drops the database using SCHEMA.
This clause was added in Hive 0.6.
Save the program in a file named HiveDropDb.java. Given below are the commands to compile and execute this program.
Hive是一种数据库技术,可以定义数据库和表来分析结构化数据。 结构化数据分析的主题是以表格方式存储数据,并传递查询以对其进行分析。 本章介绍如何创建Hive数据库。 Hive包含一个名为default的默认数据库。
Create Database Statement
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>
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
hive> CREATE SCHEMA userdb;
The following query is used to verify a databases list:
hive> SHOW DATABASES; default userdb
JDBC Program
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(); } }
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:
Database userdb created successfully. -------------------------------------------------
Drop Database Statement
Drop Database is a statement that drops all the tables and deletes the database. Its syntax is as follows:DROP DATABASE StatementDROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
The following queries are used to drop a database. Let us assume that the database name is userdb.
hive> DROP DATABASE IF EXISTS userdb;
The following query drops the database using CASCADE. It means dropping respective tables before dropping the database.
下面以级联的形式删除数据库,这意味着在删除数据库之前删除相应的表。
hive> DROP DATABASE IF EXISTS userdb CASCADE;
The following query drops the database using SCHEMA.
hive> DROP SCHEMA userdb;
This clause was added in Hive 0.6.
JDBC Program
The JDBC program to drop 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 HiveDropDb { 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("DROP DATABASE userdb"); System.out.println(“Drop userdb database successful.”); con.close(); } }
Save the program in a file named HiveDropDb.java. Given below are the commands to compile and execute this program.
$ javac HiveDropDb.java $ java HiveDropDb
Output:
Drop userdb database successful.
相对比较简单,主要是熟悉两个脚本的写法就够日常开发使用了,如果有SQL基础(不管是mssql,还是MySQL)这些一看就懂了。 ------------ 英文地址:https://www.tutorialspoint.com/hive/hive_drop_database.htm
相关文章推荐
- Linux多线程编程实例解析 【转】
- linux下 gcc编译初学者使用教程
- 记一次Centos服务器被挂马的抓马经历
- 自定义组合控件TopBar
- linux makefile 创建 编译学习
- opencv学习(三十)之设计线性滤波器filter2D
- 2017,Mesos和kubernetes
- 22_AOP_切面——静态切面
- Linux ftp 命令
- IDEA 工具tomcat服务器配置
- openstack之keystone部署
- 个人笔记:个人linuxPC登录aws服务器及复制粘贴文件到服务器
- Linux下select()系统调用
- linux上客户端绑定固定端口请求server
- LINUX通过ODBC连接MySQL
- Linux下librtmp使用及编程实战
- Linux INotif机制详解及实例代码
- CentOS安装JDK1.8
- Apache Spark 2.0三种API的传说:RDD、DataFrame和Dataset
- nginx的安装与配置