This document introduces how to create databases and tables in Hive on COS and CHDFS.
Development Preparation
Make sure you have activated Tencent Cloud and created an EMR cluster. When creating an EMR cluster, you need to select the Hive component in the software configuration interface.
The example includes content that requires access to Tencent COS. You can see Creating a Bucket to create a bucket in COS and enable COS authorization on the Instance Information page of the EMR console. The example includes content that requires access to Tencent Cloud CHDFS. You can see Mounting CHDFS to create a mount point and mount it to the EMR cluster. Using Hive to Create Databases and Tables in COS
Note:
EMR has integrated Hadoop-COS by default. After enabling COS authorization in the EMR console, you may use a temporary key tied to the Tencent Cloud EMR instance’s role to access COS, which is more secure compared to using a fixed key.
Method I: Creating the Entire Database on COS
Log in to the EMR cluster’s Master node, switch to the Hadoop user, and execute the following command to enter the Hive command line:
Execute the following command to create a database named hivewithcos in your COS bucket.
hive> create database hivewithcos location 'cosn://${bucketname}/${path}';
Note:
${bucketname} is the name of the COS bucket you created, and ${path} is the storage path.
View the execution result, and you will see the hivewithcos database created on COS.
hive> show databases;
OK
default
hivewithcos
Time taken: 0.094 seconds, Fetched: 2 row(s)
Create a data table named record (the method to load data into the table is the same as on HDFS):
hive> use hivewithcos;
hive> create table record(id int, name string) row format delimited fields terminated by ',' stored as textfile;
View the table:
hive> show tables;
OK
record
Time taken: 0.063 seconds, Fetched: 1 row(s)
Method II: Placing a Specific Table in COS
Create a database in Hive:
hive> create database test;
hive> use test;
Execute the following statement to create a table named record in the specified path of the COS bucket (the method to load data into the table is the same as on HDFS):
hive> create table record(id int, name string) row format delimited fields terminated by ',' stored as textfile location 'cosn://$bucketname/$path';
View the table:
hive> show tables;
OK
record
Time taken: 0.063 seconds, Fetched: 1 row(s)
Using Hive to Create Databases and Tables on CHDFS
Method I: Creating the Entire Database on CHDFS
Log in to the EMR cluster’s Master node, switch to the Hadoop user, and execute the following command to enter the Hive command line:
Execute the following command to create a database named hivewithofs in your CHDFS directory:
hive> create database hivewithofs location 'ofs://${mountpoint}/${path}';
Note:
${mountpoint} is the CHDFS mount address you created, and ${path} is the directory path.
View the execution result, and you will see the hivewithofs database created on CHDFS.
hive> show databases;
OK
default
hivewithofs
Time taken: 0.094 seconds, Fetched: 2 row(s)
Create a data table named record (the method to load data into the table is the same as on HDFS).
hive> use hivewithofs;
hive> create table record(id int, name string) row format delimited fields terminated by ',' stored as textfile;
View the table:
hive> show tables;
OK
record
Time taken: 0.063 seconds, Fetched: 1 row(s)
Method II: Placing a Specific Table in CHDFS
Create a database test2 in Hive:
hive> create database test2;
hive> use test2;
Execute the following statement to create a table named record in CHDFS:
hive> create table record(id int, name string) row format delimited fields terminated by ',' stored as textfile location 'cosn://$mountpoint/$path';
View the table:
hive> show tables;
OK
record
Time taken: 0.063 seconds, Fetched: 1 row(s)
Was this page helpful?