Reputation: 33
My question is how to assign variables within a loop in KQL magic command in Jupyter lab. I refer to Microsoft's document on this subject and will base my question on the code given here: https://learn.microsoft.com/en-us/azure/data-explorer/kqlmagic
%%kql
StormEvents
| summarize max(DamageProperty) by State
| order by max_DamageProperty desc
| limit 10
df = _kql_raw_result_.to_dataframe()
statefilter =df.loc[0].State
statefilter
df = _kql_raw_result_.to_dataframe()
statefilter =df.loc[0:3].State
statefilter
dfs = [] # an empty list to store dataframes
for state in statefilters:
%%kql
let _state = state;
StormEvents
| where State in (_state)
| do some operations here for that specific state
df = _kql_raw_result_.to_dataframe()
dfs.append(df) # store the df specific to state in the list
The reason why I am not querying all the desired states within the KQL query is to prevent resulting in really large query outcomes being assigned to dataframes. This is not for this sample StormEvents
table which has a reasonable size but for my research data which consists of many sites and is really big. Therefore I would like to be able to run a KQL query/analysis for each site within a for loop and assign each site's query results to a dataframe. Please let me know if this is possible or perhaps there may other logical ways to do this within KQL...
Upvotes: 0
Views: 1140
Reputation: 21
Example of using the single line magic mentioned by Michael and a return statement that converted the result to JSON. Without the conversion to JSON I wasn't getting anything back.
def testKQL():
%kql DatabaseName | take 10000
return _kql_raw_result_.to_dataframe().to_json(orient='records')
Upvotes: 0
Reputation: 151
There are few ways to do it.
The simplest is to refractor your %%kql cell magic to a %kql line magic. Line magic can be embedded in python cell.
Other option is to: from Kqlmagic import kql The Kqlmagic kql method, accept as a string a kql cell or line. You can call kql from python.
Third way is to call the kql magic via the ipython method: ip.run_cell_magic('kql', {your kql magic cell text}) You can call it from python.
Upvotes: 4