Chapter 3 Hive Data Types

The first 3 chapters  Hive Data Types

 

3.1 Basic Data Types

For the Hive 's String type database equivalent of varchar type, which is a string variable, but it can not declare which can hold up to the number of characters, in theory, it can store 2GB number of characters.

 

3.2 collection data types

Hive three complex data types ARRAY , the MAP and STRUCT . ARRAY and MAP and Java in Array and Map the like, and STRUCT with C language Struct Similarly, it encapsulates a collection of named fields, complex data type allows any level of nesting.

 

Case practical operation

 

  1)  Suppose the following table for a row, we use JSON format to represent the data structure. In the Hive format for the next visit

 

{
    "name": "songsong",
    "friends": ["bingbing" , "lili"] ,       //列表Array, 
    "children": {                      //键值Map,
        "xiao song": 18 ,
        "xiaoxiao song": 19
    }
    "address": {                      //结构Struct,
        "street": "hui long guan" ,
        "city": "beijing" 
    }
}

 

2 ) Based on the above data structure, we Hive created in the corresponding table, and import data.

  Create a local test file test.txt

Songsong, bingbing_lili, xiao Song: 18_xiaoxiao Song: 19, Hui Long guan_beijing 
yangyang, caicai_susu, xiao Yang: Yang 18_xiaoxiao: 19, the Chao yang_beijing

Note : MAP , STRUCT and ARRAY between elements in the relationship can be represented by the same character, here "_" .

3 ) Hive create a test table on test

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 keys terminated by ':'
lines terminated by '\n';

Fields explained:

row format delimited fields terminated by ', ' - column delimiter

collection items terminated by '_' --MAP STRUCT and ARRAY delimiter ( data division symbol )

map keys terminated by ':' - MAP of key and value separators

lines terminated by '\ n'; - row delimiter

 

4 ) introducing the text data to the test table

 

hive (default)> load data local inpath ‘/opt/module/datas/test.txt’into table test

 

5 ) access the set of columns in the three kinds of data, the following are ARRAY , the MAP , STRUCT access mode

hive (default)> select friends[1],children['xiao song'],address.city from test
where name="songsong";
OK
_c0     _c1     city
lili    18      beijing
Time taken: 0.076 seconds, Fetched: 1 row(s)

3.3 Type Conversion

  Hive atomic data types may be made implicit conversion, similar to the Java type conversion, for example, using an expression INT type, TINYINT automatically converted to INT type, but Hive not reverse conversion, e.g., an expression use TINYINT type, INT will not be automatically converted to TINYINT type, it will return an error, unless CAST operations.

  1. Implicit type conversion rules are as follows

  ( 1 ) any integer type can be implicitly converted to a broader range of types, such as TINYINT can be converted into the INT , the 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 to FLOAT .

  ( . 4 ) BOOLEAN type can not be converted into any other type.

  2. You may be used CAST operation display data type conversion

  E.g. CAST ( '1' AS INT) uses the string '1' is converted into an integer of 1 ; if cast fails, such as performing the CAST ( 'X-' the AS the INT) , the expression returns NULL NULL .

 

 

 

 

Guess you like

Origin www.cnblogs.com/Diyo/p/11462548.html