<?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: Use JDBC connect to databrick default cluster and read table into pyspark dataframe. All the column turned into same as column name in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8880#M4408</link>
    <description>&lt;P&gt;@yu zhang​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It looks like the issue with the first code snippet you provided is that it is not specifying the correct query to retrieve the data from your database.&lt;/P&gt;&lt;P&gt;When using the load() method with the jdbc data source, you need to provide a SQL query in the &lt;/P&gt;&lt;P&gt;dbtable option that retrieves the data you want to load into the DataFrame.&lt;/P&gt;&lt;P&gt;Assuming that your my_table2 table contains a column named name with the values you listed, you can modify your code as follows to retrieve the correct data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;url = 'jdbc:databricks://[workspace domain]:443/default;transportMode=http;ssl=1;AuthMech=3;httpPath=[path];AuthMech=3;UID=token;PWD=[your_access_token]'
 
driver_class = 'com.databricks.client.jdbc.Driver'
# create a DataFrame by querying the MySQL database
df_1 = spark.read.format("jdbc")\
.option("url", url)\
.option("driver", driver_class)\
.option("dbtable", '(select name from my_table2) as tmp')\
.load()
 
df_1.show()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This code passes a SQL query to the dbtable option that selects only the name column from your &lt;/P&gt;&lt;P&gt;my_table2 table. The resulting DataFrame should contain the data you expect.&lt;/P&gt;&lt;P&gt;Alternatively, you can use the spark.sql method to directly execute a SQL query against your database and load the results into a DataFrame, as you showed in your second code snippet:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df_2 = spark.sql('select * from my_table2')
df_2.show()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This code should also retrieve the correct data from your database.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 25 Apr 2023 04:02:41 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2023-04-25T04:02:41Z</dc:date>
    <item>
      <title>Use JDBC connect to databrick default cluster and read table into pyspark dataframe. All the column turned into same as column name</title>
      <link>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8877#M4405</link>
      <description>&lt;P&gt;I used code like below to Use JDBC connect to databrick default cluster and read table into pyspark dataframe&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;url = 'jdbc:databricks://[workspace domain]:443/default;transportMode=http;ssl=1;AuthMech=3;httpPath=[path];AuthMech=3;UID=token;PWD=[your_access_token]'
&amp;nbsp;
driver_class = 'com.databricks.client.jdbc.Driver'
# create a DataFrame by querying the MySQL database
df_1 = spark.read.format("jdbc")\
.option("url", url)\
.option("driver", driver_class)\
.option("dbtable", 'my_table2')\
.load()
&amp;nbsp;
df_1.show()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The final df_1 dataframe become&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;+----+
|name|
+----+
|name|
|name|
|name|
+----+&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the final result should be&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;+----+
|name|
+----+
|John|
|Jane|
|Bob |
+----+&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I also tested with code like&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df_2 =  spark.sql('select * from my_table2')
df_2.show()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the result dataframe is correct. please advise. thank you!&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="error"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/627i46A330DD3BDA0D17/image-size/large?v=v2&amp;amp;px=999" role="button" title="error" alt="error" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 19:45:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8877#M4405</guid>
      <dc:creator>brian_0305</dc:creator>
      <dc:date>2023-02-22T19:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Use JDBC connect to databrick default cluster and read table into pyspark dataframe. All the column turned into same as column name</title>
      <link>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8878#M4406</link>
      <description>&lt;P&gt;Hi @yu zhang​&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just try to give your database name also into the code.&lt;/P&gt;&lt;P&gt;I am using same code with database name and it's working fine for me&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 04:24:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8878#M4406</guid>
      <dc:creator>Ajay-Pandey</dc:creator>
      <dc:date>2023-02-23T04:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: Use JDBC connect to databrick default cluster and read table into pyspark dataframe. All the column turned into same as column name</title>
      <link>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8879#M4407</link>
      <description>&lt;P&gt;does pyspark.read.jdbc function has database name option? could you tell me how to give database name in this function. I think database name included in jdbc url option.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2023 15:57:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8879#M4407</guid>
      <dc:creator>brian_0305</dc:creator>
      <dc:date>2023-02-27T15:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: Use JDBC connect to databrick default cluster and read table into pyspark dataframe. All the column turned into same as column name</title>
      <link>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8880#M4408</link>
      <description>&lt;P&gt;@yu zhang​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It looks like the issue with the first code snippet you provided is that it is not specifying the correct query to retrieve the data from your database.&lt;/P&gt;&lt;P&gt;When using the load() method with the jdbc data source, you need to provide a SQL query in the &lt;/P&gt;&lt;P&gt;dbtable option that retrieves the data you want to load into the DataFrame.&lt;/P&gt;&lt;P&gt;Assuming that your my_table2 table contains a column named name with the values you listed, you can modify your code as follows to retrieve the correct data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;url = 'jdbc:databricks://[workspace domain]:443/default;transportMode=http;ssl=1;AuthMech=3;httpPath=[path];AuthMech=3;UID=token;PWD=[your_access_token]'
 
driver_class = 'com.databricks.client.jdbc.Driver'
# create a DataFrame by querying the MySQL database
df_1 = spark.read.format("jdbc")\
.option("url", url)\
.option("driver", driver_class)\
.option("dbtable", '(select name from my_table2) as tmp')\
.load()
 
df_1.show()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This code passes a SQL query to the dbtable option that selects only the name column from your &lt;/P&gt;&lt;P&gt;my_table2 table. The resulting DataFrame should contain the data you expect.&lt;/P&gt;&lt;P&gt;Alternatively, you can use the spark.sql method to directly execute a SQL query against your database and load the results into a DataFrame, as you showed in your second code snippet:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df_2 = spark.sql('select * from my_table2')
df_2.show()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This code should also retrieve the correct data from your database.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 04:02:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/use-jdbc-connect-to-databrick-default-cluster-and-read-table/m-p/8880#M4408</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-04-25T04:02:41Z</dc:date>
    </item>
  </channel>
</rss>

