vertica - 即使从不同的文件格式加载,Vertica 如何处理半结构化数据
问题描述
我对半结构化数据处理的理解Vertica
是,如果数据是这样说的(在json
)
{
"f1":1,
"f2":"hello",
"f3":false,
"f4":2
}
然后 aflextable
创建有两列__identity__
和__raw__
。__identify__
将有 4 个字段(我假设整数 1、2、3、4)并且__raw__
将是数据的原始表示(1、hello、false 和 2)。
我还可以将数据加载csv
到同一个 flextable 中的文件中,例如2, hello2, true, 3
. 如何Vertica
决定哪个字段映射到哪个列(例如,两者f1
和f4
)是int
。
解决方案
好吧,没有什么比准备好 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" }
推荐阅读
- django - Nginx 重定向(非 www 到 www)不适用于 Certbot
- puppeteer - 获取 Puppeteer 中被阻止的请求列表
- synchronization - webots模拟同步
- angular - Angular 6 FormGroup.disable() 方法不适用于我的模板驱动的 NgForm
- python - 如何在不同方向显示颜色网格?
- google-chrome - 你能在这个 html/css 代码中找到错误吗?
- json - 按兄弟节点条件使用 JsonPath 进行 JSON 查询
- reactjs - 部署服务于 nginx 后面的快速服务器
- c++ - 返回 unordered_map 的函数
- java - Java bean 将属性绑定到极坐标方程