簡體   English   中英

如何將Curl的輸出直接發送到Postgres數據庫

[英]How to send the output of Curl directly to a postgres databse

我想將輸出直接發送到postgres數據庫! 是否有可能我必須先將其轉換為另一種格式! 任何想法!!

curl -sG https://peeringdb.com/api/netixlan --data-urlencode asn=58717 \
  --data-urlencode fields=ix_id,ipaddr4,ipaddr6,speed | jq -c '.data[]'

輸出為:

{“ ix_id”:215,“速度”:1000,“ ipaddr4”:“ 198.32.167.102”,“ ipaddr6”:null} {“ ix_id”:158,“速度”:10000,“ ipaddr4”:“ 27.111.228.8 “,” ipaddr6“:” 2001:de8:4 :: 5:8717:1“} {” ix_id“:158,” speed“:10000,” ipaddr4“:” 27.111.229.92“,” ipaddr6“:” 2001 :de8:4 :: 5:8717:2“} {” ix_id“:429,” speed“:10000,” ipaddr4“:” 103.16.102.35“,” ipaddr6“:” 2001:de8:12:100 :: 35" }

您可以嘗試使用Pandas和sqlalchemy:

import pandas as pd
from sqlalchemy import create_engine

#Define your connection to the DB:
engine = create_engine('postgresql://username:password@servername:port/dbname')

#Convert your json to a Pandas Dataframe:
df = pd.read_json(json_file) # Your json goes here, 
     #or just create a dataframe from the curl dictionary output

#Sends the DataFrame to postgres:
df.to_sql('tablename', engine, if_exists='replace', schema='public')

暫無
暫無

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

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