Hive DML操作示例

记录Hive CLI中常见DML操作示例, 备用!

1.Loading Data into Managed Tables
  LOAD DATA LOCAL INPATH '${env:HOME}/california-employees'
  INTO TABLE employees
  PARTITION (country = 'US', state = 'CA');
2.Inserting Data into Tables from Queries
  INSERT OVERWRITE TABLE employees
  PARTITION (country = 'US', state = 'OR')
  SELECT * FROM staged_employees se
  WHERE se.cnty = 'US' AND se.st = 'OR';
  
  FROM staged_employees se
  INSERT OVERWRITE TABLE employees
  PARTITION (country = 'US', state = 'OR')
  SELECT * WHERE se.cnty = 'US' AND se.st = 'OR'
  INSERT OVERWRITE TABLE employees
  PARTITION (country = 'US', state = 'CA')
  SELECT * WHERE se.cnty = 'US' AND se.st = 'CA'
  INSERT OVERWRITE TABLE employees
  PARTITION (country = 'US', state = 'IL')
  SELECT * WHERE se.cnty = 'US' AND se.st = 'IL';
3.Dynamic Partition Insertss 
  INSERT OVERWRITE TABLE employees
  PARTITION (country, state)
  SELECT ..., se.cnty, se.st
  FROM staged_employees se;
  
  INSERT OVERWRITE TABLE employees
  PARTITION (country = 'US', state)
  SELECT ..., se.cnty, se.st
  FROM staged_employees se  
  WHERE se.cnty = 'US';
4.Creating Tables and Loading Them in One Query
  CREATE TABLE ca_employees
  AS SELECT name, salary, address
  FROM employees
  WHERE se.state = 'CA';
5.Exporting Data
  hadoop fs -cp source_path target_path
  
  INSERT OVERWRITE LOCAL DIRECTORY '/tmp/ca_employees'
  SELECT name, salary, address
  FROM employees
  WHERE se.state = 'CA';
  
  FROM staged_employees se
  INSERT OVERWRITE DIRECTORY '/tmp/or_employees'
  SELECT * WHERE se.cty = 'US' and se.st = 'OR'
  INSERT OVERWRITE DIRECTORY '/tmp/ca_employees'
  SELECT * WHERE se.cty = 'US' and se.st = 'CA'
  INSERT OVERWRITE DIRECTORY '/tmp/il_employees'
  SELECT * WHERE se.cty = 'US' and se.st = 'IL';

猜你喜欢

转载自springsfeng.iteye.com/blog/1845949