(一)简单入门

1、创建一个表

create table if not exists ljh_emp(

name string,

salary float,

gender string)

comment 'basic information of a employee'

row format delimited fields terminated by ',’;



2、准备数据文件

创建test目录且目录只有一个文件,文件内容如下:

ljh,25000,male

jediael,25000,male

llq,15000,female



3、将数据导入表中

load data local inpath '/home/ljhn1829/test' into table ljh_emp;



4、查询表中的内容

select * from ljh_emp;

OK

ljh    25000.0    male

jediael    25000.0    male

llq    15000.0    female

Time taken: 0.159 seconds, Fetched: 3 row(s)



(二)关于分隔符

1、默认分隔符

hive中的行默认分隔符为 \n,字段分隔符为 ctrl+A,此外还有ctrl+B,ctrl+C,可以用于分隔array,struct,map等,详见《hive编程指南》P44。

因此,若在建表是不指定row format delimited fields terminated by ‘,’,则认为默认字段分隔符为ctrl+A。

可以有2种解决方案:

一是在创建表时指定分隔符,如上例所示,

二是在数据文件中使用ctrl+A,见下例



2、在数据文件中使用ctrl+A全分隔符

(1)创建表

create table ljh_test_emp(name string, salary float, gender string);

(2)准备数据文件

创建test2目录,目录下只有一个文件,文件内容如下:

ljh^A25000^Amale

jediael^A25000^Amale

llq^A15000^Afemale

其中的^A字符仅在vi时才能看到,cat不能看到。

输出^A的方法是:在vi的插入模式下,先按ctrl+V,再按ctrl+A

(3)将数据导入表

create table ljh_test_emp(name string, salary float, gender string);

(4)查询数据

hive> select * from ljh_test_emp;

OK

ljh    25000.0    male

jediael    25000.0    male

llq    15000.0    female

Time taken: 0.2 seconds, Fetched: 3 row(s)



3、未指定分隔符,且又未使用ctrl+A作文件中的分隔符,出现以下错误

(1)创建表

create table if not exists ljh_emp_test(

name string,

salary float,

gender string)

comment 'basic information of a employee’;

(2)准备数据

ljh,25000,male

jediael,25000,male

llq,15000,female

(3)将数据导入表中

load data local inpath '/home/ljhn1829/test' into table ljh_emp_test;

(4)查看表中数据

select * from ljh_emp_test;

OK

ljh,25000,male    NULL    NULL

jediael,25000,male    NULL    NULL

llq,15000,female    NULL    NULL

Time taken: 0.185 seconds, Fetched: 3 row(s)

可以看出,由于分隔符为ctrl+A,因此导入数据时将文件中的每一行内容均只当作第一个字段,导致后面2个字段均为null。



(三)复杂一点的表

1、创建表

create table employees (

    name string,

    slalary float,

    suboddinates array<string>,

    deductions map<string,float>,

    address struct<stree:string, city:string, state:string, zip:int>

)

partitioned by(country string, state string);



2、准备数据

John Doe^A100001.1^AMary Smith^BTodd Jones^AFederal Taxes^C.2^BStateTaxes^C.05^BInsurance^C.1^A1 Michigan Ave.^BChicago^BIL^B60600

Mary Smith^A80000.0^ABill King^AFederal Taxes^C.2^BState Taxes^C.05^BInsurance^C.1^A100 Ontario St.^BChicago^BIL^B60601

Todd Jones^A70000.0^A^AFederal Taxes^C.15^BState Taxes^C.03^BInsurance^C.1^A200 Chicago Ave.^BOak Park^BIL^B60700

Bill King^A60001.0^A^AFederal Taxes^C.15^BState Taxes^C.03^BInsurance^C.1^A300 Obscure Dr.^BObscuria^BIL^B60100

注意  ^A:分隔字段 ^B:分隔array/struct/map中的元素 ^C:分隔map中的KV

详见《hive编程指南》P44。



3、将数据导入表中

load data local inpath '/home/ljhn1829/phd' into table employees partition(country='us',state='ca');



4、查看表数据

hive> select * from employees;

OK

John Doe    100001.1    ["Mary Smith","Todd Jones"]    {"Federal Taxes":0.2,"StateTaxes":0.05,"Insurance":0.1}    {"stree":"1 Michigan Ave.","city":"Chicago","state":"IL","zip":60600}    us    ca

Mary Smith    80000.0    ["Bill King"]    {"Federal Taxes":0.2,"State Taxes":0.05,"Insurance":0.1}    {"stree":"100 Ontario St.","city":"Chicago","state":"IL","zip":60601}    us    ca

Todd Jones    70000.0    []    {"Federal Taxes":0.15,"State Taxes":0.03,"Insurance":0.1}    {"stree":"200 Chicago Ave.","city":"Oak Park","state":"IL","zip":60700}    us    ca

Bill King    60001.0    []    {"Federal Taxes":0.15,"State Taxes":0.03,"Insurance":0.1}    {"stree":"300 Obscure Dr.","city":"Obscuria","state":"IL","zip":60100}    us    ca

Time taken: 0.312 seconds, Fetched: 4 row(s)



5、查看hdfs中的文件

hadoop fs -ls /data/gamein/g4_us/meta/employees/country=us/state=ca

Found 1 items

-rwxr-x---   3 ljhn1829 g4_us        428 2015-05-12 12:49 /data/gamein/g4_us/meta/employees/country=us/state=ca/progamming_hive_data.txt

该文件中的内容与原有文件一致。





(四)通过select子句插入数据

1、创建表

create table employees2 (

    name string,

    slalary float,

    suboddinates array<string>,

    deductions map<string,float>,

    address struct<stree:string, city:string, state:string, zip:int>

)

partitioned by(country string, state string);



2、插入数据

hive>  set hive.exec.dynamic.partition.mode=nonstrict;

否则会出现以下异常:

FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict



insert into table employees2

partition (country,state)

select name,slalary,suboddinates,deductions,address, e.country, e.state

from employees e;

版权声明:本文为博主原创文章,未经博主允许不得转载。

HIVE快速入门 分类: B4_HIVE 2015-06-06 11:27 59人阅读 评论(0) 收藏的更多相关文章

  1. Retinex系列之McCann99 Retinex 分类: 图像处理 Matlab 2014-12-03 11:27 585人阅读 评论(0) 收藏

    一.McCann99 Retinex McCann99利用金字塔模型建立对图像的多分辨率描述,自顶向下逐层迭代,提高增强效率.对输入图像的长宽有 严格的限制,要求可表示成 ,且 ,. 上述限制来源于金 ...

  2. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  3. 百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 346人阅读 评论(0) 收藏

    在百度编辑器示例代码基础上进行了修改,封装成类库,只需简单配置即可使用. 完整demo下载 版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. Train Problem I 分类: HDU 2015-06-26 11:27 10人阅读 评论(0) 收藏

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  6. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...

  7. Segment Tree with Lazy 分类: ACM TYPE 2014-08-29 11:28 134人阅读 评论(0) 收藏

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; stru ...

  8. 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  9. C语言之void类型及void指针 分类: C/C++ 2015-07-13 11:24 8人阅读 评论(0) 收藏

    原文网址:http://www.cnblogs.com/pengyingh/articles/2407267.html 1.概述 许多初学者对C/C 语言中的void及void指针类型不甚理解,因此在 ...

随机推荐

  1. 分享js寄生组合模式继承

    function person(){ this.name = 'taobao'; this.showMess = function(){ return this.name; } } person.pr ...

  2. Shiro + SSM(框架) + Freemarker(jsp)

    Shiro + SSM(框架) + Freemarker(jsp)讲解的权限控制Demo,还不赶快去下载? 我们知道Ajax不能做页面redirect和forward跳转,所以Ajax请求假如没登录, ...

  3. xml格式报文的拼装,和解析成实体类

    我们的微信支付,使用的是第三方的支付,某银行的微信支持渠道.所有的接口请求.应答都是xml格式报文,这样就需要用到xml格式报文的拼装和解析,这儿简单讲一下. 拼接xml格式报文. 从页面表单提交和配 ...

  4. JQuery之为某个div加入行样式

    JQuery都是以$符号开头的.当然能够用jQuery取代$符号,他们是恒等的,同一时候也是相等的.()事实上就是一个方法,里面能够传递匿名函数等,选取某个div时,如id为div1则用$('#div ...

  5. 程序猿果真有前端后端client吗

    前端 后端 client DBA OP 程序猿有分这么细的吗? 入行时候有区别. 殊途同归 吾道一以贯之, 假设作为程序猿不能领悟一贯, 则永远不清楚.

  6. animation- 动画效果实现(xml形式实现)

    1.定义xml动画 1)在anim文件夹下定义xml文件 解释:这个文件夹下面的文件会被默认为动画文件,如果这个文件不存在,需要自己创建 display_result_anim.xml <?xm ...

  7. 洛谷 P1755 斐波那契的拆分

    P1755 斐波那契的拆分 题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个 ...

  8. “ping”命令的原理就是向对方主机发送UDP数据包,HTTP在每次请求结束后都会主动释放连接,因此HTTP连接是一种“短连接”

    Socket  是一套建立在TCP/IP协议上的接口不是一个协议 应用层:  HTTP  FTP  SMTP  Web 传输层:  在两个应用程序之间提供了逻辑而不是物理的通信(TCP  UDP) T ...

  9. Node组装启动过程

    elasticsearch的启动过程是根据配置和环境组装需要的模块并启动的过程.这一过程就是通过guice注入各个功能模块并启动这些模块,从而得到一个功能完整的node.正如之前所说elasticse ...

  10. 支持10W高并发请求的IIS Web服务器常用设置

    支持高并发的IIS Web服务器常用设置   适用的IIS版本:IIS 7.0, IIS 7.5, IIS 8.0 适用的Windows版本:Windows Server 2008, Windows ...