Monday, 18 April 2016

Hive

Hive:
It organizes data into tables, which provides a means for attaching a structure to data stored in HDFS.
MetaData such as table schema is stored in the a datastore called MetaStore.

Key Points:
1. Data is stored in thetables.
2. Schema is stored in the meta store.
3. Table definitions are local to your machine, so they cannot be shared with other users.
4. The prerequisite for installing the Hive is, the version of the hadoop and the version of the cluster must be same.

To launch hive shell go to the terminal window and type hive and press enter, the prompt will change to hive

hive>

now you are in the hive Environment.

Basically the shell is the way to interact with the Hive. We give commands in this shell. this shell is called HiveQL(Hive Query Language ) its a dialect of SQL. it is influenced by MySQL.

Some basic Commands:
1. hive> show tables;

key points:
1. This command show the tables that are created
2. Hive SQL is a case sensitive. it is only sensitive for the string literals.
3. The database stores its files in a directory called metastore_db, which is relative to the location from which you ran.

to run hive in a non interactive mode, type command as follows
Ex: hive -f file1.q

for short cut scripts, you can use the -e option to specify the commands inline, in which case the final semicolon is not required.

Ex: hive -e 'select * from tab1'

Example:
create a file tab1.txt and write the word hadoop in the text file.
now go to the hive prompt and execute the following Query:

hive -e "create tble tab1(value string);\
              load data local inpath '/tmp/tab1'
               overwrite into table tab1"

Note: Every time when a query is fired, a standard error is printed in the both interactive and non interactive mode such as 'the time taken to run a query'

Example: hive -s -e 'select * from tab1'

Note: in hive we can save the files to the local system and as well as in HDFS.

Example: create table student(sid int,sname,smobile int)  ------------ 1
                 row format delimited                                         ------------ 2
                 fields terminated by '\t';                                     ------------ 3

1. Normal SQL query for creating a table called 'student' with fields id,sname,smobile.
2. Row format clause states that 'each row in the data file is delimited by text'.
3. It expects that there should be 3  fields in each row with types pecified and fields separated by tabs and rows by new lines.

Now we populate data:

Load data local inpath 'input/proc/myex/student.txt' overwrite into table student;

Note: Tables are stored as directories under Hive's warehouse directory, which is controlled by the
  hive.metastore.warehouse.dir property
and Defaults to : /user/hive/warehouse

so the 'student' file is stored in the
/user/hive/warehouse/student/student.txt

Configuration:
hive --config/user/username/dev/hive-conf





No comments:

Post a Comment