Skip to content

Commit 8ca7200

Browse files
author
Oracle Public Cloud User
committed
Add SQL examples
1 parent 9a400e0 commit 8ca7200

File tree

3 files changed

+159
-66
lines changed

3 files changed

+159
-66
lines changed

MongoExamples/SpringBoot/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,100 @@ curl -i http://localhost:8080/station/120
139139
...
140140
```
141141

142+
# SQL/JSON
142143

144+
The following are examples of SQL/JSON that can be run
145+
over the `station` and `status` collections. These queries
146+
can be executed in Database Actions as described in [Lab 4](https://oracle.github.io/learning-library/data-management-library/database/json/mongodb-api/workshops/freetier/index.html?lab=dbactions).
143147

148+
Select the JSON documents as text:
149+
150+
```
151+
select json_serialize(data)
152+
from station
153+
```
154+
155+
Project fields as columns:
156+
157+
```
158+
select
159+
s.id,
160+
s.data.name.string(),
161+
s.data.region_id.string(),
162+
s.data.lat.number(),
163+
s.data.lon.number(),
164+
s.data.station_type.string()
165+
from station s;
166+
```
167+
168+
Group stations by `region_id`:
169+
170+
```
171+
select
172+
t.data.region_id.string() as region_id,
173+
count(*) as count
174+
from station t
175+
where t.data.station_type.string() = 'classic'
176+
group by t.data.region_id.string();
177+
```
178+
179+
Automatically create relational views using `json_dataguide`:
180+
181+
```
182+
declare
183+
dg clob;
184+
begin
185+
select json_dataguide(data, dbms_json.FORMAT_HIERARCHICAL, dbms_json.pretty) into dg
186+
from station;
187+
188+
dbms_json.create_view('STATION_VIEW', 'STATION', 'DATA', dg, resolveNameConflicts => true, path => '$._id');
189+
190+
select json_dataguide(data, dbms_json.FORMAT_HIERARCHICAL, dbms_json.pretty) into dg
191+
from status;
192+
193+
dbms_json.create_view('STATUS_VIEW', 'STATUS', 'DATA', dg);
194+
end;
195+
/
196+
```
197+
198+
Select from the created view:
199+
200+
```
201+
select * from station_view;
202+
```
203+
204+
Join between collections:
205+
206+
```
207+
select
208+
s."station_id" station_id,
209+
(select t."capacity" from station_view t where t."_id" = s."station_id") capacity,
210+
min(s."num_bikes_available") min,
211+
max(s."num_bikes_available") max,
212+
round(avg(s."num_bikes_available")) avg
213+
from status_view s
214+
group by s."station_id";
215+
```
216+
217+
```
218+
create view station_availability as
219+
select
220+
s."station_id" station_id,
221+
(select t."capacity" from station_view t where t."_id" = s."station_id") capacity,
222+
min(s."num_bikes_available") min,
223+
max(s."num_bikes_available") max,
224+
round(avg(s."num_bikes_available")) avg
225+
from status_view s
226+
group by s."station_id";
227+
```
228+
229+
Select stations that always had more than half capacity available:
230+
231+
```
232+
select station_id, min, max, avg, capacity
233+
from station_availability
234+
where (capacity - min) < (capacity / 2) and
235+
capacity > 55
236+
order by min desc;
237+
```
144238

MongoExamples/SpringBoot/src/main/java/example/model/Station.java

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,104 +11,104 @@
1111
* @see https://github.com/NABSA/gbfs/blob/master/gbfs.md#station_information
1212
*/
1313
public class Station {
14-
14+
1515
@Id
1616
String id;
17-
17+
1818
String name;
19-
19+
2020
String region_id;
21-
22-
double lon,lat;
23-
21+
22+
double lon, lat;
23+
2424
boolean eightd_has_key_dispenser;
25-
25+
2626
String legacy_id;
27-
27+
2828
List<String> rental_methods;
29-
29+
3030
String external_id;
31-
31+
3232
int capacity;
33-
33+
3434
String short_name;
35-
35+
3636
boolean electric_bike_surcharge_waiver;
37-
37+
3838
String station_type;
39-
39+
4040
List<String> eightd_station_services;
4141

4242
boolean has_kiosk;
43-
43+
4444
Map<String, String> rental_uris;
45-
45+
4646
public Station() {
47-
47+
4848
}
4949

50-
public String getId() {
51-
return id;
52-
}
50+
public String getId() {
51+
return id;
52+
}
5353

54-
public String getName() {
55-
return name;
56-
}
54+
public String getName() {
55+
return name;
56+
}
5757

58-
public String getRegion_id() {
59-
return region_id;
60-
}
58+
public String getRegion_id() {
59+
return region_id;
60+
}
6161

62-
public double getLon() {
63-
return lon;
64-
}
62+
public double getLon() {
63+
return lon;
64+
}
6565

66-
public double getLat() {
67-
return lat;
68-
}
66+
public double getLat() {
67+
return lat;
68+
}
6969

70-
public boolean isEightd_has_key_dispenser() {
71-
return eightd_has_key_dispenser;
72-
}
70+
public boolean isEightd_has_key_dispenser() {
71+
return eightd_has_key_dispenser;
72+
}
7373

74-
public String getLegacy_id() {
75-
return legacy_id;
76-
}
74+
public String getLegacy_id() {
75+
return legacy_id;
76+
}
7777

78-
public List<String> getRental_methods() {
79-
return rental_methods;
80-
}
78+
public List<String> getRental_methods() {
79+
return rental_methods;
80+
}
8181

82-
public String getExternal_id() {
83-
return external_id;
84-
}
82+
public String getExternal_id() {
83+
return external_id;
84+
}
8585

86-
public int getCapacity() {
87-
return capacity;
88-
}
86+
public int getCapacity() {
87+
return capacity;
88+
}
8989

90-
public String getShort_name() {
91-
return short_name;
92-
}
90+
public String getShort_name() {
91+
return short_name;
92+
}
9393

94-
public boolean isElectric_bike_surcharge_waiver() {
95-
return electric_bike_surcharge_waiver;
96-
}
94+
public boolean isElectric_bike_surcharge_waiver() {
95+
return electric_bike_surcharge_waiver;
96+
}
9797

98-
public String getStation_type() {
99-
return station_type;
100-
}
98+
public String getStation_type() {
99+
return station_type;
100+
}
101101

102-
public List<String> getEightd_station_services() {
103-
return eightd_station_services;
104-
}
102+
public List<String> getEightd_station_services() {
103+
return eightd_station_services;
104+
}
105105

106-
public boolean isHas_kiosk() {
107-
return has_kiosk;
108-
}
106+
public boolean isHas_kiosk() {
107+
return has_kiosk;
108+
}
109109

110-
public Map<String, String> getRental_uris() {
111-
return rental_uris;
112-
}
110+
public Map<String, String> getRental_uris() {
111+
return rental_uris;
112+
}
113113

114114
}

MongoExamples/SpringBoot/src/main/java/example/repository/StatusRepository.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import org.springframework.boot.autoconfigure.web.WebProperties.Resources;
65
import org.springframework.data.mongodb.repository.MongoRepository;
76
import org.springframework.data.mongodb.repository.Query;
87
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

0 commit comments

Comments
 (0)