Fabric Data Days Monthly is back. Join us on March 26th for two expert-led sessions on 1) Getting Started with Fabric IQ and 2) Mapping & Spacial Analytics in Fabric. Register now
Hi,
Solved! Go to Solution.
Hi @Ka13 ,
Yes we can read this query in a copy activity. First convert this select query to json
|
import json
from notebookutils import notebook
nb_result = {
"select_query": select_query
}
notebook.exit(json.dumps(nb_result))
|
Then in pipeline add notebook activity and connect to copy data. In the dynamic content of copy data, add this json expression.
|
@json(activity('Notebook1').output.exitValue).select_query
|
This works.
If this post helps, please accept this as a solution. Appreciate your kudos.
Thanks,
Pallavi_r
Hi @RajK2,
thanks for explaining the scenario. To better understand the issue and provide an accurate solution, could you please share a small set of sample data (with any sensitive information removed) along with the expected output? This will help ensure the guidance is aligned with your exact requirement.
Thanks,
Prashanth
MS fabric community support
Hi Prasare , I tried the below solution provided by Pallavi it worked
Thanks Prasare, will test and let you know if any issues.
Hi @Ka13 ,
The exitvalue returned from mssparkutils.notebook.run() is always a string. We need to first parse into json using function - json.loads to access its element. Please see the below code.
|
import json
parsed = json.loads(exit_value)
schema = parsed["parent1"]["Details"]["schema"]
table = parsed["parent1"]["Details"]["table"]
|
The above code worked for me. Please see and let me know if it resolves this issue.
If this post helps, please accept this as a solution. Appreciate your kudos.
Thanks,
Pallavi_r
Thanks Pallavi for your reply.
Can we pass the above ExitValue from one notebook to another Notebook activity and access the values of schema and table in another Notebook?. Can you please suggest
Hi @Ka13
Yes definitely. Please see this. Below code is written in 2nd notebook calling another notebook.
|
exit_value = mssparkutils.notebook.run(
"nb_begin",
timeout_seconds=60
)
|
If this post helps, please accept this as a solution. Appreciate your kudos.
Thanks,
Pallavi
Thanks Pallavi, in Details if we have one more field of columns like - id,name,city,country. And to extract columns.
can you please suggest.
Hi @Ka13 ,
So here we need frame a dynamic sql. in the previous one we had parsed the string into json. Now we have to retrieve all the column names into string literal in a python list.
|
details = parsed["parent1"]["Details"]
schema = details["schema"]
table = details["table"]
columns = details["columns"]
|
|
getcol = [
f"CAST({col} AS STRING) AS {col}"
for col in columns
]
|
Then to create a dynamic sql expression, we need to merge all the elements from the above list into a single comma separated string using ",".join(column)
|
select_query = f"""
SELECT
{", ".join(getcol)}
FROM {schema}.{table}
"""
print(select_query)
|
working code below:
If this post helps, please accept this as a solution. Appreciate your kudos.
Thanks,
Pallavi
Hi Pallavi, can we pass this formed select query in next Copy Activity?
Thanks Pallavi, can we pass this select query formed to the next activity ( Copy Activity) ? , to execute the query. Will test the above and let you know if any questions.
Hi Pallavi, can we pass this formed select query in next Copy Activity?
Hi @Ka13 ,
Yes we can read this query in a copy activity. First convert this select query to json
|
import json
from notebookutils import notebook
nb_result = {
"select_query": select_query
}
notebook.exit(json.dumps(nb_result))
|
Then in pipeline add notebook activity and connect to copy data. In the dynamic content of copy data, add this json expression.
|
@json(activity('Notebook1').output.exitValue).select_query
|
This works.
If this post helps, please accept this as a solution. Appreciate your kudos.
Thanks,
Pallavi_r
Thanks Pallavi tested above it's working
Hi Pallavi,
in the getcol , the column names are getting split as below . Can you please suggest
ID - AS I , D AS D
Hi @Ka13
Sorry for late reply. Can you please send me the exit value and parsed.
| User | Count |
|---|---|
| 18 | |
| 7 | |
| 4 | |
| 3 | |
| 3 |