Python 如何在Clickhouse驱动程序中获取JSON格式的数据

Python 如何在Clickhouse驱动程序中获取JSON格式的数据,python,clickhouse,Python,Clickhouse,我试图在Django项目中获取Clickhouse数据。我正在使用clickhouse\u驱动程序,并且: 当我在Clickhouse服务器中执行此命令时,从myTable LIMIT 5格式JSON中选择*它以JSON格式输出。但在python中,当我尝试使用clickhouse_驱动程序时,它只输出如下字段: ['2020213'、'qwerty'、'asdfg'、'2030103'、'qweasd'、'asdxv'] 但是我想要键值json格式..比如 {日志日期:2020213,主机:q

我试图在Django项目中获取Clickhouse数据。我正在使用clickhouse\u驱动程序,并且:

当我在Clickhouse服务器中执行此命令时,从myTable LIMIT 5格式JSON中选择*它以JSON格式输出。但在python中,当我尝试使用clickhouse_驱动程序时,它只输出如下字段:

['2020213'、'qwerty'、'asdfg'、'2030103'、'qweasd'、'asdxv']

但是我想要键值json格式..比如

{日志日期:2020213,主机:qwerty,cef:asdfg}

有什么解决这个问题的建议吗?或者我必须搜索一个备用的clickhouse\u驱动程序

Thx.

单击房屋驱动程序忽略格式条款请参阅

可以通过将列名与相关值组合来手动执行:

从clickhouse\u驱动程序导入客户端 从json导入转储 client=Clienthost='localhost' data=client.execute_iter'SELECT*FROM system.functions LIMIT 5',列类型=True columns=[nextdata中的列的列[0] 对于数据中的行: json=dumpsdictzipcolumns,[行中的值对应值] printf'{json}' 结果: {name:fromUnixTimestamp64Nano,is_-aggregate:0,不区分大小写:0,别名_-to:} {name:tounix,is_-aggregate:0,不区分大小写:0,别名_-to:} {name:tounix:64micro,is_-aggregate:0,不区分大小写:0,别名_-to:} {name:sumburConsistentHash,is_aggregate:0,不区分大小写:0,别名_to:} {name:yandexConsistentHash,is_-aggregate:0,不区分大小写:0,别名_-to:} 或使用熊猫:

从clickhouse\u驱动程序导入客户端 作为pd进口熊猫 client=Clienthost='localhost' data=client.execute_iter'SELECT*FROM system.functions LIMIT 5',列类型=True columns=[nextdata中的列的列[0] df=pd.DataFrame.from_recordsdata,columns=columns printdf.to_jsonorient='records' 后果 [{name:fromUnixTimestamp64Nano,is_-aggregate:0,不区分大小写:0,别名_-to:},{name:toUnixTimestamp64Nano,is_-aggregate:0,不区分大小写:0,别名_-to:},{name:sumburConsistentHash,is_-aggregate:0,不区分大小写:0,别名_-to:},{name:yandexConsistentHash,is_-aggregate:0,不区分大小写:0,别名_-to:}]
我没有尝试Vladimir解决方案,但以下是我的解决方案:

client.execute命令为我们提供了_column_types=True参数。它为我们提供了表的元数据。之后:

result , columns = client.execute('SELECT * FROM myTbl LIMIT 5',with_column_types=True)
df=pandas.DataFrame(result,columns=[tuple[0] for tuple in columns])
dfJson=df.to_json(orient='records')
这就是我们想要的


Thx建议:

您是否尝试过Pandas.to_JSON和read_sql?实际上这与类型无关。它与clickhouse_driver lib有关。lib在尝试获取数据时不提供键,仅提供值。如果我可以获取键,我将使用它:thxyes同时我找到了解决方案:我在问题下给出了我的解决方案->
result , columns = client.execute('SELECT * FROM myTbl LIMIT 5',with_column_types=True)
df=pandas.DataFrame(result,columns=[tuple[0] for tuple in columns])
dfJson=df.to_json(orient='records')