Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-3093 HIVE Support for QueryDatabaseTable #1281

Closed
wants to merge 1 commit into from

Conversation

patricker
Copy link
Contributor

This update adds HIVE support to QueryDatabaseTable.

I included a few changes to GenerateTableFetch, but these were only for compatability with the new properties added to the DB Adapter interface. This does NOT add support to GenerateTableFetch for HIVE (as I don't think paging HIVE is possible).

I'm expecting this will need a few tweaks, just let me know.

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically master)?

  • Is your initial contribution a single, squashed commit?

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
  • Have you written or updated unit tests to verify your changes?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
  • If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

@@ -186,8 +186,10 @@ public void onTrigger(final ProcessContext context, final ProcessSessionFactory
try (final Connection con = dbcpService.getConnection();
final Statement st = con.createStatement()) {

final Integer queryTimeout = context.getProperty(QUERY_TIMEOUT).asTimePeriod(TimeUnit.SECONDS).intValue();
st.setQueryTimeout(queryTimeout); // timeout in seconds
if(dbAdapter.getSupportsStatementTimeout()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, but when we were talking about supporting Hive in ExecuteSQL, there were concerns about those drivers that don't support statement timeouts, which is one reason why there's a separate Hive NAR (and Select/PutHiveQL processors). This was before DatabaseAdapter came along, so perhaps now this can be alleviated as you've done here. Does this work when using a HiveConnectionPool? Or are you using a DBCPConnectionPool that points to Hive JARs? It seems like if this works with a HiveConnectionPool (with your changes to JdbcCommon below), then the Select/PutHiveQL processors could be deprecated.

Copy link
Contributor Author

@patricker patricker Nov 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a HiveConnectionPool. Still easiest way to handle Kerberos, custom Hive-Site.xml, etc...

@@ -284,7 +291,7 @@ public static Schema createSchema(final ResultSet rs, String recordName, boolean
break;

case INTEGER:
if (meta.isSigned(i)) {
if (!dbAdapter.getSupportsMetaDataColumnIsSigned() || meta.isSigned(i)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the DB doesn't support isSigned(), should we treat them all as Longs? I assume it's done this way if Hive returns INTEGER as the type and Integer objects for values. I wonder if there's another DB that doesn't support isSigned() and could return Long objects. In that case we might want another check for getSupportsMetaDataColumnIsSigned() (and for the type of object) when storing the values into the record.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your spot on. Originally I had this without the !, but in my table I had an INT and when I tried to put it into an Avro Long field it threw an exception.

@@ -300,7 +307,7 @@ public static Schema createSchema(final ResultSet rs, String recordName, boolean
// Check the precision of the BIGINT. Some databases allow arbitrary precision (> 19), but Avro won't handle that.
// If the precision > 19 (or is negative), use a string for the type, otherwise use a long. The object(s) will be converted
// to strings as necessary
int precision = meta.getPrecision(i);
int precision = dbAdapter.getSupportsMetaDataColumnGetPrecision()?meta.getPrecision(i):0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we assume a default of zero here or < 0?

Copy link
Contributor Author

@patricker patricker Nov 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably -1 would be a safer choice if we can't retrieve precision. HIVE appears to supports up to DECIMAL(38,18), but who knows what might happen with other databases down the road; we don't want to accidentally lose data or start throwing exceptions for overflows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we don't need this. I got tired of fighting exceptions one after another and started reading the HIVE JDBC driver code to find unsupported methods. It looks like I misread the code and getPrecision is a supported HIVE JDBC method call.

https://github.com/apache/hive/blob/ff67cdda1c538dc65087878eeba3e165cf3230f4/jdbc/src/java/org/apache/hive/jdbc/HiveResultSetMetaData.java

query.append(limit);
}
if (offset != null && offset > 0) {
query.append(" OFFSET ");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattyb149 I copy/pasted this from the Generic adapter, but from what I can find HIVE has no support for OFFSET. Do you think this should throw an exception at this stage?

@@ -161,7 +161,7 @@
.description("The type/flavor of database, used for generating database-specific code. In many cases the Generic type "
+ "should suffice, but some databases (such as Oracle) require custom SQL clauses. ")
.allowableValues(dbAdapters.keySet())
.defaultValue(dbAdapters.values().stream().findFirst().get().getName())
.defaultValue("Generic")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattyb149 In case your curious, I found that Generic may or may not be selected as the default after you start adding new DBAdapters. I ran into this exact issue when I added a new Hive DBAdapter, so I just hard coded it rather then having the default value possibly change.

@mattyb149
Copy link
Contributor

Do you mind rebasing this against the latest master? Please and thanks!

@patricker
Copy link
Contributor Author

@mattyb149 I am still planning to update this PR. Any chance you could take a look at #1510 first and see if we can get it pulled in? It is simpler and has some direct conflicts/overlap with this PR, I'd rather get it into master before I rebase and cleanup my Hive work.

@mattyb149
Copy link
Contributor

#1510 is now merged, do you mind rebasing this one? Please and thanks!

@patricker
Copy link
Contributor Author

@mattyb149 I'm working through this. I found a couple of issues that I'm figuring out.
Also, I might have a way to do GenerateTableFetch with HIVE, using logic similar to what you do for SQL 2008 using ROW NUMBER.

@patricker
Copy link
Contributor Author

patricker commented May 3, 2017

@mattyb149 I've completed the changes. I added support for GenerateTableFetch, though Hive being hive it's not all that fast...

There is one quirk, and I feel it should be documented but I'm not sure where. When using QueryDatabaseTable with Hive you need to qualify your max value column with the table name. This is because Hive always qualifies the column names in the ResultSet. Also, as a result of this, you'll need to Normalize the column names.

With GenerateTableFetch, since it pulls back the max value through a query and aliases the column name, I don't think the above issue is a problem.

ExecuteSQL should work without any special changes, just remember to select a Database Adapter when testing HIVE, this didn't used to be an available option.

@ijokarumawak
Copy link
Member

@patricker This PR has now conflicts since #1798 (NIFI-2624: Avro logical types) and others are merged. Would you mind rebasing this? I'd like to review once the merge conflicts get addressed, and test to see if logical types can be used with Hive tables.
Thank you for your contribution, time and effort!

@patricker
Copy link
Contributor Author

@ijokarumawak I'll work on it.

@mattyb149
Copy link
Contributor

@patricker Are you working on this one? If not, perhaps @ijokarumawak or I can take over.

@patricker
Copy link
Contributor Author

@mattyb149 @ijokarumawak Thanks for checking in on me. I had planned to work on this, but due to recent shifts at work I don't even use HIVE anymore... If someone wants to put a bow on this I'd appreciate it.

@MikeThomsen MikeThomsen mentioned this pull request Mar 3, 2018
11 tasks
@patricker patricker closed this Apr 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants