forked from apache/gobblin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#57 from rayortigas/s3
S3 support.
- Loading branch information
Showing
5 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
gobblin-core/src/test/java/gobblin/source/extractor/hadoop/HadoopFsHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package gobblin.source.extractor.hadoop; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
import gobblin.configuration.ConfigurationKeys; | ||
import gobblin.configuration.SourceState; | ||
import gobblin.source.extractor.filebased.FileBasedHelperException; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class HadoopFsHelperTest { | ||
|
||
@Test(expectedExceptions = IllegalArgumentException.class) | ||
public void testConnectFailsWithS3URLWithoutAWSCredentials() throws FileBasedHelperException { | ||
Configuration conf = new Configuration(); // plain conf, no S3 credentials | ||
SourceState sourceState = new SourceState(); | ||
sourceState.setProp(ConfigurationKeys.SOURCE_FILEBASED_FS_URI, "s3://support.elasticmapreduce/spark/install-spark/"); | ||
HadoopFsHelper fsHelper = new HadoopFsHelper(sourceState, conf); | ||
fsHelper.connect(); | ||
} | ||
|
||
@Test | ||
public void testGetFileStreamSucceedsWithUncompressedFile() throws FileBasedHelperException, IOException { | ||
SourceState sourceState = new SourceState(); | ||
URL rootUrl = getClass().getResource("/source/"); | ||
String rootPath = rootUrl.toString(); | ||
sourceState.setProp(ConfigurationKeys.SOURCE_FILEBASED_FS_URI, rootPath); | ||
HadoopFsHelper fsHelper = new HadoopFsHelper(sourceState); | ||
|
||
fsHelper.connect(); | ||
URL url = getClass().getResource("/source/simple.tsv"); | ||
String path = url.toString(); | ||
InputStream in = fsHelper.getFileStream(path); | ||
String contents = IOUtils.toString(in, "UTF-8"); | ||
Assert.assertEquals(contents, "A\t1\nB\t2\n"); | ||
} | ||
|
||
@Test | ||
public void testGetFileStreamSucceedsWithGZIPFile() throws FileBasedHelperException, IOException { | ||
SourceState sourceState = new SourceState(); | ||
URL rootUrl = getClass().getResource("/source/"); | ||
String rootPath = rootUrl.toString(); | ||
sourceState.setProp(ConfigurationKeys.SOURCE_FILEBASED_FS_URI, rootPath); | ||
HadoopFsHelper fsHelper = new HadoopFsHelper(sourceState); | ||
|
||
fsHelper.connect(); | ||
URL url = getClass().getResource("/source/simple.tsv.gz"); | ||
String path = url.toString(); | ||
InputStream in = fsHelper.getFileStream(path); | ||
String contents = IOUtils.toString(in, "UTF-8"); | ||
Assert.assertEquals(contents, "A\t1\nB\t2\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
A 1 | ||
B 2 |
Binary file not shown.
22 changes: 22 additions & 0 deletions
22
gobblin-utility/src/main/java/gobblin/util/HadoopUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package gobblin.util; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
|
||
public class HadoopUtils { | ||
public static Configuration newConfiguration() { | ||
Configuration conf = new Configuration(); | ||
|
||
// Explicitly check for S3 environment variables, so that Hadoop can access s3 and s3n URLs. | ||
// h/t https://github.com/apache/spark/blob/master/core/src/main/scala/org/apache/spark/deploy/SparkHadoopUtil.scala | ||
String awsAccessKeyId = System.getenv("AWS_ACCESS_KEY_ID"); | ||
String awsSecretAccessKey = System.getenv("AWS_SECRET_ACCESS_KEY"); | ||
if (awsAccessKeyId != null && awsSecretAccessKey != null) { | ||
conf.set("fs.s3.awsAccessKeyId", awsAccessKeyId); | ||
conf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey); | ||
conf.set("fs.s3n.awsAccessKeyId", awsAccessKeyId); | ||
conf.set("fs.s3n.awsSecretAccessKey", awsSecretAccessKey); | ||
} | ||
|
||
return conf; | ||
} | ||
} |