agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
plphyton function - return each list value as a row ?
4+ messages / 2 participants
[nested] [flat]

* plphyton function - return each list value as a row ?
@ 2020-07-25 21:26  karsten <karsten@terragis.net>
  0 siblings, 2 replies; 4+ messages in thread

From: karsten @ 2020-07-25 21:26 UTC (permalink / raw)
  To: pgsql-sql@lists.postgresql.org

Hi All,
 
I am trying to create my first plphyton function ( retrieving earthquake
data from an API) .
Overall I was able to get one single row to be returned, but am struggling
how to return the entire list I have as multiple rows - see below. Currently
I get the following error when running the GetEartquakeAll function:

select GetEartquakeAll('2020-01-01' ,'2020-03-01', -120, 40,200, 1.7)
gives me
ERROR: length of returned sequence did not match number of columns in row

How can I 'simply' return each list value as a row ? 
Thanks
Karsten Vennemann
 
CREATE OR REPLACE FUNCTION GetEartquakeAll(start date ,stop date, lon float,
lat float,radius int, minmagnitude float) RETURNS equake_values AS $$
  import urllib2
  import json as json
  data =
urllib2.urlopen('https://earthquake.usgs.gov/fdsnws/event/1/query?format=geo
json&starttime=%s&endtime=%s&latitude=%s&longitude=%s&maxradiuskm=%s&minmagn
itude=%s&orderby=magnitude' % (start,stop,lat,lon,radius,minmagnitude))
  js_data = json.load(data)
  equake = js_data
  equakearray = []
  a = 0
  for i in equake['features']:
    equakeplace = i['properties']['place'] # tile for earthquake location
    magnitude =   i['properties']['mag']
    qlong =       i['geometry']['coordinates'][0]
    qlat =        i['geometry']['coordinates'][1]      
    equakevalue = {"place": equakeplace, "magnitude": magnitude , "qlong":
qlong, "qlat": qlat}
    equakearray.append(equakevalue)
    a = a+1
  return equakearray
$$ LANGUAGE plpythonu; 
           
# create custom data type that is returned from equake  data API query
CREATE TYPE equake_values AS ( 
  place text,
  magnitude float,
  qlong  float,
  qlat  float
);  







^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: plphyton function - return each list value as a row ?
@ 2020-07-25 21:32  David G. Johnston <david.g.johnston@gmail.com>
  parent: karsten <karsten@terragis.net>
  1 sibling, 0 replies; 4+ messages in thread

From: David G. Johnston @ 2020-07-25 21:32 UTC (permalink / raw)
  To: karsten <karsten@terragis.net>; +Cc: pgsql-sql@lists.postgresql.org <pgsql-sql@lists.postgresql.org>

On Saturday, July 25, 2020, karsten <karsten@terragis.net> wrote:

> Hi All,
>
> I am trying to create my first plphyton function ( retrieving earthquake
> data from an API) .
> Overall I was able to get one single row to be returned, but am struggling
> how to return the entire list I have as multiple rows - see below.
> Currently
> I get the following error when running the GetEartquakeAll function:
>
> select GetEartquakeAll('2020-01-01' ,'2020-03-01', -120, 40,200, 1.7)
> gives me
> ERROR: length of returned sequence did not match number of columns in row
>
> How can I 'simply' return each list value as a row ?
> Thanks
> Karsten Vennemann
>
> CREATE OR REPLACE FUNCTION GetEartquakeAll(start date ,stop date, lon
> float,
> lat float,radius int, minmagnitude float) RETURNS equake_values AS $$
>

The choice of mailing list for this is unusual as this seems to have
nothing to do with general SQL.

Anyway, you may find the following documentation section useful.

 https://www.postgresql.org/docs/12/plpython-data.html#id-1.8.11.11.7

David J.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* plphyton function - return each list value as a row ?
@ 2020-07-25 21:41  karsten <karsten@terragis.net>
  parent: karsten <karsten@terragis.net>
  1 sibling, 1 reply; 4+ messages in thread

From: karsten @ 2020-07-25 21:41 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi All,
 
I am trying to create my first plphyton function ( retrieving earthquake
data from an API) .
Overall I was able to get one single row to be returned, but am struggling
how to return the entire list I have as multiple rows - see below. Currently
I get the following error when running the GetEartquakeAll function:

select GetEartquakeAll('2020-01-01' ,'2020-03-01', -120, 40,200, 1.7) gives
me
ERROR: length of returned sequence did not match number of columns in row

How can I 'simply' return each list value as a row ? 
Thanks
Karsten Vennemann
 
CREATE OR REPLACE FUNCTION GetEartquakeAll(start date ,stop date, lon float,
lat float,radius int, minmagnitude float) RETURNS equake_values AS $$
  import urllib2
  import json as json
  data =
urllib2.urlopen('https://earthquake.usgs.gov/fdsnws/event/1/query?format=geo
json&starttime=%s&endtime=%s&latitude=%s&longitude=%s&maxradiuskm=%s&minmagn
itude=%s&orderby=magnitude' % (start,stop,lat,lon,radius,minmagnitude))
  js_data = json.load(data)
  equake = js_data
  equakearray = []
  a = 0
  for i in equake['features']:
    equakeplace = i['properties']['place'] # tile for earthquake location
    magnitude =   i['properties']['mag']
    qlong =       i['geometry']['coordinates'][0]
    qlat =        i['geometry']['coordinates'][1]      
    equakevalue = {"place": equakeplace, "magnitude": magnitude , "qlong":
qlong, "qlat": qlat}
    equakearray.append(equakevalue)
    a = a+1
  return equakearray
$$ LANGUAGE plpythonu; 
           
# create custom data type that is returned from equake  data API query
CREATE TYPE equake_values AS (
  place text,
  magnitude float,
  qlong  float,
  qlat  float
);  










^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* RE: plphyton function - return each list value as a row ?
@ 2020-07-25 21:50  karsten <karsten@terragis.net>
  parent: karsten <karsten@terragis.net>
  0 siblings, 0 replies; 4+ messages in thread

From: karsten @ 2020-07-25 21:50 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Answering my own question I got it to work by a tiny change add SETOF for
the return definition:
Cheers
Karsten

...
RETURNS SETOF equake_values AS $$
...

-----Original Message-----
From: karsten [mailto:karsten@terragis.net] 
Sent: Saturday, July 25, 2020 14:42
To: pgsql-general@lists.postgresql.org
Subject: plphyton function - return each list value as a row ?

Hi All,
 
I am trying to create my first plphyton function ( retrieving earthquake
data from an API) .
Overall I was able to get one single row to be returned, but am struggling
how to return the entire list I have as multiple rows - see below. Currently
I get the following error when running the GetEartquakeAll function:

select GetEartquakeAll('2020-01-01' ,'2020-03-01', -120, 40,200, 1.7) gives
me
ERROR: length of returned sequence did not match number of columns in row

How can I 'simply' return each list value as a row ? 
Thanks
Karsten Vennemann
 
CREATE OR REPLACE FUNCTION GetEartquakeAll(start date ,stop date, lon float,
lat float,radius int, minmagnitude float) RETURNS equake_values AS $$
  import urllib2
  import json as json
  data =
urllib2.urlopen('https://earthquake.usgs.gov/fdsnws/event/1/query?format=geo
json&starttime=%s&endtime=%s&latitude=%s&longitude=%s&maxradiuskm=%s&minmagn
itude=%s&orderby=magnitude' % (start,stop,lat,lon,radius,minmagnitude))
  js_data = json.load(data)
  equake = js_data
  equakearray = []
  a = 0
  for i in equake['features']:
    equakeplace = i['properties']['place'] # tile for earthquake location
    magnitude =   i['properties']['mag']
    qlong =       i['geometry']['coordinates'][0]
    qlat =        i['geometry']['coordinates'][1]      
    equakevalue = {"place": equakeplace, "magnitude": magnitude , "qlong":
qlong, "qlat": qlat}
    equakearray.append(equakevalue)
    a = a+1
  return equakearray
$$ LANGUAGE plpythonu; 
           
# create custom data type that is returned from equake  data API query
CREATE TYPE equake_values AS (
  place text,
  magnitude float,
  qlong  float,
  qlat  float
);  













^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2020-07-25 21:50 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-07-25 21:26 plphyton function - return each list value as a row ? karsten <karsten@terragis.net>
2020-07-25 21:32 ` David G. Johnston <david.g.johnston@gmail.com>
2020-07-25 21:41 ` karsten <karsten@terragis.net>
2020-07-25 21:50   ` karsten <karsten@terragis.net>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox