How To Query Spanner And Get Metadata, Especially Columns' Names?
I'm trying to query custom SQL on Spanner and convert the results into a Pandas Dataframe, so I need data and column names, but I can't find a way to get the column names. Accordin
Solution 1:
You must fetch at least one row before the metadata are available. So if you were to change the order of your code so that you first fetch the data (or at least some data), and then get the metadata, it should work.
results: StreamedResultSet = snapshot.execute_sql(query)
print("metadata", results.metadata)
for row in results:
print(row)
Into this:
results: StreamedResultSet = snapshot.execute_sql(query)
for row in results:
print(row)
print("metadata", results.metadata)
then you should be getting metadata.
Also note that result set statistics (results.stats
) is only available when you are profiling a query. When you are just executing the query, as you are in your above example, this will always be empty.
Post a Comment for "How To Query Spanner And Get Metadata, Especially Columns' Names?"