【问题标题】:How Vertica handles semi-structured data even if loaded from different file formats即使从不同的文件格式加载,Vertica 如何处理半结构化数据
【发布时间】:2021-01-18 16:41:06
【问题描述】:

我对@9​​87654321@ 中的半结构化数据处理的理解是,如果数据是这样说的(json

{
"f1":1,
"f2":"hello",
"f3":false,
"f4":2
}

然后使用两列__identity____raw__ 创建一个flextable__identify__ 将有 4 个字段(我想是整数 1、2、3、4),__raw__ 将是数据的原始表示形式(1、hello、false 和 2)。

我还可以在同一弹性表中的csv 文件中加载数据,例如2, hello2, true, 3Vertica 如何决定哪个字段映射到哪个列(例如f1f4)都是int

【问题讨论】:

    标签: vertica


    【解决方案1】:

    好吧,没有什么比准备好 Vertica SQL 提示符(以及创建数据库对象的权限...)更能尝试找出答案了。

    对于 JSON,字段名称在结构中:键值对。

    使用 CSV,数据文件的第一行需要有列名 - 我在下面添加...

    -- connecting with VSQL, 
    $ vsql -h localhost -d sbx -U dbadmin -w pwd
    $ vsql -h localhost -d sbx -U dbadmin -w pwd
    Welcome to vsql, the Vertica Analytic Database interactive terminal.
    
    Type:  \h or \? for help with vsql commands
           \g or terminate with semicolon to execute query
           \q to quit
    
    sbx=> -- create the flex table
    sbx=> CREATE FLEX TABLE flx();
    CREATE TABLE
    sbx=> -- load the flex table from stdin - data handed in-line - using your input
    sbx=> COPY flx FROM stdin PARSER fjsonparser();
    Enter data to be copied followed by a newline.
    End with a backslash and a period on a line by itself.
    >> {                                        
    >> "f1":1,
    >> "f2":"hello",
    >> "f3":false,
    >> "f4":2
    >> }
    >> \.
    -- test the load ...
    sbx=> SELECT f1,f2,f3,f4 FROM flx;
     f1 |  f2   |  f3   | f4 
    ----+-------+-------+----
     1  | hello | false | 2
    sbx=>-- load the CSV file - note that we need the title line, 
    sbx=>-- which I add, to have same values in the same fields
    sbx=> COPY flx FROM stdin PARSER fcsvparser();
    Enter data to be copied followed by a newline.
    End with a backslash and a period on a line by itself.
    >> f1,f2,f3,f4
    >> 2, hello2, true, 3
    >> \.
    sbx=>-- check the contents now
    sbx=> SELECT f1,f2,f3,f4 FROM flx;
     f1 |   f2   |  f3   | f4 
    ----+--------+-------+----
     1  | hello  | false | 2
     2  | hello2 | true  | 3
    sbx=>-- resulting table definition in catalog ...
    sbx=> \d flx
                                                 List of Fields by Tables
     Schema  | Table |    Column    |          Type          |  Size  | Default | Not Null | Primary Key | Foreign Key 
    ---------+-------+--------------+------------------------+--------+---------+----------+-------------+-------------
     dbadmin | flx   | __identity__ | int                    |      8 |         | t        | f           | 
     dbadmin | flx   | __raw__      | long varbinary(130000) | 130000 |         | t        | f           | 
    (2 rows)
    
    sbx=> -- check the contents of __identity__ and (after visualising) __raw__
    sbx=> SELECT __identity__,REPLACE(MAPTOSTRING(__raw__),CHR(10),' ') FROM flx;
     __identity__ |                                REPLACE                                 
    --------------+------------------------------------------------------------------------
                1 | {     "f1": "1",     "f2": "hello",     "f3": "false",     "f4": "2" }
                2 | {     "f1": "2",     "f2": "hello2",     "f3": "true",     "f4": "3" }
    
    

    【讨论】:

    • 知道了。所以csv 文件需要第一行的字段名。我一定错过了这个细节。
    猜你喜欢
    • 2021-03-17
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2021-05-11
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多