Sunteți pe pagina 1din 4

nanclsloadLESSON-12

Hive Commands
·0 Class will start with refreshing the previous class with QA…. (30)
·1 Ensure all student successfully installed hive & in hive shell (20)
Today’s topics:
·2 Create, alter and drop table in hive ... … .. . (30)
·3 Create, load and display internal table.. .. .. (30)
·4 Create, load and display external table.. .. .. (30)
·5 Brief discussion – cloudera . .. (10)
·6 Questions / answers.. .. .. .. .. .. .. .. .. .. . (30)
Create, alter and drop table in hive
Remember
·7 Hive shells look like “hive>” in linux
·8 For terminating hive shell, use the command exit; or quit;
·9 All hive commands ends with “ ; “
·10 Successful command execution will show “OK” and time taken for the execution
with other data.
·11 hive commands as shown below are written with red font to be familiar with
hive commands.
·12 Commands are case in-sensitive.

Create table
Be in hadoopuser _-> run hadoop and yarn ->jps ->type 'hive' and press enter.
·13 create table employee(empid int, empname string);
# create a table “employee” with 2 columns with title and type of text.

·14 show tables; # The command will show the name of the table created.

·15 Describe employee; # will display the schema or, desc employee;
·16 show Databases; # The command will show the name of the database (in
this case the name of default Database is “default.”)
Alter table
·17 alter table employee rename to employeenew;

# Will change the name of table.

·18 show tables; # The command will show the name of the table
altered.

Drop table
·19 drop table employeenew; # This command will delete the table.
·20 Show tables; # No tables will be displayed

# will delete the table as mentioned. If it is internal table, it will delete the schema
and the data altogether. For external table, data is not lost only the schema will
be deleted.

Create, load and display internal table


Create a text file in local host
exit; # to exit from hive
Be in hadoop user
# Check all jps domain is working
# create a tab delimited file, use tab to seterate and down-arrow key to goto next line
nano /tmp/car # open a file under tmp with name car
Toyota camry
Honda crv
Nissan sunny # type with field separated by tab and followed by ^X, y,
hit ENTR for closing and saving

Switch to hive shell


Create a table with full schema (internal Table):
create table vehicle(mfg string, model string)
row format delimited
fields terminated by ‘\t’
stored as textfile; (# optional) # table schema created in
hive

Loading data from local file ( from local machine into hive)
load data local inpath ‘/tmp/car’ into table vehicle; # loading data to
table.

"local" -- keyword will not be used above if data is loaded from HDFS

Display the table


Select * from vehicle; # To display the content of full table.

# Check the table under user/hive/warehousehdfs / in hadoops

, where hive will save the table by default.

drop table vehicle; # This command will delete the table.


# Check the table again under user/hive/warehouse, and the table will be found to be
deleted.

Create, load and display external table


Create a text file in hdfs
exit; # to exit from hive
Be in hadoop user
# Check all jps domain/services are working
nano student # open a file – “student”
roshan rajen
jabin bijen
costa sapin
sam madhu # type and followed by ^X, y, hit ENTR for closing and
saving
hadoop fs –put student /usr/hive/warehouse # copy the file
in hdfs

Switch to hive shell

Create a table with full schema (create an external table in hive and load data from
HDFS into this external file --data coming from HDFS which is external source, thats why
data will be saved in a external type file in hive) type external file
create external table pupil(fname string, lname string)
row format delimited
fields terminated by ‘\t’
stored as textfile; (# optional) # table schema created in hive

Loading data from external file


load data inpath ‘hdfs:/usr/hive/warehouse/student’ into table pupil;

# loading external data to table.

Display the table


Select * from pupil; # display the content of full table.

drop table pupil; # This command will delete the


schema from hive shell
but will keep the table in hive
warehouse

# Check the table again under user/hive/warehouse, and the table will be found to be
existed.

Brief discussion – cloudera


Hive and hue
Useful link
https://www.guru99.com/hiv e-create-alter-drop-table.html

S-ar putea să vă placă și