<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Importing python function with spark.read.jdbc in to Repos in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/importing-python-function-with-spark-read-jdbc-in-to-repos/m-p/17656#M11624</link>
    <description>&lt;P&gt;If you have a notebook in a repo, that notebook can include a %run.  I attached a screenshot where you can see it in my notebook.  You can tell it's a repo bc of the published button on the top.  &lt;/P&gt;</description>
    <pubDate>Wed, 15 Jun 2022 12:40:10 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2022-06-15T12:40:10Z</dc:date>
    <item>
      <title>Importing python function with spark.read.jdbc in to Repos</title>
      <link>https://community.databricks.com/t5/data-engineering/importing-python-function-with-spark-read-jdbc-in-to-repos/m-p/17655#M11623</link>
      <description>&lt;P&gt;Hi all!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Before we used Databricks Repos we used the run magic to run various utility python functions from one notebook inside other notebooks, fex like reading from a jdbc connections. We now plan to switch to repos to utilize the fantastic CI/CD possibilities that gives us. But we meet some challenges with this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First, as I have understood, you cant use the run magic to run notebooks. Only arbritary files are allowed to be used. Then we re-wrote from notebook to .py files and sucessfully imported the functions but a spark related error occured.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is the overview of my setup:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The python file with the function resides in the folder "utils" and the notebook I want to make use of it is inside the folder "landing".&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1798i208BF09F98BA2F1E/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;The function looks like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;class qybele_db_connection:
  
  #this class creates a secure connection to the seas database
  jdbcHostname = "x"
  jdbcPort = 3306
  jdbcDatabase = "x"
  jdbcUrl = "jdbc:mysql://{0}:{1}/{2}".format(jdbcHostname, jdbcPort, jdbcDatabase)
  #always use the databricks secrets manager to store password
  jdbcpassword = 'x'
  connectionProps = { "user": 'x', "password": jdbcpassword }
  
  @staticmethod
  def read(spark_session,query=str):
    
&amp;nbsp;
    """
    this static method uses the above variables in a spark.read.jdbc function to create the connection.
    Note that the function takes in a string, which is a query and passes it to the "table" method in the spark function.
    """
    
    try:
      
      
      print('Executing query')
      df=spark.read.jdbc(url=qybele_db_connection.jdbcUrl, table=query, properties=qybele_db_connection.connectionProps)
      
    except Exception as e:
      raise Exception("Issue with reading from seas database")
&amp;nbsp;
    return df&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is how I import it to the notebook:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1783iD2CB12F9FCA9A8C2/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Do I really have to start with the whole sparksession thing when including "spark.read.jdbc" in python files for this kind of workflow? &lt;/LI&gt;&lt;LI&gt;How can I stick to the run magic like before; ie running the notebook, instead of importing the python function? &lt;/LI&gt;&lt;LI&gt;How does this effect the results when you use this notebook from repos in jobs/workflows? &lt;/LI&gt;&lt;LI&gt;How does databricks secrets act when using repos like this?&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Wed, 15 Jun 2022 09:36:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/importing-python-function-with-spark-read-jdbc-in-to-repos/m-p/17655#M11623</guid>
      <dc:creator>mortenhaga</dc:creator>
      <dc:date>2022-06-15T09:36:12Z</dc:date>
    </item>
    <item>
      <title>Re: Importing python function with spark.read.jdbc in to Repos</title>
      <link>https://community.databricks.com/t5/data-engineering/importing-python-function-with-spark-read-jdbc-in-to-repos/m-p/17656#M11624</link>
      <description>&lt;P&gt;If you have a notebook in a repo, that notebook can include a %run.  I attached a screenshot where you can see it in my notebook.  You can tell it's a repo bc of the published button on the top.  &lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 12:40:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/importing-python-function-with-spark-read-jdbc-in-to-repos/m-p/17656#M11624</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-06-15T12:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: Importing python function with spark.read.jdbc in to Repos</title>
      <link>https://community.databricks.com/t5/data-engineering/importing-python-function-with-spark-read-jdbc-in-to-repos/m-p/17657#M11625</link>
      <description>&lt;P&gt;Thats...odd. I was sure I had tried that, but now it works somehow. I guess it has to be that now I did it with double quotation marks. Thanks anyway! Works like a charm.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 12:57:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/importing-python-function-with-spark-read-jdbc-in-to-repos/m-p/17657#M11625</guid>
      <dc:creator>mortenhaga</dc:creator>
      <dc:date>2022-06-15T12:57:25Z</dc:date>
    </item>
  </channel>
</rss>

