加载分隔的数据
您可以使用两种分隔解析器(fdelimitedparser 和 fdelimitedpairparser)之一加载 Flex 表。
-
数据通过各行的数据指定列名称时,使用
fdelimitedpairparser。 -
数据不指定列名称或具有与列名称对应的标题行时,使用
fdelimitedparser。
本节将介绍如何使用 fdelimitedpairparser 和 fdelimitedparser 支持的一些选项。
拒绝重复值
您可以通过使用 fdelimitedparser 的 reject_on_duplicate=true 选项拒绝重复值。加载在拒绝重复值后继续。下个示例演示了如何使用此参数,接着显示了指定的异常和拒绝的数据文件。将被拒绝的数据保存到表而不是文件中,包括数据及其异常。
=> CREATE FLEX TABLE delim_dupes();
CREATE TABLE
=> COPY delim_dupes FROM stdin PARSER fdelimitedparser(reject_on_duplicate=true)
exceptions '/home/dbadmin/load_errors/except.out' rejected data '/home/dbadmin/load_errors/reject.out';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> A|A
>> 1|2
>> \.
=> \! cat /home/dbadmin/load_errors/reject.out
A|A
=> \! cat /home/dbadmin/load_errors/except.out
COPY: Input record 1 has been rejected (Processed a header row with duplicate keys with
reject_on_duplicate specified; rejecting.). Please see /home/dbadmin/load_errors/reject.out,
record 1 for the rejected record.
COPY: Loaded 0 rows, rejected 1 rows.
拒绝实体化列类型错误
fjsonparser 解析器和 fdelimitedparser 解析器都具有一个布尔参数 reject_on_materialized_type_error。将此参数设置为 true 将导致行被拒绝,前提是输入数据中同时存在以下情况:
-
包括与现有实体化列匹配的键
-
具有无法强迫采用实体化列的数据类型的值
假定 Flex 表具有实体化的列 OwnerPercent,声明为 FLOAT。试图加载带 OwnerPercent 键(值为 VARCHAR)的行将导致 fdelimitedparser 拒绝该数据行。
以下示例演示了如何设置该参数。
-
创建一个表
reject_true_false,带有两个实际列:=> CREATE FLEX TABLE reject_true_false(one VARCHAR, two INT); CREATE TABLE -
使用
fjsonparser在reject_on_materialized_type_error=false的情况下将 JSON 数据(从STDIN)加载至表中。尽管false是默认值,以下示例将显示指定该值以进行演示:=> COPY reject_true_false FROM stdin PARSER fjsonparser(reject_on_materialized_type_error=false); Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> {"one": 1, "two": 2} >> {"one": "one", "two": "two"} >> {"one": "one", "two": 2} >> \. -
调用
maptostring在加载数据之后显示表值:=> SELECT maptostring(__raw__), one, two FROM reject_true_false; maptostring | one | two ----------------------------------+-----+----- { "one" : "one", "two" : "2" } | one | 2 { "one" : "1", "two" : "2" } | 1 | 2 { "one" : "one", "two" : "two" } | one | (3 rows) -
截断该表:
=> TRUNCATE TABLE reject_true_false; -
重新加载相同的数据,但这次请设置
reject_on_materialized_type_error=true:=> COPY reject_true_false FROM stdin PARSER fjsonparser(reject_on_materialized_type_error=true); Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> {"one": 1, "two": 2} >> {"one": "one", "two": "two"} >> {"one": "one", "two": 2} >> \. -
调用
maptostring显示表内容。仅加载两行,而之前的结果中有三行:=> SELECT maptostring(__raw__), one, two FROM reject_true_false; maptostring | one | two ---------------------------------------+-----+----- { "one" : "1", "two" : "2" } | 1 | 2 { "one" : "one", "two" : "2" } | one | 2 (2 rows)