2015-10-14 35 views
7

Her biri birden çok kayda sahip olan birden çok JSON dosyasından bir Postgres tablosuna veri yüklemem gerekir. Aşağıdaki kodu kullanıyorum ama bu (tür birçok dışında iki kayıtlarını veren) gibi SAMPLE.JSON dosyasınınBir dosyadan json verilerini Postgres'e yükleme

COPY tbl_staging_eventlog1 ("EId", "Category", "Mac", "Path", "ID") 
from 'C:\\SAMPLE.JSON' 
delimiter ',' 
; 

İçerik olan (pencerelerde pgAdmin III kullanıyorum) çalışmaz:

[{"EId":"104111","Category":"(0)","Mac":"ABV","Path":"C:\\Program Files (x86)\\Google","ID":"System.Byte[]"},{"EId":"104110","Category":"(0)","Mac":"BVC","Path":"C:\\Program Files (x86)\\Google","ID":"System.Byte[]"}] 
+0

Hangi Postgres versiyonu? 9.3? 9.4? – Christian

+0

bu http://stackoverflow.com/a/24196160/3961156 adresini ziyaret edin –

cevap

20

bu deneyin:

-- let's create a temp table to bulk data into 
create temporary table temp_json (values text) on commit drop; 
copy temp_json from 'C:\SAMPLE.JSON'; 

-- uncomment the line above to insert records into your table 
-- insert into tbl_staging_eventlog1 ("EId", "Category", "Mac", "Path", "ID") 

select values->>'EId' as EId, 
     values->>'Category' as Category, 
     values->>'Mac' as Mac, 
     values->>'Path' as Path, 
     values->>'ID' as ID  
from (
      select json_array_elements(replace(values,'\','\\')::json) as values 
      from temp_json 
     ) a; 
+0

Çok teşekkürler! Bu çok yararlı – anil

+0

@anil: bu yüzden bu cevabı doğru olanı kabul etmelisiniz. – Christian

+0

Mükemmel görünüyor, ancak "HATA: ilişki" temp_json "mevcut değil" alınıyor .. –

İlgili konular