Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

ddl - Concat variable with a string in location parameter in a create statement?

In Greenplum, I need to create an external table with a dynamic location parameter. For an example:

CREATE READABLE TABLE_A(
date_inic date,
func_name varchar,
q_session bigint
)
LOCATION(:location)
FORMAT 'TEXT' (DELIMITER '|');

But in :location parameter I need to concat it with a fixed string. I tried:

LOCATION (:location || '123')

But I get a syntax error, otherwise in select statement it works perfectly. I'm inserting :location value like: " 'gphdfs://teste:1010/tmp' "

Can anyone help me?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are missing a few things in your table definition. You forgot "external" and "table".

CREATE READABLE EXTERNAL TABLE table_a
(
date_inic date,
func_name varchar,
q_session bigint
)
LOCATION(:location)
FORMAT 'TEXT' (DELIMITER '|');

Note: gphdfs has been deprecated and you should use PXF or gpfdist instead.

Next, you just need to use double quotes around the location value.

[gpadmin@mdw ~]$ psql -f example.sql -v location="'gpfdist://teste:1010/tmp'"
CREATE EXTERNAL TABLE
[gpadmin@mdw ~]$ psql 
psql (9.4.24)
Type "help" for help.

gpadmin=# d+ table_a 
                          External table "public.table_a"
  Column   |       Type        | Modifiers | Storage  | Stats target | Description 
-----------+-------------------+-----------+----------+--------------+-------------
 date_inic | date              |           | plain    |              | 
 func_name | character varying |           | extended |              | 
 q_session | bigint            |           | plain    |              | 
Type: readable
Encoding: UTF8
Format type: text
Format options: delimiter '|' null 'N' escape ''
External options: {}
External location: gpfdist://teste:1010/tmp
Execute on: all segments

And from bash, you can just concat the strings together too.

loc="gpfdist://teste"
port="1010"
dir="tmp"
location="'""$loc"":""$port""/""$dir""'"

psql -f example.sql -v location="$location"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...