Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main/scala/com/databricks/spark/redshift/Parameters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,28 @@ private [redshift] object Parameters extends Logging {
* use of standard system properties, environment variables, or IAM role configuration if available.
*/
def credentialsString(configuration: Configuration) = {
val ((_, accessKeyId), (_, secretAccessKey)) = credentialsTuple(configuration)

s"aws_access_key_id=$accessKeyId;aws_secret_access_key=$secretAccessKey"
}

/**
* Looks up "aws_access_key_id" and "aws_secret_access_key" in the parameter map
* and ensures they are set on the Configuration. If no credentials have been provided,
* this function will instead try using the Hadoop Configuration fs.* settings for the provided tempDir
* scheme, and if that also fails, it finally tries AWS DefaultCredentialsProviderChain, which makes
* use of standard system properties, environment variables, or IAM role configuration if available.
*/
def setCredentials(configuration: Configuration): Unit = {
val ((accessKeyIdProp, accessKeyId), (secretAccessKeyProp, secretAccessKey)) = credentialsTuple(configuration)

configuration.setIfUnset(accessKeyIdProp, accessKeyId)
configuration.setIfUnset(secretAccessKeyProp, secretAccessKey)
}

private def credentialsTuple(configuration: Configuration) = {
val scheme = new URI(tempDir).getScheme
val hadoopConfPrefix = s"fs.$scheme}"
val hadoopConfPrefix = s"fs.$scheme"

val (accessKeyId, secretAccessKey) =
if(parameters.contains("aws_access_key_id")) {
Expand All @@ -192,7 +211,7 @@ private [redshift] object Parameters extends Logging {
}
}

s"aws_access_key_id=$accessKeyId;aws_secret_access_key=$secretAccessKey"
((s"$hadoopConfPrefix.awsAccessKeyId", accessKeyId), (s"$hadoopConfPrefix.awsSecretAccessKey", secretAccessKey))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ case class RedshiftRelation(jdbcWrapper: JDBCWrapper, params: MergedParameters,

protected def makeRdd(schema: StructType): RDD[Row] = {
val sc = sqlContext.sparkContext
params.setCredentials(sc.hadoopConfiguration)
val rdd = sc.newAPIHadoopFile(params.tempPath, classOf[RedshiftInputFormat],
classOf[java.lang.Long], classOf[Array[String]], sc.hadoopConfiguration)
rdd.values.map(Conversions.rowConverter(schema))
Expand Down Expand Up @@ -107,7 +108,7 @@ case class RedshiftRelation(jdbcWrapper: JDBCWrapper, params: MergedParameters,
val filterClauses = filters map {
case EqualTo(attr, value) => s"${sqlQuote(attr)} = ${compileValue(value)}"
case LessThan(attr, value) => s"${sqlQuote(attr)} < ${compileValue(value)}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Another test coverage issue :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, there's a separate earlier PR just for that one line... got pulled into this branch by accident as I'm trying to get things working

case GreaterThan(attr, value) => s"${sqlQuote(attr)}) > ${compileValue(value)}"
case GreaterThan(attr, value) => s"${sqlQuote(attr)} > ${compileValue(value)}"
case LessThanOrEqual(attr, value) => s"${sqlQuote(attr)} <= ${compileValue(value)}"
case GreaterThanOrEqual(attr, value) => s"${sqlQuote(attr)} >= ${compileValue(value)}"
} mkString "AND"
Expand Down