<?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>question Re: hql Script Error Handling in Beeline Execution in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/hql-Script-Error-Handling-in-Beeline-Execution/m-p/406842#M252571</link>
    <description>&lt;UL&gt;&lt;LI&gt;When using the &lt;FONT color="#FF0000"&gt;-f&lt;/FONT&gt; flag with Beeline to execute an HQL script file, Beeline follows a &lt;STRONG&gt;fail-fast approach&lt;/STRONG&gt;. If any &lt;STRONG&gt;single SQL statement fails&lt;/STRONG&gt;, Beeline &lt;STRONG&gt;will stop execution immediately&lt;/STRONG&gt; and exit with an error.&lt;/LI&gt;&lt;LI&gt;This behavior helps ensure &lt;STRONG&gt;data integrity ,&amp;nbsp;&lt;/STRONG&gt;it avoids executing dependent or subsequent statements that may rely on a failed operation (e.g., missing table, failed insert).&lt;/LI&gt;&lt;LI&gt;Beeline &lt;STRONG&gt;does not support built-in error handling or continuation&lt;/STRONG&gt; of script execution after a failed statement within the same script file.&lt;/LI&gt;&lt;LI&gt;To continue executing remaining statements even if one fails:&lt;/LI&gt;&lt;LI&gt;Use a &lt;STRONG&gt;Bash (or similar) wrapper script&lt;/STRONG&gt; to:&lt;BR /&gt;&lt;BR /&gt;Loop over each statement/block.&lt;BR /&gt;Execute each one using a separate beeline -e "&amp;lt;statement&amp;gt;"&lt;BR /&gt;Check the exit code after each call.&lt;BR /&gt;Log errors to a file (e.g., stderr, exit code, or custom messages).&lt;BR /&gt;Proceed to the next statement regardless of the previous outcome.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Sample BASH Script&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="markup"&gt;[root@node4 ~]# cat run_hive_statements.sh
#!/bin/bash

# Set the HQL file to read SQL-like Hive queries from
HQL_FILE="sample_hql_success_failure.hql"

# Log file for capturing errors or output, named with the current date
LOG_FILE="error_log_$(date +%Y%m%d).log"

# Read the contents of the HQL file into a variable
HQL_CONTENT=$(cat "$HQL_FILE")

# Preprocess the file: ensure each SQL statement ends with a newline after semicolon
STATEMENTS=$(sed 's/;/;\n/g' "$HQL_FILE")

# Loop through each SQL-like statement (semicolon-separated)
for STATEMENT in "${STATEMENTS[@]}"; do

  # Clean up the statement: remove empty lines and comments
  SQL_SCRIPT=$(echo "$STATEMENT" | sed '/^\s*$/d' | sed '/^--.*$/d')

  # If the cleaned SQL statement is not empty, proceed
  if [ -n "$SQL_SCRIPT" ]; then

    # Split potential multiline statements using awk with record separator as ';'
    echo "$SQL_SCRIPT" | awk '
      BEGIN { RS=";" }
      {
        gsub(/^[ \t\r\n]+|[ \t\r\n]+$/, "", $0);  # Trim leading/trailing whitespace
        if(length($0)) print $0 ";"
      }
    ' | while read -r STATEMENT; do

      # Execute each final cleaned and trimmed SQL statement
      echo "Executing: $STATEMENT"
      beeline -n hive -p hive -e "$STATEMENT" &amp;gt;&amp;gt; "$LOG_FILE" 2&amp;gt;&amp;amp;1
      EXIT_CODE=$?
      if [ "$EXIT_CODE" -ne 0 ]; then
         echo "Error executing statement:"
         echo "$FINAL_STATEMENT;" &amp;gt;&amp;gt; "$LOG_FILE"
         echo "Beeline exited with code: $EXIT_CODE" &amp;gt;&amp;gt; "$LOG_FILE"
      fi
    done
  fi
done
[root@node4 ~]#​&lt;/LI-CODE&gt;&lt;UL&gt;&lt;LI&gt;Sample HQL file&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="markup"&gt;[root@node4 ~]# cat sample_hql_success_failure.hql
-- This script demonstrates both successful and failing INSERT statements

-- Successful INSERT
CREATE TABLE IF NOT EXISTS successful_table (id INT, value STRING);
INSERT INTO successful_table VALUES (1, 'success1');
INSERT INTO successful_table VALUES (2, 'success2');
SELECT * FROM successful_table;

-- Intentionally failing INSERT (ParseException)
CREATE TABLE IF NOT EXISTS failing_table (id INT, value INT);
INSERT INTO failing_table VALUES (1, 'this_will_fail);
SELECT * FROM failing_table;

-- Another successful INSERT (will only run if the calling script continues)
CREATE TABLE IF NOT EXISTS another_successful_table (id INT, data STRING);
INSERT INTO another_successful_table VALUES (101, 'continued_data');
SELECT * FROM another_successful_table;

-- Intentionally failing INSERT (table does not exist)
INSERT INTO non_existent_table SELECT * FROM successful_table;
[root@node4 ~]#​&lt;/LI-CODE&gt;&lt;UL&gt;&lt;LI&gt;Sample ERROR log created as part of the script.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="markup"&gt;[root@node4 ~]# grep -iw "error" error_log_20250423.log
Error: Error while compiling statement: FAILED: ParseException line 1:54 character '&amp;lt;EOF&amp;gt;' not supported here (state=42000,code=40000)
Error: Error while compiling statement: FAILED: SemanticException [Error 10001]: Line 1:12 Table not found 'non_existent_table' (state=42S02,code=10001)
[root@node4 ~]#​&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 23 Apr 2025 07:46:46 GMT</pubDate>
    <dc:creator>ggangadharan</dc:creator>
    <dc:date>2025-04-23T07:46:46Z</dc:date>
    <item>
      <title>hql Script Error Handling in Beeline Execution</title>
      <link>https://community.cloudera.com/t5/Support-Questions/hql-Script-Error-Handling-in-Beeline-Execution/m-p/406797#M252561</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I’ve created a .hql file containing multiple INSERT&amp;nbsp; statements&amp;nbsp; for Hive tables and I'm calling the script using Beeline. I need help with capturing error codes in a log file.&lt;/P&gt;&lt;P&gt;Also, if any INSERT statement fails, I want the continue the&amp;nbsp; remaining statements&amp;nbsp; and need to be write a log into file.&lt;/P&gt;&lt;P&gt;I am using below command but it is writing more details log&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;gt; archive_$(date +\%Y\%m\%d).log 2&amp;gt;&amp;amp;1'&lt;/P&gt;&lt;P&gt;Please advise how to handle this?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Apr 2025 06:23:41 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/hql-Script-Error-Handling-in-Beeline-Execution/m-p/406797#M252561</guid>
      <dc:creator>APentyala</dc:creator>
      <dc:date>2025-04-22T06:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: hql Script Error Handling in Beeline Execution</title>
      <link>https://community.cloudera.com/t5/Support-Questions/hql-Script-Error-Handling-in-Beeline-Execution/m-p/406842#M252571</link>
      <description>&lt;UL&gt;&lt;LI&gt;When using the &lt;FONT color="#FF0000"&gt;-f&lt;/FONT&gt; flag with Beeline to execute an HQL script file, Beeline follows a &lt;STRONG&gt;fail-fast approach&lt;/STRONG&gt;. If any &lt;STRONG&gt;single SQL statement fails&lt;/STRONG&gt;, Beeline &lt;STRONG&gt;will stop execution immediately&lt;/STRONG&gt; and exit with an error.&lt;/LI&gt;&lt;LI&gt;This behavior helps ensure &lt;STRONG&gt;data integrity ,&amp;nbsp;&lt;/STRONG&gt;it avoids executing dependent or subsequent statements that may rely on a failed operation (e.g., missing table, failed insert).&lt;/LI&gt;&lt;LI&gt;Beeline &lt;STRONG&gt;does not support built-in error handling or continuation&lt;/STRONG&gt; of script execution after a failed statement within the same script file.&lt;/LI&gt;&lt;LI&gt;To continue executing remaining statements even if one fails:&lt;/LI&gt;&lt;LI&gt;Use a &lt;STRONG&gt;Bash (or similar) wrapper script&lt;/STRONG&gt; to:&lt;BR /&gt;&lt;BR /&gt;Loop over each statement/block.&lt;BR /&gt;Execute each one using a separate beeline -e "&amp;lt;statement&amp;gt;"&lt;BR /&gt;Check the exit code after each call.&lt;BR /&gt;Log errors to a file (e.g., stderr, exit code, or custom messages).&lt;BR /&gt;Proceed to the next statement regardless of the previous outcome.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Sample BASH Script&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="markup"&gt;[root@node4 ~]# cat run_hive_statements.sh
#!/bin/bash

# Set the HQL file to read SQL-like Hive queries from
HQL_FILE="sample_hql_success_failure.hql"

# Log file for capturing errors or output, named with the current date
LOG_FILE="error_log_$(date +%Y%m%d).log"

# Read the contents of the HQL file into a variable
HQL_CONTENT=$(cat "$HQL_FILE")

# Preprocess the file: ensure each SQL statement ends with a newline after semicolon
STATEMENTS=$(sed 's/;/;\n/g' "$HQL_FILE")

# Loop through each SQL-like statement (semicolon-separated)
for STATEMENT in "${STATEMENTS[@]}"; do

  # Clean up the statement: remove empty lines and comments
  SQL_SCRIPT=$(echo "$STATEMENT" | sed '/^\s*$/d' | sed '/^--.*$/d')

  # If the cleaned SQL statement is not empty, proceed
  if [ -n "$SQL_SCRIPT" ]; then

    # Split potential multiline statements using awk with record separator as ';'
    echo "$SQL_SCRIPT" | awk '
      BEGIN { RS=";" }
      {
        gsub(/^[ \t\r\n]+|[ \t\r\n]+$/, "", $0);  # Trim leading/trailing whitespace
        if(length($0)) print $0 ";"
      }
    ' | while read -r STATEMENT; do

      # Execute each final cleaned and trimmed SQL statement
      echo "Executing: $STATEMENT"
      beeline -n hive -p hive -e "$STATEMENT" &amp;gt;&amp;gt; "$LOG_FILE" 2&amp;gt;&amp;amp;1
      EXIT_CODE=$?
      if [ "$EXIT_CODE" -ne 0 ]; then
         echo "Error executing statement:"
         echo "$FINAL_STATEMENT;" &amp;gt;&amp;gt; "$LOG_FILE"
         echo "Beeline exited with code: $EXIT_CODE" &amp;gt;&amp;gt; "$LOG_FILE"
      fi
    done
  fi
done
[root@node4 ~]#​&lt;/LI-CODE&gt;&lt;UL&gt;&lt;LI&gt;Sample HQL file&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="markup"&gt;[root@node4 ~]# cat sample_hql_success_failure.hql
-- This script demonstrates both successful and failing INSERT statements

-- Successful INSERT
CREATE TABLE IF NOT EXISTS successful_table (id INT, value STRING);
INSERT INTO successful_table VALUES (1, 'success1');
INSERT INTO successful_table VALUES (2, 'success2');
SELECT * FROM successful_table;

-- Intentionally failing INSERT (ParseException)
CREATE TABLE IF NOT EXISTS failing_table (id INT, value INT);
INSERT INTO failing_table VALUES (1, 'this_will_fail);
SELECT * FROM failing_table;

-- Another successful INSERT (will only run if the calling script continues)
CREATE TABLE IF NOT EXISTS another_successful_table (id INT, data STRING);
INSERT INTO another_successful_table VALUES (101, 'continued_data');
SELECT * FROM another_successful_table;

-- Intentionally failing INSERT (table does not exist)
INSERT INTO non_existent_table SELECT * FROM successful_table;
[root@node4 ~]#​&lt;/LI-CODE&gt;&lt;UL&gt;&lt;LI&gt;Sample ERROR log created as part of the script.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="markup"&gt;[root@node4 ~]# grep -iw "error" error_log_20250423.log
Error: Error while compiling statement: FAILED: ParseException line 1:54 character '&amp;lt;EOF&amp;gt;' not supported here (state=42000,code=40000)
Error: Error while compiling statement: FAILED: SemanticException [Error 10001]: Line 1:12 Table not found 'non_existent_table' (state=42S02,code=10001)
[root@node4 ~]#​&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 23 Apr 2025 07:46:46 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/hql-Script-Error-Handling-in-Beeline-Execution/m-p/406842#M252571</guid>
      <dc:creator>ggangadharan</dc:creator>
      <dc:date>2025-04-23T07:46:46Z</dc:date>
    </item>
  </channel>
</rss>

