Chapter hive learning: data types

Basic data types

hive data types java data types length example
INT int 4byte signed integer 20
BIGINT long 8byte signed integer 20
DOUBLE double Double-precision floating-point number 3.14159
STRING string Character series. You can specify the character set. You may be used single or double quotes 'now is the time' "for all good men"

For string type hive equivalent varchar type of database. This is a variable of type string, it can not declare how many characters can be stored, in theory, you can store 2GB of characters

Collection data types

type of data description Syntax Example
STRUCT And similar c language struct, are available through the "dot" notation access element content. For example: if the data type of a column is STRUCT {first STRING, last STRING}, then the first element can be referenced by a first field struct()
MAP

MAP is a set of keys - tuples of values ​​using array notation can access the data. For example: if the data type of a column is MAP, where the keys - the time value of 'frist' -> 'John' and 'last' -> 'Doe', you can get the last element of a field name by { 'last'}

map()
ARRAY An array is a collection of variables having the same name and type, and these variables are called array elements, each array element has a number, number is zero. For example: the value array [ 'John', 'Doe'], then the second element can be an array name [1] referenced Array()
  • Example, data import based on the following format json
    {
        "name": "songsong",
        "friends": ["bingbing", "lili"],              //列表Array
        "children": {                                 //键值Map
            "xiao song": 18,
            "xiaoxiao song": 19
        }
        "address": {                                  //结构Struct
            "street": "hui long guan",
            "city": "beijing"
        }
    }
    
    
    //创建本地测试文件test.txt
    songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
    yangyang,caicai_susu,xiao yang:18_xiaoxiao yang 19,chao yang_beijing
    
    //建表语句
    create table test(
    name string,
    friends array<string>,
    children map<string, int>,
    address struct<street:string, city:string>
    )
    row format delimited fields terminated by ','         //列分隔符
    collection items terminated by '_'                 //map,struct和array数据分割符
    map keys terminated by ':'             //map中key和value分隔符
    lines terminated by '\n'         //行分隔符
    
    
    //导入文本数据到测试表
    load data local inpath "/opt/module/datas/test.txt" into table test
    
    //指定数组元素访问
    select friends[1] from test
    
    //指定键值对访问
    select children['xiao song'] from test
    
    //结构体访问
    select address.city from test

    MAP and is similar to Java ARRAY, STRUCT similar to the C language struct, encapsulates a collection of named fields

The type of conversion 

hive can be derived from the implicit data type conversion, inverse conversion is not performed, unless the operation CAST

  • Implicit type conversion
  1. Any integer type can be implicitly converted to a broader type, such as TINYINT can be converted into INT, INT can be converted into BIGINT
  2. All integer types, FLOAT and STRING type can be converted implicitly to DOUBLE
  3. TINYINT, SMALLINT, INT can be converted into FLOAT
  4. BOOLEA type can not be converted into any other type
  • CAST display operation using the data type conversion
  1. For example: CAST ( '1' AS INT) uses the string '1' into an integer 1; if cast fails, performed as CAST ( '1' AS INT), the expression return value is NULL
Published 111 original articles · won praise 57 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_38358499/article/details/105352184