2014-02-07 23 views

回答

2

Flex表格是Vertica 7.0中的一個新功能。這feauture創建不同類型的表格特別設計用於加載和查詢非結構化數據,也稱爲HP Vertica的 語法半結構化數據來創建一個Flex表:

create flex table unstruc_data(); 

凡unstruc_data的內容有兩列_ 身份 __ _; 其中,行col是半結構化數據的內容,其類型爲LONG VARBINARY,標識將爲行ID。
的Flex表附帶了一組的幫助功能:

  • COMPUTE_FLEXTABLE_KEYS
  • BUILD_FLEXTABLE_VIEW
  • COMPUTE_FLEXTABLE_KEYS_AND_BUILD_VIEW
  • MATERIALIZE_FLEXTABLE_COLUMNS
  • RESTORE_FLEXTABLE_DEFAULT_KEYS_TABLE_AND_VIEW

我不打算解釋所有這些,因爲我認爲你應該去研究它們。 有關新Vertica的更多細節特點去這個鏈接Vertica 7.0 New Stuff

1

所有非結構化數據保存到原始數據字段

這是一個BLOB

當您需要訪問非結構化的領域,這是一個緩慢,因爲需要BLOB提取

1

在JSON文檔通過客戶端傳遞給您的情況下,您需要將其存儲在Vertica DB中。

沒有使用flex表,這裏有幾個問題: 1)您需要知道Json的結構。 2)在Vertica DB中創建一個表格。 3)從JSON文檔中提取每個列的值 4)將值插入表中。

從這個過程中

除此之外,如果一個新的密鑰被添加到JSON沒有對Vertica的DB附加任務修改表,並在處理邏輯,以獲得新的密鑰對值

使用Flex表,下面詳細是我們如何簡化它的解釋:

1) Take the below Json,EE.txt 
    {"Name":"Rahul","Age":30} 
2) Create a flex table EMP_test  
    dbadmin=> create flex table EMP_Test(); 
    CREATE TABLE 
3) Load the data into the flex table 
    dbadmin=> copy EMP_Test from '/home/dbadmin/EE.txt' parser fjsonparser(); 
     Rows Loaded 
    ------------- 
               1 
    (1 row) 

4) To find out what keys are there in your Json , You have to refresh keys projection using below command 
    dbadmin=> select compute_flextable_keys('EMP_Test'); 
                  compute_flextable_keys               
    -------------------------------------------------- 
     Please see public.EMP_Test_keys for updated keys 
    (1 row) 
    dbadmin=> select * FRom EMP_Test_keys; 
     key_name | frequency | data_type_guess 
    ----------+-----------+----------------- 
     Age      |         1 | varchar(20) 
     Name     |         1 | varchar(20) 
    (2 rows) 


5) Refresh the view for flex table using below command .You can query the view for data 
    dbadmin=> 
    dbadmin=> select build_flextable_view('EMP_Test'); 
                    build_flextable_view                  
    ----------------------------------------------------- 
     The view public.EMP_Test_view is ready for querying 
    (1 row) 

    dbadmin=> select * From EMP_Test_View 
    dbadmin-> ; 
     age | name   
    -----+------- 
     30  | Rahul 
    (1 row) 

6) Now , If your Json structure changes and a Additional key 'Gender' is added . 
     {"Name":"Sid","Age":22,"Gender":"M"} 

7) You can load the data directly into the table EMP_Test 
    dbadmin=> copy EMP_Test from '/home/dbadmin/EE1.txt' parser fjsonparser(); 
     Rows Loaded 
    ------------- 
               1 
    (1 row) 
8) Re compute the keys and rebuild the view using below command 
    dbadmin=> select compute_flextable_keys('EMP_Test'); 
                  compute_flextable_keys               
    -------------------------------------------------- 
     Please see public.EMP_Test_keys for updated keys 
    (1 row) 

    dbadmin=> select build_flextable_view('EMP_Test'); 
                    build_flextable_view                  
    ----------------------------------------------------- 
     The view public.EMP_Test_view is ready for querying 
    (1 row) 

9) You can find the new data added and new keys using the below command . 
    dbadmin=> 
    dbadmin=> select * From EMP_Test_keys; 
     key_name | frequency | data_type_guess 
    ----------+-----------+----------------- 
     Age      |         2 | varchar(20) 
     Name     |         2 | varchar(20) 
     Gender   |         1 | varchar(20) 
    (3 rows) 

    dbadmin=> select * From EMP_test_view; 
     age | name  | gender 
    -----+-------+-------- 
     30  | Rahul | 
     22  | Sid   | M 
    (2 rows) 

This is how Flex table converts unstructured data(semi structured data) to structured data . 
Flex table has made it very easy to integrate any data service with vertica DB .