Hive practice (1) -- basics of hive Data Import/Export

Source: Internet
Author: User

Frontier:

Hive also uses SQL-like syntax, but as a data warehouse, hive is naturally different from traditional relational databases (MySQL/Oracle) for OLTP. it is used for offline data computing and analysis, without the pursuit of high concurrency/low latency application scenarios. in particular, hive's data is schema on read, which is very free and loose for Data Writing, while reading data is subject to various restrictions. while rmdbs is schema on write, which imposes strict data writing restrictions.

*). Data Import/Export
Let's see how to import the following hive data:
1) Create Database db_hive_test;
Create Database db_hive_test;
In the HDFS directory/user/hive/warehouse
[<Username >@< hostname> ~] # Sudo-u hdfs dfs-ls/user/hive/warehouse
Drwxrwxrwt-root hive 0 2014-07-02 10: 49/user/hive/warehouse/db_hive_test.db
  

2) create a data table tb_user

  CREATE TABLE tb_user (    user_id int,    username string,    age int,    phone string  ) ROW FORMAT DELIMITED   FIELDS TERMINATED BY ‘\t‘   STORED AS TEXTFILE;

3). Data Preparation
Edit data.txt in the workshop. The content is as follows:

  [<username>@

4) import data into hive

Load data local inpath '/path/to/data.txt' into Table db_hive_test.tb_user;

5). Data Verification
Select * From tb_user;
  

Is it easy? But why not use insert into to insert data into a single row? In fact, hive does not support insert and update operations on a single row ).
You can use
#) Load data syntax
Load data [local] inpath 'filepath' [overwrite] into Table tablename [partition (partcol1 = val1, partcol2 = val2...)]
#) Insert select syntax
Insert overwrite/into Table <table_name> [partition (partcol1 = val1, partclo2 = val2...)] select_statement
Note: overwrite rewrite and into append.

6) create a data table tb_newuser;

  CREATE TABLE tb_newuser (     user_id int,    username string,    age int,    phone string  ) ROW FORMAT DELIMITED   FIELDS TERMINATED BY ‘\t‘   STORED AS TEXTFILE;

7). Insert select

Insert into Table tb_newuser select * From tb_user;

8). Data Verification
Select * From tb_newuser;
  

Data Table Structure Replication
Create Table tb_newuser02 like tb_user;

Import when creating a table
Create Table tb_newuser01 select * From tb_user;

How to export data
Export Syntax: insert overwrite/into local directory '<directory> 'select_statement

Insert overwrite local directory '/path/to/data' select * From tb_user;
CAT/path/to/data/1_00_0
  

Insert overwrite directory '/path/to/data' select * From tb_user;
  

The difference between local and local is that one is stored locally and the other is stored on HDFS.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.