{ "cells": [ { "cell_type": "markdown", "id": "a2bcf822", "metadata": {}, "source": [ "# Create a text import job" ] }, { "cell_type": "markdown", "id": "728208e5", "metadata": {}, "source": [ "You use a text import job to import data from a plain text file with an accompanying import\n", "template.\n", "\n", "This example shows how to create a text import job request, submit it to the job\n", "queue, and interact with the resulting text import job object returned by the server.\n", "\n", "Information on how to create a text import template is outside the scope of this example.\n", "For information on how to import plain text data into Granta MI, see the Granta MI documentation\n", "or consult your ACE representative." ] }, { "cell_type": "markdown", "id": "9e6dbb3e", "metadata": {}, "source": [ "## Connect to Granta MI" ] }, { "cell_type": "markdown", "id": "8ed83612", "metadata": {}, "source": [ "Import the ``Connection`` class and create the connection. For more information,\n", "see the [Connect and access the job queue](0_Getting_started.ipynb) example." ] }, { "cell_type": "code", "execution_count": null, "id": "c5349805", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.jobqueue import Connection\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "client = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()" ] }, { "cell_type": "markdown", "id": "fdb7ce47", "metadata": {}, "source": [ "## Create a ``TextImportJobRequest`` object" ] }, { "cell_type": "markdown", "id": "539af602", "metadata": {}, "source": [ "The first step in importing a text file with the job queue is to create a\n", "``TextImportJobRequest`` object. When creating this object, specify the name of the job and the\n", "files to import. You can also specify an optional description and the scheduled execution\n", "date, if the import should be deferred until that date and time.\n", "\n", "A text import job requires data files, template files, and optionally additional files to be\n", "uploaded as attachments. These can be provided as relative or absolute paths or as `pathlib.Path`\n", "objects." ] }, { "cell_type": "code", "execution_count": null, "id": "97335571", "metadata": {}, "outputs": [], "source": [ "import pathlib\n", "\n", "from ansys.grantami.jobqueue import TextImportJobRequest\n", "\n", "text_import_request = TextImportJobRequest(\n", " name=\"Text Import\",\n", " description=\"An example text import job\",\n", " template_file=pathlib.Path(\"./assets/text_import_template.xml\"),\n", " data_files=[\"./assets/example_data.txt\"],\n", ")\n", "\n", "text_import_request" ] }, { "cell_type": "markdown", "id": "94875fb0", "metadata": {}, "source": [ "## Submit the job to the server\n", "Next, submit the jobs to the server. There are two methods for submitting job\n", "requests:\n", "\n", "* ``create_job()``: Submit the job request to the server and immediately return an\n", " ``AsyncJob`` object in the *pending* state.\n", "* ``create_job_and_wait()``: Submit the job request to the server and block until the job\n", " either completes or fails. Return an ``AsyncJob`` object in the *succeeded* or *failed* state.\n", "\n", "This example uses the ``create_job_and_wait()`` method. For an example that shows\n", "how to create and submit a job that runs asynchronously, see\n", "[Schedule and modify jobs](4_Scheduling_and_modifying_jobs.ipynb)." ] }, { "cell_type": "code", "execution_count": null, "id": "32d2d4db", "metadata": {}, "outputs": [], "source": [ "text_import_job = client.create_job_and_wait(text_import_request)\n", "text_import_job" ] }, { "cell_type": "markdown", "id": "3b19f93d", "metadata": {}, "source": [ "## Access output files\n", "Finally, access the results of the job. Import jobs typically create log files, but the exact type\n", "of files generated varies based on the type of import template. In this case, the files are all\n", "plain text." ] }, { "cell_type": "markdown", "id": "45cda829", "metadata": {}, "source": [ "Access the list of files generated by the job with the ``output_file_names`` property. This\n", "returns a list of file names." ] }, { "cell_type": "code", "execution_count": null, "id": "f3c82b9a", "metadata": {}, "outputs": [], "source": [ "text_import_job.output_file_names" ] }, { "cell_type": "markdown", "id": "bcc09c4a", "metadata": {}, "source": [ "In general, a text import job returns two files:\n", "\n", "- ``.log``: Log file of the import operation on the server\n", "- ``summary.json``: Data file that summarizes the number of records impacted by the import\n", " job and provides details of any errors that occurred during processing." ] }, { "cell_type": "markdown", "id": "2d4df319", "metadata": {}, "source": [ "This cell shows how to access the content of the log file as ``bytes`` using the\n", "``AsyncJob.get_file_content()`` method:" ] }, { "cell_type": "code", "execution_count": null, "id": "093587b2", "metadata": {}, "outputs": [], "source": [ "log_file_name = next(name for name in text_import_job.output_file_names if \"log\" in name)\n", "log_file_content = text_import_job.get_file_content(log_file_name)\n", "log_file_string = log_file_content.decode(\"utf-8\")\n", "print(f\"{log_file_name} (first 200 characters):\")\n", "print(f\"{log_file_string[:500]}...\")" ] }, { "cell_type": "markdown", "id": "a0e6a4b5", "metadata": {}, "source": [ "This next cell shows how to download the import summary file to disk using the\n", "``AsyncJob.download_file()`` method." ] }, { "cell_type": "code", "execution_count": null, "id": "446ed947", "metadata": {}, "outputs": [], "source": [ "summary_file_name = next(\n", " name for name in text_import_job.output_file_names if name == \"summary.json\"\n", ")\n", "output_path = f\"./{summary_file_name}\"\n", "text_import_job.download_file(summary_file_name, output_path)\n", "print(f\"{summary_file_name} saved to disk\")" ] } ], "metadata": { "granta": { "clean_database": true }, "jupytext": { "notebook_metadata_filter": "granta" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }