Hive data type + Hive sql

Hive data type + Hive sql

basic type

  • Integer
    • int tinyint (byte) smallint(short) bigint(long)
  • Float
    • float double
  • Boolean
    • boolean
  • character
    • string char (fixed length) VARCHAR (variable length)
  • Time Type
    • timestamp date

References / composite type

  • Advantages similar to the container (Container), the operating data for us
  • Composite type can be nested and complex types
  • Array
    • The same type of data storage
    • Find the data according to the index, starting from 0 by default
    • user[0]
  • Map
    • A set of key-value pairs can be accessed by key value
    • key is not the same, the same key will overwrite each other
    • map['first']
  • Struct (the structure is the C language, golang also has)
    • Properties, structure defining the object are fixed
    • Get property values
    • user.uname

Type Conversion

  • automatic
    • Any integer type can be implicitly converted to a broader range of types
    • All integer types, FLOAT and STRING type can be converted implicitly to DOUBLE.
    • TINYINT, SMALLINT, INT can be converted to FLOAT.
    • BOOLEAN type can not be converted to any other type.
  • Enforcement
    • CAST('1' AS INT)
  • In the design table, the data type to the possible suitable type
  • Prevent trouble after operation unnecessary

DDL operations - Database

Databases, tables, fields, and so should pay attention to naming names

Define the implementation of database components (create, modify, delete) function

Perform any hivesql statement at the end of every sentence with a semicolon (;)

database

  • Create a database

    • Each will create a table to create a directory in the HDFS file system

      • create database ronnie;
      • create database if not exists ronnie;
    • Creating databases and the development of storage location

      • create database ronnie location '/ronnie/ronnie_test;

        1569236690603

  • Delete Database

    • drop database library name;
    • drop database if exists 库名;
    • If the current library is not empty, cascading deletes
      • drop database if exists 库名 cascade;
  • Modify database information

    • Other metadata information database are not changed
      • data storage name
      • Database resides directory location.
    • alter database ronnie set dbproperties ( 'createtime' = '20170830'); [Set Library Property]
  • Display Database

    • show databases;

      hive> show databases;
      OK
      default
      ronnie
      Time taken: 0.228 seconds, Fetched: 2 row(s)
      hive> 
      
    • show databases like 'r *'; [fuzzy match]

    hive> show databases like'r*';
    OK
    ronnie
    Time taken: 0.01 seconds, Fetched: 1 row(s)
    hive> 
    
  • View information

    • desc database ronnie;
  • Use Database

    • use ronnie;

DDL operations - table

  • Way to create the table: that is mapped to the data, so data representation of the design

Create a table

  • Create a table written statement of the time, do not appear tab key, garbled

  • Create a data file, uploaded to Linux

  • Creating userinfo table, a table is created in the folder name database file folder

  • The load data into the tables

    ronnieInfo.txt
1,luna,00000
2,slark,11111
3,sven,22222
4,anit_mage,33333
create table ronnieInfo(
id int,
uname string,
password string
)
row format delimited fields terminated by ',' lines terminated by '\n';

load data local inpath '/root/ronnieInfo.txt' overwrite into table ronnieInfo;

select * from ronnieInfo
select id from ronnieInfo where id = 2;

The command line displays:

hive> select * from ronnieInfo;
OK
1   luna    00000
2   slark   11111
3   sven    22222
4   anit_mage   33333
Time taken: 0.322 seconds, Fetched: 4 row(s)
hive> select id from ronnieInfo where id = 2;
OK
2
Time taken: 0.151 seconds, Fetched: 1 row(s)
Important Instruction Set:
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name 
(col_name data_type [COMMENT col_comment], ...)
[COMMENT table_comment] 
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] 
[CLUSTERED BY (col_name, col_name, ...) ]
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] 
[ROW FORMAT row_format] 
[STORED AS file_format] 
[LOCATION hdfs_path]
  • CREATE
    • Keyword, create a table
  • [EXTERNAL]
    • Table type, internal or external table Table
  • TABLE
    • Type created
  • [IF NOT EXISTS]
    • This judgment table exists
  • table_name
    • Table name, to follow the naming rules
  • (col_name data_type [COMMENT col_comment], ...)
    • Define a column (column name Data type 1, 2 Column Name Data Type 1)
    • Separated by commas column to column, no need to add the last column,
  • [HOW table_comment]
    • Annotation information table
  • [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
    • Create a partition table
  • [CLUSTERED BY (col_name, col_name, ...)
    • Points barrel
  • [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
    • Points barrel
  • [ROW FORMAT row_format]
    • Each row of data segmentation format
  • [STORED AS file_format]
    • Data storage format
  • [LOCATION hdfs_path]
    • Address data file

Modify table

Modify the table when the folder name will be modified

ALTER TABLE ronnieInfo RENAME TO ronnie_info;

Update Column

ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment][FIRST|AFTER column_name];

Increase the replacement column

ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...);

View table structure

desc table_name;

Delete table

DROP TABLE [IF EXISTS] table_name;

Guess you like

Origin www.cnblogs.com/ronnieyuan/p/11574504.html