Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中将Postgres查询结果转换为JSON或字符串_Python_Postgresql_Azureservicebus_Azure Servicebus Queues - Fatal编程技术网

如何在Python中将Postgres查询结果转换为JSON或字符串

如何在Python中将Postgres查询结果转换为JSON或字符串,python,postgresql,azureservicebus,azure-servicebus-queues,Python,Postgresql,Azureservicebus,Azure Servicebus Queues,我有一个Postgres数据库,我想将查询结果发送到Azure服务总线。 查询的输出为: [(226530, 5, 'GDGDGh', '326', 'Logical Name', 553713665, 537985025, 136, 0, 301, datetime.datetime(2021, 3, 8, 19, 46, 26, 536000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), datetime.dat

我有一个Postgres数据库,我想将查询结果发送到Azure服务总线。 查询的输出为:

[(226530, 5, 'GDGDGh', '326', 'Logical Name', 553713665, 537985025, 136, 0, 301, datetime.datetime(2021, 3, 8, 19, 46, 26, 536000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), datetime.datetime(2021, 3, 8, 19, 46, 25, 749508, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), datetime.datetime(2021, 3, 8, 19, 46, 25, 795195, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 2, {'error': {}, 'MSG_TYPE_TAG': '301', 'ATTACHED_DEVICE_SERIAL_NUMBER_TAG': '537985025', 'HDGDGDGD_TILTDATA_LOG_TAG': {'date_time': '2021-0 ... (531 characters truncated) ... 19:46:26.537', 'sequence_number': 420745, 'inclination_x_date_time': '2021-03-08 19:46:26.539', 'inclination_y_date_time': '2021-03-08 19:46:26.540'}}, 0, 420745)]
如果将此输出发送到服务总线队列,则收到以下错误:

TypeError: ServiceBusMessage body must be a string, bytes, or None.  Got instead: <class 'list'>
AttributeError: 'list' object has no attribute 'to_string'
AttributeError: 'list' object has no attribute 'to_json'
另外,我使用了
json.dumps()
进行转换,收到以下错误:

TypeError: Object of type LegacyRow is not JSON serializable
是否有任何解决方案可以将此输出转换为Azure服务总线

我的代码是:

import os
import psycopg2
import pandas as pd
import datetime
import sqlalchemy
import json
from azure.servicebus import ServiceBusClient, ServiceBusMessage


connstr = "Endpoint=s*****"
queue_name = '****'


def get_engine(database='***_rms', username='user**', password='pass**', host='***', port=5432):
    engine_string = f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}"
    engine = sqlalchemy.create_engine(engine_string)
    return engine


def read_from_db(table_name, date_time):
    acceleration_array = []
    engine = get_engine()
    connection = engine.connect()
    metadata = sqlalchemy.MetaData()
    pinconnector_attacheddevicelogdata = sqlalchemy.Table(
        table_name, metadata, autoload=True, autoload_with=engine)
    query = sqlalchemy.select([pinconnector_attacheddevicelogdata]).where(pinconnector_attacheddevicelogdata.columns.capture_time > date_time)\
        .order_by(pinconnector_attacheddevicelogdata.columns.capture_time).limit(1)
    ResultProxy = connection.execute(query)
    ResultSet = ResultProxy.fetchall()
    engine.dispose()
    return ResultSet


def get_date_time(date_time_string):
    [date, time] = date_time_string.split(" ")
    [year, month, day] = date.split("-")
    [hour, minute, second] = time.split(":")
    [second, microsecond] = second.split(".")
    [year, month, day, hour, minute, second, microsecond] = list(
        map(lambda x: int(x), [year, month, day, hour, minute, second, microsecond]))
    return datetime.datetime(year, month, day, hour, minute, second, microsecond, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None))


def write_date_time(date_time, address="date_time.txt"):
    file1 = open(address, "w+")
    file1.write((str(date_time)).split("+")[0])
    file1.close()


def read_date_time(address="date_time.txt"):
    file1 = open(address, "r")
    date_time = file1.readline()
    date_time = get_date_time(date_time)
    file1.close()
    return date_time


def get_query_latest_date_time(db_query, date_time):
    for item in db_query:
        if item["capture_time"] > date_time:
            date_time = item["capture_time"]
    return date_time


date_time = read_date_time()
db_query = read_from_db("pinconnector_attacheddevicelogdata", date_time)
write_date_time(db_query[-1]["capture_time"])
print(db_query)
#data_send = db_query.to_string()
#data_send = json.dumps(db_query)


with ServiceBusClient.from_connection_string(connstr) as client:
    with client.get_queue_sender(queue_name) as sender:
        single_message = ServiceBusMessage(data_send)
        sender.send_messages(single_message)

也许您可以尝试以下代码:

    db_query_to_list = list(db_query[0])
    logging.info(db_query_to_list)
    jsonString = json.dumps(db_query_to_list, default=str)