ÔÆÌ⺣ - רҵÎÄÕ·¶ÀýÎĵµ×ÊÁÏ·ÖÏíÆ½Ì¨

µ±Ç°Î»ÖãºÊ×Ò³ > Hiveѧϰ±Ê¼Ç

Hiveѧϰ±Ê¼Ç

  • 62 ´ÎÔĶÁ
  • 3 ´ÎÏÂÔØ
  • 2026/1/27 14:13:25

¹ØÓÚÀ´Ô´µÄÎı¾Êý¾ÝµÄ×ֶμä¸ô·û

Èç¹ûÒª½«×Ô¶¨Òå¼ä¸ô·ûµÄÎļþ¶ÁÈëÒ»¸ö±í£¬ÐèҪͨ¹ý´´½¨±íµÄÓï¾äÀ´Ö¸Ã÷ÊäÈëÎļþ¼ä¸ô·û£¬È»ºóload dataµ½Õâ¸ö±í¾ÍokÁË¡£

2.6 Insert

2.6.1 Inserting data into Hive Tables from queries

Standard syntax: INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement Hive extension (multiple inserts): FROM from_statement INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 [INSERT OVERWRITE TABLE tablename2 [PARTITION ...] select_statement2] ... Hive extension (dynamic partition inserts): INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement

Insertʱ£¬from×Ó¾ä¼È¿ÉÒÔ·ÅÔÚselect×Ó¾äºó£¬Ò²¿ÉÒÔ·ÅÔÚinsert×Ó¾äǰ£¬ÏÂÃæÁ½¾äÊǵȼ۵Ä

hive> FROM invites a INSERT OVERWRITE TABLE events SELECT a.bar, count(*) WHERE a.foo > 0 GROUP BY a.bar; hive> INSERT OVERWRITE TABLE events SELECT a.bar, count(*) FROM invites a WHERE a.foo > 0 GROUP BY a.bar; hiveûÓÐÖ±½Ó²åÈëÒ»ÌõÊý¾ÝµÄsql£¬²»¹ý¿ÉÒÔͨ¹ýÆäËû·½·¨ÊµÏÖ£º ¼ÙÉèÓÐÒ»ÕűíBÖÁÉÙÓÐÒ»ÌõÊý¾Ý£¬ÎÒÃÇÏëÏò±íA£¨int£¬string£©ÖвåÈëÒ»ÌõÊý¾Ý£¬¿ÉÒÔÓÃÏÂÃæµÄ·½·¨ÊµÏÖ£º from B

insert table A select 1£¬¡®abc¡¯ limit 1£»

ÎÒ¾õµÃhiveºÃÏñ²»Äܹ»²åÈëÒ»¸ö¼Ç¼£¬ÒòΪÿ´ÎÄãдinsertÓï¾äµÄʱºò¶¼ÊÇÒª½«Õû¸ö±íµÄÖµoverwrite¡£ÎÒÏëÕâ¸öÓ¦¸ÃÊÇÓëhiveµÄstorage layerÊÇÓйØÏµµÄ£¬ÒòΪËüµÄ´æ´¢²ãÊÇHDFS£¬²åÈëÒ»¸öÊý¾ÝҪȫ±íɨÃ裬»¹²»ÈçÓÃÕû¸ö±íµÄÌæ»»À´µÄ¿ìЩ¡£

21

Hive²»Ö§³ÖÒ»ÌõÒ»ÌõµÄÓÃinsertÓï¾ä½øÐвåÈë²Ù×÷£¬Ò²²»Ö§³ÖupdateµÄ²Ù×÷¡£Êý¾ÝÊÇÒÔloadµÄ·½Ê½£¬¼ÓÔØµ½½¨Á¢ºÃµÄ±íÖС£Êý¾ÝÒ»µ©µ¼È룬Ôò²»¿ÉÐ޸ġ£ÒªÃ´dropµôÕû¸ö±í£¬ÒªÃ´½¨Á¢ÐÂµÄ±í£¬µ¼ÈëеÄÊý¾Ý¡£

2.6.2 Writing data into filesystem from queries

Standard syntax: INSERT OVERWRITE [LOCAL] DIRECTORY directory1 SELECT ... FROM ... Hive extension (multiple inserts): FROM from_statement INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1 [INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2] ...

µ¼³öÎļþµ½±¾µØ INSERT OVERWRITE LOCAL DIRECTORY '/tmp/local_out' SELECT a.* FROM pokes a;

µ¼³öÎļþµ½HDFS

INSERT OVERWRITE DIRECTORY '/user/admin/SqlldrDat/CnClickstat/20101101/19/clickstat_gp_fatdt0/0' SELECT a.* FROM c02_clickstat_fatdt1 a WHERE dt=¡¯20101201¡¯;

Ò»¸öÔ´¿ÉÒÔͬʱ²åÈëµ½¶à¸öÄ¿±ê±í»òÄ¿±êÎļþ£¬¶àÄ¿±êinsert¿ÉÒÔÓÃÒ»¾ä»°À´Íê³É

FROM src INSERT OVERWRITE TABLE dest1 SELECT src.* WHERE src.key < 100 INSERT OVERWRITE TABLE dest2 SELECT src.key, src.value WHERE src.key >= 100 and src.key < 200 INSERT OVERWRITE TABLE dest3 PARTITION(ds='2008-04-08', hr='12') SELECT src.key WHERE src.key >= 200 and src.key < 300 INSERT OVERWRITE LOCAL DIRECTORY '/tmp/dest4.out' SELECT src.value WHERE src.key >= 300; Eg:

from xi

insert overwrite table test2 select '1,2,3' limit 1 insert overwrite table d select '4,5,6' limit 1;

22

2.7 Cli

2.7.1 Hive Command line Options

$HIVE_HOME/bin/hiveÊÇÒ»¸öshell¹¤¾ß£¬Ëü¿ÉÒÔÓÃÀ´ÔËÐÐÓÚ½»»¥»òÅú´¦Àí·½Ê½ÅäÖõ¥Ôª²éѯ¡£ Óï·¨£º Usage: hive [-hiveconf x=y]* [<-i filename>]* [<-f filename>|<-e query-string>] [-S] -i Initialization Sql from file (executed automatically and silently before any other commands) -e 'quoted query string' Sql from command line -f Sql from file -S Silent mode in interactive shell where only data is emitted -hiveconf x=y Use this to set hive/hadoop configuration variables. -e and -f cannot be specified together. In the absence of these options, interactive shell is started. However, -i can be used with any other options. Multiple instances of -i can be used to execute multiple init scripts. To see this usage help, run hive -h

ÔËÐÐÒ»¸ö²éѯ£º

$HIVE_HOME/bin/ hive -e 'select count(*) from c02_clickstat_fatdt1' Example of setting hive configuration variables

$HIVE_HOME/bin/hive -e 'select a.col from tab1 a' -hiveconf hive.exec.scratchdir=/home/my/hive_scratch -hiveconf mapred.reduce.tasks=32 ½«²éѯ½á¹ûµ¼³öµ½Ò»¸öÎļþ

23

HIVE_HOME/bin/hive -S -e ' select count(*) from c02_clickstat_fatdt1' > a.txt ÔËÐÐÒ»¸ö½Å±¾

HIVE_HOME/bin/hive -f /home/my/hive-script.sql Example of running an initialization script before entering interactive mode

HIVE_HOME/bin/hive -i /home/my/hive-init.sql

2.7.2 Hive interactive Shell Command

Command quit set = Description ʹÓà quit or exit Í˳ö ʹÓÃÕâ¸ö·½Ê½À´ÉèÖÃÌØ¶¨µÄÅäÖñäÁ¿µÄÖµ¡£ÓÐÒ»µãÐèҪעÒâµÄÊÇ£¬Èç¹ûÄãÆ´´íÁ˱äÁ¿Ãû£¬CLI½«²»»áÏÔʾ´íÎó¡£ Õ⽫´òÓ¡µÄÅäÖñäÁ¿£¬Èç¹ûûÓÐÖ¸¶¨±äÁ¿ÔòÓÉÏÔʾHIVEºÍÓû§±äÁ¿¡£Èçset I ÔòÏÔʾiµÄÖµ,setÔòÏÔʾhiveÄÚ²¿±äÁ¿Öµ This will give all possible hadoop/hive configuration variables. Adds a file to the list of resources. list all the resources already added set set -v add FILE * list FILE list FILE * Check given resources are already added or not. ! execute a shell command from hive shell dfs execute dfs command command from hive shell Eg: executes hive query and prints results to stdout hive> set i=32; hive> set i; hive> select a.* from xiaojun a; hive> !ls; 24

ËÑË÷¸ü¶à¹ØÓÚ£º Hiveѧϰ±Ê¼Ç µÄÎĵµ
  • ÊÕ²Ø
  • Î¥¹æ¾Ù±¨
  • °æÈ¨ÈÏÁì
ÏÂÔØÎĵµ10.00 Ôª ¼ÓÈëVIPÃâ·ÑÏÂÔØ
ÍÆ¼öÏÂÔØ
±¾ÎÄ×÷Õߣº...

¹²·ÖÏí92ƪÏà¹ØÎĵµ

Îĵµ¼ò½é£º

¹ØÓÚÀ´Ô´µÄÎı¾Êý¾ÝµÄ×ֶμä¸ô·û Èç¹ûÒª½«×Ô¶¨Òå¼ä¸ô·ûµÄÎļþ¶ÁÈëÒ»¸ö±í£¬ÐèҪͨ¹ý´´½¨±íµÄÓï¾äÀ´Ö¸Ã÷ÊäÈëÎļþ¼ä¸ô·û£¬È»ºóload dataµ½Õâ¸ö±í¾ÍokÁË¡£ 2.6 Insert 2.6.1 Inserting data into Hive Tables from queries Standard syntax: INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement Hive extension (multiple inserts): FROM from_statement INSERT OVERWRITE TABLE table

¡Á ÓοͿì½ÝÏÂÔØÍ¨µÀ£¨ÏÂÔØºó¿ÉÒÔ×ÔÓɸ´ÖƺÍÅŰ棩
µ¥Æª¸¶·ÑÏÂÔØ
ÏÞÊ±ÌØ¼Û£º10 Ôª/·Ý Ô­¼Û:20Ôª
VIP°üÔÂÏÂÔØ
ÌØ¼Û£º29 Ôª/Ô ԭ¼Û:99Ôª
µÍÖÁ 0.3 Ôª/·Ý ÿÔÂÏÂÔØ150·Ý
ȫվÄÚÈÝÃâ·Ñ×ÔÓɸ´ÖÆ
VIP°üÔÂÏÂÔØ
ÌØ¼Û£º29 Ôª/Ô ԭ¼Û:99Ôª
µÍÖÁ 0.3 Ôª/·Ý ÿÔÂÏÂÔØ150·Ý
ȫվÄÚÈÝÃâ·Ñ×ÔÓɸ´ÖÆ
×¢£ºÏÂÔØÎĵµÓпÉÄÜ¡°Ö»ÓÐĿ¼»òÕßÄÚÈݲ»È«¡±µÈÇé¿ö£¬ÇëÏÂÔØÖ®Ç°×¢Òâ±æ±ð£¬Èç¹ûÄúÒѸ¶·ÑÇÒÎÞ·¨ÏÂÔØ»òÄÚÈÝÓÐÎÊÌ⣬ÇëÁªÏµÎÒÃÇЭÖúÄã´¦Àí¡£
΢ÐÅ£ºfanwen365 QQ£º370150219
Copyright © ÔÆÌ⺣ All Rights Reserved. ËÕICP±¸16052595ºÅ-3 ÍøÕ¾µØÍ¼ ¿Í·þQQ£º370150219 ÓÊÏ䣺370150219@qq.com