<?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 ForeachBatch() - Get results from  batchDF._jdf.sparkSession().sql('merge stmt') in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/foreachbatch-get-results-from-batchdf-jdf-sparksession-sql-merge/m-p/12186#M7040</link>
    <description>&lt;P&gt;Most python examples show the structure of the foreachBatch method as:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;def foreachBatchFunc(batchDF, batchId):
    batchDF.createOrReplaceTempView('viewName')
    
    (
        batchDF
            ._jdf.sparkSession()
            .sql(
                """
   &amp;lt;&amp;lt;   merge statement &amp;gt;&amp;gt;
&amp;nbsp;
               """
    )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;._jdf.sparkSession().sql() returns a java object not a dataframe&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How do you get access to the results dataframe containing the (affected, inserted, updated, deleted) row counts?&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jan 2023 09:20:49 GMT</pubDate>
    <dc:creator>jm99</dc:creator>
    <dc:date>2023-01-13T09:20:49Z</dc:date>
    <item>
      <title>ForeachBatch() - Get results from  batchDF._jdf.sparkSession().sql('merge stmt')</title>
      <link>https://community.databricks.com/t5/data-engineering/foreachbatch-get-results-from-batchdf-jdf-sparksession-sql-merge/m-p/12186#M7040</link>
      <description>&lt;P&gt;Most python examples show the structure of the foreachBatch method as:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;def foreachBatchFunc(batchDF, batchId):
    batchDF.createOrReplaceTempView('viewName')
    
    (
        batchDF
            ._jdf.sparkSession()
            .sql(
                """
   &amp;lt;&amp;lt;   merge statement &amp;gt;&amp;gt;
&amp;nbsp;
               """
    )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;._jdf.sparkSession().sql() returns a java object not a dataframe&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How do you get access to the results dataframe containing the (affected, inserted, updated, deleted) row counts?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 09:20:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/foreachbatch-get-results-from-batchdf-jdf-sparksession-sql-merge/m-p/12186#M7040</guid>
      <dc:creator>jm99</dc:creator>
      <dc:date>2023-01-13T09:20:49Z</dc:date>
    </item>
    <item>
      <title>Re: ForeachBatch() - Get results from  batchDF._jdf.sparkSession().sql('merge stmt')</title>
      <link>https://community.databricks.com/t5/data-engineering/foreachbatch-get-results-from-batchdf-jdf-sparksession-sql-merge/m-p/12187#M7041</link>
      <description>&lt;P&gt;Just found a solution...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Need to convert the Java Dataframe (jdf) to a DataFrame&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark import sql
&amp;nbsp;
def batchFunc(batchDF, batchId):
  batchDF.createOrReplaceTempView('viewName')
  sparkSession = batchDF._jdf.sparkSession()
&amp;nbsp;
  resJdf = sparkSession .sql('merge statement')
&amp;nbsp;
  resultDf = sql.DataFrame(resJdf, batchDF.sql_ctx)
  firstRow = resultDf.first()
&amp;nbsp;
  insertedRowCount = 0
  updatedRowCount = 0
  deletedRowCount = 0
  affectedRowCount = 0
&amp;nbsp;
  if firstRow:
      if ('num_affected_rows' in resultDf .columns):
          affectedRowCount += firstRow['num_affected_rows']
      if ('num_inserted_rows' in resultDf .columns):
          insertedRowCount += firstRow['num_inserted_rows']
      if ('num_updated_rows' in resultDf .columns):
          updatedRowCount += firstRow['num_updated_rows']
      if ('num_deleted_rows' in resultDf .columns):
          deletedRowCount += firstRow['num_deleted_rows']  
&amp;nbsp;
  # do what you want with the counts!&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 12:14:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/foreachbatch-get-results-from-batchdf-jdf-sparksession-sql-merge/m-p/12187#M7041</guid>
      <dc:creator>jm99</dc:creator>
      <dc:date>2023-01-13T12:14:57Z</dc:date>
    </item>
  </channel>
</rss>

