簡體   English   中英

將3個表轉換為1個JSON列postgres

[英]Turn 3 tables into 1 JSON column postgres

我設法做到了,但是一旦我不得不更改其中一項輸入,我就無法使其工作

這有效

select row_to_json(VVV) as json_flow
from (select *,(SELECT json_agg(row_to_json(
(SELECT t FROM (SELECT length, ip_src, pcap,num,coordinates,host,uri,user_agent,city_ids,ts,device,cookie) t))) AS cookies
FROM   public.dpkt_scan ck where flow.flow_id = ck.flow_id and ck.cookie IS NOT null
GROUP  BY flow_id),
(SELECT json_agg(row_to_json(
(SELECT b FROM (SELECT pnum, src_ip, pcap,file_name,file_extension,file_size, dst_ip, timestamp,md5) b))) AS files
FROM   public.files_scan f where flow.flow_id = f.flow_id
GROUP  BY flow_id),
(SELECT json_agg(row_to_json(
(SELECT c FROM (SELECT destination_ip, destination_port, pcap,packet_number,source_port,extraction, extraction_id, source_ip) c))) AS cerdentials
FROM   public.credentials_scan c where flow.flow_id = c.flow_id
GROUP  BY flow_id) from public.flows_updated flow) as VVV

這會將包含3個表中數據的一列返回到1個json中,並在flow_id上進行匯總

現在我需要更改憑證表,以使事情變得更復雜

這將返回該表中我需要的數據

select cs_user.*, cs_user.flow_id, cs_user.pcap, cs_user.extraction as username, cs_pass.extraction as password
from credentials_scan cs_user left outer join credentials_scan cs_pass
on cs_user.packet_number=cs_pass.packet_number
and cs_user.pcap=cs_pass.pcap
where cs_user.extraction!= cs_pass.extraction
and cs_user.extraction like '%User%'
and cs_pass.extraction like '%Pass%'
group by cs_user.flow_id

現在我需要用新查詢替換從憑證中獲取數據的部分...

我不是DBA,第一個查詢是我在Google上搜索了很多之后。 如果有人有更好的方法將3個表變成1個JSON列,這些列匯總在一個字段上,則它也可以工作

簡而言之,我無法替換此部分(它選擇要從該表中獲取哪些字段)

SELECT destination_ip, destination_port, pcap,packet_number,source_port,extraction, extraction_id, source_ip

與新的查詢,我表明做一些計算和邏輯

謝謝。

編輯:我的工作查詢結果的示例

"application_attributes": "apache",
          "max_uri": "/clock.php?reqtime=1491224912479",
          "upload": 15243,
          "classification_engine": "classic",
          "version_string": "Windows NT 10.0",
          "cookies": [
            {
              "cookie": "_pk_id.1.2283=57dd5ddf65ceff96.1491224886.1.1491224886.1491224886.; _pk_ses.1.2283=*",
              "pcap": "/testx6.pcap",
              "num": 8117,
              "length": 535,
              "ip_src": "x.x.x.x",
              "coordinates": "",
              "host": "www.theforest.us",
              "uri": "/clock.php?reqtime=1491224894479",
              "device": "windows",
              "user_agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0",
              "city_ids": "",
              "ts": 1491224897.000065
            },
            {
              "cookie": "_pk_id.1.2283=57dd5ddf65ceff96.1491224886.1.1491224886.1491224886.; _pk_ses.1.2283=*",
              "pcap": "/testx6.pcap",
              "num": 8199,
              "length": 535,
              "ip_src": "x.x.x.x",
              "coordinates": "",
              "host": "www.theforest.us",
              "uri": "/clock.php?reqtime=1491224906480",
              "device": "windows",
              "user_agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0",
              "city_ids": "",
              "ts": 1491224909.000065
            }],
"cerdentials": null,
          "internal_ip": "x.x.x.x",
          "download": 31178,
          "first_packet": 7945,
          "pcap": "/testx6.pcap",
          "flow_id": 222,
          "flag_int_ext": true,
          "external_ip": "x.x.x.x",
          "protocol_stack": "ipv4,tcp,http",
          "dr_type": "apache",
          "os": "Windows",
          "src_port": 54061,
          "max_uri_host": "",
          "encrypted": false,
          "accounts_count": null,
          "application": "",
          "dst_port": 80,
          "files": [
            {
              "src_ip": "x.x.x.x",
              "pnum": 7953,
              "pcap": "/testx6.pcap",
              "file_name": "Image file 1.png",
              "file_extension": "png",
              "file_size": 1278,
              "dst_ip": "x.x.x.x",
              "timestamp": "2017-04-03 13-08-08.000148",
              "md5": "df3b6fb119a8be8abe44deb021b4c80c"
            },
            {
              "src_ip": "x.x.x.x",
              "pnum": 7953,
              "pcap": "/testx6.pcap",
              "file_name": "Image file 2.png",
              "file_extension": "png",
              "file_size": 510,
              "dst_ip": "x.x.x.x",
              "timestamp": "2017-04-03 13-08-08.000148",
              "md5": "482f3baa4842ea727d32ac147daa47b8"
            },
            {
              "src_ip": "x.x.x.x",
              "pnum": 7953,
              "pcap": "/testx6.pcap",
              "file_name": "Image file 1.gif",
              "file_extension": "gif",
              "file_size": 366,
              "dst_ip": "x.x.x.x",
              "timestamp": "2017-04-03 13-08-08.000148",
              "md5": "08eae37a90618ac55d9a7cffc82c736c"
            }],,
          "locations_count": 0,
          "protocol_title": "http",
          "device": "windows"

您可以只對結果進行jsonb_agg ,例如在這里我加入兩個表(您可以將您的邏輯放入CTE查詢中),然后將結果顯示為json:

so=# \pset format unaligned
Output format is unaligned.
so=# with joined as (select * from pg_database d join pg_stat_activity a on a.datname=d.datname where pid <> 98698)
select jsonb_pretty(jsonb_agg(joined)) from joined;
jsonb_pretty
[
    {
        "pid": 75073,
        "datid": "16661",
        "query": "select 0.15*14;",
        "state": "idle",
        "datacl": null,
        "datdba": "10",
        "datname": "so",
        "usename": "vao",
        "datctype": "UTF-8",
        "encoding": 6,
        "usesysid": "10",
        "datcollate": "C",
        "datminmxid": "1",
        "wait_event": null,
        "xact_start": null,
        "backend_xid": null,
        "client_addr": null,
        "client_port": -1,
        "query_start": "2017-10-31T22:31:42.236156+00:00",
        "backend_xmin": null,
        "datallowconn": true,
        "datconnlimit": -1,
        "datfrozenxid": "858",
        "state_change": "2017-10-31T22:31:42.242102+00:00",
        "backend_start": "2017-10-30T21:17:49.857856+00:00",
        "datistemplate": false,
        "datlastsysoid": "12668",
        "dattablespace": "1663",
        "client_hostname": null,
        "wait_event_type": null,
        "application_name": "psql"
    }
]
(1 row)
Time: 1.533 ms

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM