Hive

8 / 18

Hive - Saving Data




Not able to play video? Try with vimeo

We can save data from Hive tables or queries to the local file system as well as to HDFS. Let's save CMC stock data from nyse table to a file with the name onlycmc in the local file system. Login to CloudxLab Linux console and type Hive and wait for Hive prompt to appear. Select your database with use command. Run use followed by your database name. If your username is same as your database name you can use ${env:USER} which gets replaced with your username automatically. Type insert overwrite local directory '/home/${env:USER}/onlycmc' select * from nyse where symbol1 = 'CMC';, and press enter. Now, quit hive shell and type "tail onlycmc/000000_0" to see the CMC stock data. This is a file as well as folder that is just now created for exporting data from the select query. Now save the CMC stock data in onlycmc file in your home directory in HDFS. Again, open Hive shell in web console. Type insert overwrite directory 'onlycmc' select * from nyse where symbol1 = 'CMC'; and press enter. To see the data, use hadoop fs -ls command. The onlycmc directory should be in the list. To see the contents of this directory you can use hadoop fs -ls onlycmc. Further, you can take a look at the contents of the file using hadoop fs -cat command.

INSTRUCTIONS

Steps:

  • Login to the web console
  • Launch Hive with typing in hive on the console. Run below commands in Hive.
  • Use your database by using the below command. ${env:USER} gets replaced by your username automatically:

    use ${env:USER};
    
  • To save the data in the local file system

    insert overwrite local directory '/home/${env:USER}/onlycmc'
    select * from nyse where symbol1 = 'CMC';
    
  • To view this data type in the following commands (Run in the web console)

     tail onlycmc/000000_0
    
  • To save data in HDFS (Run in Hive)

    insert overwrite directory 'onlycmc' select * from nyse where
    symbol1 = 'CMC';
    

Loading comments...