2016-11-10 2 views
1

CSVの形式で画像のURLを持っている -csvにデータ抽出が必要なように、ストアドプロシージャ内のcsvをどのように読み込みますか?

www.domain.com/table_id/x_y_height_width.jpg 

我々は、ストアドプロシージャでこれらのURLからtable_idが、X、Y、高さと幅を抽出し、複数のSQLクエリでこれらのパラメータを使用します。

どうすればいいですか?

+0

。残りの質問は以下のように回答されています。 https://www.postgresql.org/docs/current/static/file-fdw.html – mlinth

答えて

2

regexp_split_to_array and split_part functions

create or replace function split_url (
    _url text, out table_id int, out x int, out y int, out height int, out width int 
) as $$ 
    select 
     a[2]::int, 
     split_part(a[3], '_', 1)::int, 
     split_part(a[3], '_', 2)::int, 
     split_part(a[3], '_', 3)::int, 
     split_part(split_part(a[3], '_', 4), '.', 1)::int 
    from (values 
     (regexp_split_to_array(_url, '/')) 
    ) rsa(a); 
$$ language sql immutable; 

select * 
from split_url('www.domain.com/234/34_12_400_300.jpg'); 
table_id | x | y | height | width 
----------+----+----+--------+------- 
     234 | 34 | 12 | 400 | 300 

lateralを行う他のテーブルとの機能を使用するには:あなたがテーブルとしてCSVファイルを読み込むために、外国データラッパを使用することができます

with t (url) as (values 
    ('www.domain.com/234/34_12_400_300.jpg'), 
    ('www.examplo.com/984/12_90_250_360.jpg') 
) 
select * 
from 
    t 
    cross join lateral 
    split_url(url) 
; 
        url     | table_id | x | y | height | width 
---------------------------------------+----------+----+----+--------+------- 
www.domain.com/234/34_12_400_300.jpg |  234 | 34 | 12 | 400 | 300 
www.examplo.com/984/12_90_250_360.jpg |  984 | 12 | 90 | 250 | 360 
+0

これは本当に役に立ちました!ありがとう! – Tisha

関連する問題