hive query

Thought that it was relatively simple, retained a

https://blog.csdn.net/kumquatlemon/article/details/78528101

Hive query
1, hive fuzzy search table
Show Tables like '* name *';
2, see table structure information
desc formatted TableName;
desc table_name
. 3, view the partition information
Show Partitions TableName;
. 4, according to partition the query data
select table_coulm from tablename where partitionname = '2016-02-25';
5, delete partitions
ALTER Test drop partition Table (dt = '2016-03-01');
ALTER Test drop partition Table (dt = '2016-01-17')
. 6, killed the shell is not a task to perform hive
Hadoop the job -kill job_201403041453_58315
7, hive command-line operation
to execute a query, display the progress mapreduce on the terminal, after the implementation, and finally the output query results to the terminal, then hive withdraw from the process, not It will enter interactive mode.
-e Hive 'the SELECT table_cloum from the Table'
-S, there will be no output on the terminal mapreduce of progress, finished, only the query results to the output terminal. This silent mode is very useful ,, invoked by third-party programs, third-party programs to obtain the result set by the standard output of the hive.
hive -S -e 'select tablecloum from table '
execute sql file
Hive -f hive_sql.sql
. 8, and delete files to view File Size
Hive> -du DFS / XX / XX
Hive> -rmr DFS / XX / XX
. 9, the mapjoin use scenarios: 1. there is an association operation in table 2 is very small, the link operation is not equivalent
select / * + mapjoin (a) * / fa, fb from a t join B f on (fa = ta and f.ftime 20,110,802 =) 
10, open the simple mode is not enabled Hive of Mr
SET = hive.fetch.task.conversion More;
. 11, table Hive modify
the ALTER tABLE oldtablename the RENAME the TO newTableName;
12 is, Hive add fields
alter table temp add columns (current_session_timelenth_count bigint comment 'page total duration of stay');
the ALTER the CHANGE current_session_timelenth current_session_timelenth the TEMP bigint the Comment 'residence time in the current session';
-------------------------------------------------- ---------------------------------------- 
Hive supports a number of SQL data definition language (data Manipulation Language, DML) existing features, including a variety of the following statements:
use condition where the filter table rows
using the column or select sub-query expressions
using equivalence, connecting multiple tables
all rows or sub-merged table queries
against multiple "packets" of columns aggregate calculations
query results into another table
export the contents of the table to a local directory or directory in HDFS
following references from click to open the link
1, only the first two queries:
the SELECT * from Student limit 2;
2, statistical table of the number of lines:
the SELECT cOUNT (*) from Student;
3, find a table id field id sum:
the SELECT sUM (id) from Student;
4, query the partition table
select * from beauties where nation = 'China';     
. 5, multi-table associated with:
select t. account, u. name , t. income, t. expenses, t. surplus from user_info u join (select account, sum (income) as income, sum (expenses) as expenses, sum (income-expenses) as surplus from 
trade_detail by Group Account) T ON = T U Account Account;..
alias
SELECT COUNT (DISTINCT e.uid) from (SELECT * from tablenamewhere
Rank <= =. 3 and Order. 1) E;
parentheses are returned in a table , it is only a temporary alias e
user search searched "Obama" through the search keyword
SELECT m.uid, m.keyword from (SELECT DISTINCT n.uid from
tablenamewhere like keyword '% Obama%' n-) m  
WHERE m.uid = n.uid;
user checked searched for "Obama" in the search had not contain "Obama" keyword itself
select m.uid, m.keyword from sogou_20111230 m join (select distinct uid from sogou_20111230 where keyword like '% Obama%') n on m.uid = n.uid where m.keyword not like '% Obama%';
the UNION ALL two or more tables may be combined
SELECT COUNT (DISTINCT e.uid) from (
SELECT * WHERE TableName from Rank <. 11 
Union All 
SELECT * WHERE ext_sogou_20111230_limit3 from Rank <. 11) E;
. 6, to re-query: group by a use
SELECT * from MyTable group by UID;
group by statement often used with an aggregate function, results may be grouped according to one or more of the polymerization operation is then performed for each group
select year (ts), avg ( rank) from tablename WHERE TS like '% 2011' Group by year (TS);
. 7, independently of the total number of the UID:
SELECT COUNT (DISTINCT (UID)) from MyTable; (efficiently) or a select count (*) from (select * from mytable group by uid) a;
8, the query frequency ranking (the highest frequency of the former 50):
the SELECT keyword, COUNT (*) AS cnt from the Test Group by keyword the Order by cnt desc limit 50;
9, added protection against deletion:
the ALTER the Table TableName
> Partition ( Day = '0925') enable no_drop;
"delete" to delete the added protection:
ALTER Table TableName
> Partition (Day = '20,161,207') disable no_drop;
10, added protection against query:
ALTER Table TableName
> Partition (Day = 20,161,207 ' ) enable offline;
delete protected query:
ALTER Table TableName
> Partition (Day = '20,161,207') disable Offline;
. 11, strong turn:
SELECT Cast (Rank AS DOUBLE) from TableName limit 10;
12 is, splicing:
SELECT the concat (UID , url) from tablename limit 10;
position 13, position 5 after find url string in the first occurrence of str
the locate SELECT ( "str", url,. 5) from TableName limit 100;
14, extracted string str meet the regular expression is a string of five parts of the url
select regexp_extract ( "str", url , 5) from tablename 100 limit; 
15, in accordance with the regular expression "0" split string uid, and the divided portion so as to return a string array
SELECT split (uid, "0") from TableName limit 100;
16, string url , taken apart from 0 string of length 3, as a substring
SELECT substr (url, 0,3) from 3 TableName limit; 
. 17, all the url string to uppercase letters
select upper (url ) from TableName limit. 3;
18 is, or WHERE ..and where .... or where both conditions query 
SELECT * WHERE TableName from Rank <= =. 3. 1 and Order limit. 3;
! SELECT * from tablenamewhere Rank = 0 or limit. 3. 1 = Order;
. 19, like filter string 
select * from tablename where url like ' % http%' limit 10;
RLIKE expression * n filtered through a Java function and%, as it is in the extended function hive operator
SELECT * from tablenamewhere RLIKE limit URL. 3 '* * HTTP..'; 
20 is, semi-left table semi left half the Join semi-independent hanging
SELECT * WHERE from Rank in BE (1, 2,5);
SELECT * from the Join ext_sogou_20111230_limit3 tablenamem left sEMI m.rank n-ON = n.rank;
21 is, hive view only support the logical view query complexity reduction effect of
Create view
Create view sogou_view AS 
SELECT * from tablenamewhere Rank <=. 3;
22 is, index 
Hive index to create a separate table implementation
creates indexes
the CREATE tABLE the Employees the iNDEX employees_index the ON (name) the AS 
'org.apache.hadoop.hive.ql. index.compact.CompactIndexHandler '
the WITH DEFERRED REBUILD IDXPROPERTIES (' Creator '=' Me ','
the created_at '=' some Time ') IN TABLE employees_index_table;
23, between 7-9 am, searched for "Baidu" users, which users click on a URL Baidu's
teacher:
the SELECT DISTINCT n.uid from (the SELECT * from sogou_view the WHERE keyword = 'Baidu')
and substr (TS , 9,2) in ( '07', '08', '09')) WHERE n-n.url like '%% Baidu.com';
SELECT UID from sogou_view WHERE (Cast (substr (TS, 9,2) 
int AS)>. 7 or Cast (substr (TS, 9,2) AS int) <. 9) and URL 
like '%% www.ganji.com' or like keyword '% Baidu%';
SELECT UID from sogou_view WHERE substr ( TS, 9,2) in ( '07', '08', '09') and URL
like '%% www.ganji.com' and like keyword '%% Baidu';
--------- ------------ 
author: Kumquatlemon 
source: CSDN 
original: https: //blog.csdn. net / kumquatlemon / article / details / 78528101
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/xiaokui9/article/details/89156232