zoukankan      html  css  js  c++  java
  • Hadoop Hive sql 语法详解

    Hive 最适合应用在基于大量不可变数据的批处理作业。

    1. 建表

     CREATE ATBEL [IF NOT EXISTS] table_name

    (col_name data_type)

    CREATE TABLE 创建一个表,如果相同名字的表已经存在,则抛出异常。用IF NOT EXISTS来忽略这个异常。

    如:CRETAE TABLE BCUSTOMER(

    CST_ID Integer,

    CST_NAME STRING

    );

    创建外部表:

    CREATE EXTERNAL TABLE page_view(viewTime INT, userid BIGINT,
         page_url STRING, referrer_url STRING,
         ip STRING COMMENT 'IP Address of the User',
         country STRING COMMENT 'country of origination')
     COMMENT 'This is the staging page view table'
     ROW FORMAT DELIMITED FIELDS TERMINATED BY '54'
     STORED AS TEXTFILE
     LOCATION '<hdfs_location>';

    建分区表

    CREATE TABLE par_table(viewTime INT, userid BIGINT,
         page_url STRING, referrer_url STRING,
         ip STRING COMMENT 'IP Address of the User')
     COMMENT 'This is the page view table'
     PARTITIONED BY(date STRING, pos STRING)
    ROW FORMAT DELIMITED ‘ ’
       FIELDS TERMINATED BY ' '
    STORED AS SEQUENCEFILE;

    建Bucket表

    CREATE TABLE par_table(viewTime INT, userid BIGINT,
         page_url STRING, referrer_url STRING,
         ip STRING COMMENT 'IP Address of the User')
     COMMENT 'This is the page view table'
     PARTITIONED BY(date STRING, pos STRING)
     CLUSTERED BY(userid) SORTED BY(viewTime) INTO 32 BUCKETS
     ROW FORMAT DELIMITED ‘ ’
       FIELDS TERMINATED BY ' '
    STORED AS SEQUENCEFILE;

    创建表并创建索引字段ds

    hive> CREATE TABLE invites (foo INT, bar STRING) PARTITIONED BY (ds STRING);

    复制一个空表 (允许复制现有的表结构,但不复制数据)

    CREATE TABLE empty_key_value_store
    LIKE key_value_store;
     
    显示所有表: SHOW TABLES;
     
    表添加一列/更新列:
    ALTER TABLE BCUSTOMER ADD/REPLACE COLUMNS (new_col INT)
     
    添加一列并增加列字段注释:
    ALTER TABLE BCUSTOMER ADD COLUMNS (new_col INT COMMENT 'a comment')
     
    更改表名:
    ALTER TABLE BCUSTOMER  RENAME TO NEWNAME;
     
    删除表:
    DROP TABLE IF EXISTS IKEA.TEMP_IF_REPORT_CHECK_CHERXU;
     
    删除列:
    ALTER TABLE table_name DROP column_name;
     
    Hive SQL中没有delete 和update功能,可以用OVERWRITE代替
    假如你要删除CST_ID=1的人
    INSERT INTO TABLE BCUSTOMER
    SELECT * FROM BCUSTOMER  WHERE CST_ID !=1
     
     
    HIVE中不支持等值连接
    select * from a,b where a.key=b.key (不可以)
    应该写为:
    select * from a join b on a.key=b.key
  • 相关阅读:
    redis学习
    win2008下c#调用directshow问题
    vs2005升级到vs2010相关问题
    spark-shell 启动失败,显示端口问题
    监控spark-sql 等脚本
    spark 相关配置 shuffle 相关配置选项
    spark on Yarn 语句
    使用hive thriftserver 连接spark sql
    HBase 报错系列之region is not online
    HBase 表迁移中对丢失的表检查使用的语句
  • 原文地址:https://www.cnblogs.com/cherxu/p/6879822.html
Copyright © 2011-2022 走看看