MAPDELIMITEDEXTRACTOR
Extracts data with a delimiter character and other optional arguments, returning a single VMap value.
Syntax
MAPDELIMITEDEXTRACTOR (record-value [ USING PARAMETERS param=value[,...] ])
Arguments
record-value
- String containing a JSON or delimited format record on which to apply the expression.
Parameters
delimiter
- Single delimiter character.
Default:
|
header_names
- Delimiter-separated list of column header names.
Default:
ucol
n
, wheren
is the column offset number, starting with0
for the first column. trim
- Boolean, trim white space from header names and field values.
Default:
true
treat_empty_val_as_null
- Boolean, set empty fields to
NULL
rather than an empty string (''
).Default:
true
Examples
These examples use a short set of delimited data:
Name|CITY|New city|State|zip
Tom|BOSTON|boston|MA|01
Eric|Burlington|BURLINGTON|MA|02
Jamie|cambridge|CAMBRIDGE|MA|08
To begin, save this data as delim.dat
.
-
Create a flex table,
dflex
:=> CREATE FLEX TABLE dflex(); CREATE TABLE
-
Use COPY to load the
delim.dat
file. Use the flex tablesfdelimitedparser
with theheader='false'
option:=> COPY dflex FROM '/home/release/kmm/flextables/delim.dat' parser fdelimitedparser(header='false'); Rows Loaded ------------- 4 (1 row)
-
Create a columnar table,
dtab
, with an identityid
column, adelim
column, and avmap
column to hold a VMap:=> CREATE TABLE dtab (id IDENTITY(1,1), delim varchar(128), vmap long varbinary(512)); CREATE TABLE
-
Use COPY to load the
delim.dat
file into thedtab
table. MAPDELIMITEDEXTRACTOR uses theheader_names
parameter to specify a header row for the sample data, along withdelimiter '!'
:=> COPY dtab(delim, vmap AS MAPDELIMITEDEXTRACTOR (delim USING PARAMETERS header_names='Name|CITY|New City|State|Zip')) FROM '/home/dbadmin/data/delim.dat' DELIMITER '!'; Rows Loaded ------------- 4 (1 row)
-
Use MAPTOSTRING for the flex table
dflex
to view the__raw__
column contents. Notice the default header names in use (ucol0
–ucol4
), since you specifiedheader='false'
when you loaded the flex table:=> SELECT MAPTOSTRING(__raw__) FROM dflex limit 10; maptostring ------------------------------------------------------------------------------------- { "ucol0" : "Jamie", "ucol1" : "cambridge", "ucol2" : "CAMBRIDGE", "ucol3" : "MA", "ucol4" : "08" } { "ucol0" : "Name", "ucol1" : "CITY", "ucol2" : "New city", "ucol3" : "State", "ucol4" : "zip" } { "ucol0" : "Tom", "ucol1" : "BOSTON", "ucol2" : "boston", "ucol3" : "MA", "ucol4" : "01" } { "ucol0" : "Eric", "ucol1" : "Burlington", "ucol2" : "BURLINGTON", "ucol3" : "MA", "ucol4" : "02" } (4 rows)
-
Use MAPTOSTRING again, this time with the
dtab
table'svmap
column. Compare the results of this output to those for the flex table. Note that MAPTOSTRING returns theheader_name
parameter values you specified when you loaded the data:=> SELECT MAPTOSTRING(vmap) FROM dtab; maptostring ------------------------------------------------------------------------------------------------------------------------ { "CITY" : "CITY", "Name" : "Name", "New City" : "New city", "State" : "State", "Zip" : "zip" } { "CITY" : "BOSTON", "Name" : "Tom", "New City" : "boston", "State" : "MA", "Zip" : "02121" } { "CITY" : "Burlington", "Name" : "Eric", "New City" : "BURLINGTON", "State" : "MA", "Zip" : "02482" } { "CITY" : "cambridge", "Name" : "Jamie", "New City" : "CAMBRIDGE", "State" : "MA", "Zip" : "02811" } (4 rows)
-
Query the
delim
column to view the contents differently:=> SELECT delim FROM dtab; delim ------------------------------------- Name|CITY|New city|State|zip Tom|BOSTON|boston|MA|02121 Eric|Burlington|BURLINGTON|MA|02482 Jamie|cambridge|CAMBRIDGE|MA|02811 (4 rows)