sql >> Database teknologi >  >> RDS >> Oracle

Hvordan analyserer man json effektivt i Oracle 18c?

Du kan bruge json_table() at omdanne JSON-værdien til en relationel repræsentation. Det kan igen bruges i en MERGE-erklæring.

For eksempel. følgende forespørgsel:

select j.*
from itv_dev_logs
  cross join json_table(log_clob, '$.data.routedStops[*]'
                        columns stop_id integer path '$.stopId',
                                zone_ltf integer path '$.zoneLTF', 
                                info_stop_nr integer path '$.routingInfo.stop',
                                info_route_ref varchar(20) path '$.routingInfo.routeRef',
                                info_eta varchar(20) path '$.routingInfo.eta',
                                info_eta_dt timestamp path '$.routingInfo.etaDateTime',
                                info_stop_time number path '$.routingInfo.stopTime'
                        ) j
where log_id = 1435334;
 

Returnerer noget som dette:

STOP_ID | ZONE_LTF | INFO_STOP_NR | INFO_ROUTE_REF | INFO_ETA | INFO_ETA_DT | INFO_STOP_TIME | INFO_DIST_PREV_STOP | INFO_BREAK_TIME | INFO_BREAK_DURATION --------------+----------+--------------+----------------+----------+-------------------------+----------------+---------------------+-----------------+-------------------- 1554383571432 | 1 | 1 | R119 | 11:01 | 2019-04-16 11:01:00.000 | 0.08 | 0.27 | 00:00 | 00:00 1554383571515 | 1 | 2 | R119 | 11:07 | 2019-04-16 11:07:00.000 | 0.08 | 0.34 | 00:00 | 00:00 1554383571601 | 1 | 3 | R119 | 11:13 | 2019-04-16 11:13:00.000 | 0.08 | 0 | 00:00 | 00:00 1554383571671 | 1 | 4 | R119 | 11:19 | 2019-04-16 11:19:00.000 | 0.08 | 0 | 00:00 | 00:00 1554383571739 | 1 | 5 | R119 | 11:25 | 2019-04-16 11:25:00.000 | 0.08 | 0 | 00:00 | 00:00

Det kunne bruges som kilden til en MERGE-sætning for at opdatere din måltabel:

merge into your_target_table tg
using (
    select j.*
    from itv_dev_logs
      cross join json_table(log_clob, '$.data.routedStops[*]'
                            columns stop_id integer path '$.stopId',
                                    zone_ltf integer path '$.zoneLTF', 
                                    info_stop_nr integer path '$.routingInfo.stop',
                                    info_route_ref varchar(20) path '$.routingInfo.routeRef',
                                    info_eta varchar(20) path '$.routingInfo.eta',
                                    info_eta_dt timestamp path '$.routingInfo.etaDateTime',
                                    info_stop_time number path '$.routingInfo.stopTime'
                            ) j
    where log_id = 1435334
) t on (t.stop_id = tg.stop_id and ... more join conditions ...)
when matched then update
    set stop_nr = t.info_stop_nr, 
        eta_timestamp = t.eta_dt, 
 

Da du hverken har angivet strukturen af ​​målet eller informationen, hvilke JSON-nøgler der skal matches til hvilke tabelkolonner, er alle kolonnenavne kun gæt, og du skal erstatte dem med de korrekte navne.




  1. Opdater kolonne med tilfældig værdi

  2. Brug af CASE i PostgreSQL til at påvirke flere kolonner på én gang

  3. Når mysql_query returnerer false

  4. PostgreSQL Connection Pooling med PgBouncer