use of co.cask.cdap.api.dataset.lib.FileSet in project cdap by caskdata.
the class FileSetTest method testRollbackWithNonEmptyDir.
@Test
public void testRollbackWithNonEmptyDir() throws IOException, TransactionFailureException, DatasetManagementException {
FileSet fileSet1 = createFileset(testFileSetInstance1);
Location outputDir = fileSet1.getOutputLocation();
Assert.assertFalse(outputDir.exists());
Assert.assertTrue(outputDir.mkdirs());
Location outputFile = outputDir.append("outputFile");
// this will create the outputFile
outputFile.getOutputStream().close();
Assert.assertTrue(outputFile.exists());
Assert.assertTrue(outputDir.exists());
((FileSetDataset) fileSet1).onFailure();
// both the output dir and file in it should still exist
Assert.assertTrue(outputDir.exists());
Assert.assertTrue(outputFile.exists());
}
use of co.cask.cdap.api.dataset.lib.FileSet in project cdap by caskdata.
the class FileSetTest method testPermissions.
@Test
public void testPermissions() throws Exception {
String fsPermissions = "rwxrwx--x";
String customPermissions = "rwx--x--x";
String group = UserGroupInformation.getCurrentUser().getPrimaryGroupName();
// create one file set with default permission so that the namespace data dir exists
dsFrameworkUtil.createInstance("fileSet", OTHER_NAMESPACE.dataset("dummy"), DatasetProperties.EMPTY);
// determine the default permissions of created directories (we want to test with different perms)
String defaultPermissions = dsFrameworkUtil.getInjector().getInstance(NamespacedLocationFactory.class).get(OTHER_NAMESPACE).getPermissions();
if (fsPermissions.equals(defaultPermissions)) {
// swap the permissions so we can test with different file set permissions than the default
customPermissions = "rwxrwx--x";
fsPermissions = "rwx--x--x";
}
// create a dataset with configured permissions that are different from the default
DatasetId datasetId = OTHER_NAMESPACE.dataset("testPermFS");
dsFrameworkUtil.createInstance("fileSet", datasetId, FileSetProperties.builder().setBasePath("perm/test/path").setFilePermissions(fsPermissions).setFileGroup(group).build());
FileSet fs = dsFrameworkUtil.getInstance(datasetId);
// validate that the entire hierarchy of directories was created with the correct permissions
Location base = fs.getBaseLocation();
Assert.assertEquals(group, base.getGroup());
Assert.assertEquals(fsPermissions, base.getPermissions());
Location parent = Locations.getParent(base);
Assert.assertNotNull(parent);
Assert.assertEquals(group, parent.getGroup());
Assert.assertEquals(fsPermissions, parent.getPermissions());
parent = Locations.getParent(parent);
Assert.assertNotNull(parent);
Assert.assertEquals(group, parent.getGroup());
Assert.assertEquals(fsPermissions, parent.getPermissions());
Location nsRoot = Locations.getParent(parent);
Assert.assertNotNull(nsRoot);
Assert.assertNotEquals(fsPermissions, nsRoot.getPermissions());
// create an empty file and validate it is created with the fileset's permissions
Location child = base.append("a");
Location grandchild = child.append("b");
grandchild.getOutputStream().close();
Assert.assertEquals(group, child.getGroup());
Assert.assertEquals(group, grandchild.getGroup());
Assert.assertEquals(fsPermissions, child.getPermissions());
Assert.assertEquals(fsPermissions, grandchild.getPermissions());
// create an empty file with custom permissions and validate them
child = base.append("x");
grandchild = child.append("y");
grandchild.getOutputStream(customPermissions).close();
Assert.assertEquals(group, child.getGroup());
Assert.assertEquals(group, grandchild.getGroup());
Assert.assertEquals(customPermissions, child.getPermissions());
Assert.assertEquals(customPermissions, grandchild.getPermissions());
// instantiate the dataset with custom permissions in the runtime arguments
fs = dsFrameworkUtil.getInstance(datasetId, ImmutableMap.of(FileSetProperties.PROPERTY_FILES_PERMISSIONS, customPermissions));
// create an empty file with custom permissions and validate them
base = fs.getBaseLocation();
child = base.append("p");
grandchild = child.append("q");
grandchild.getOutputStream().close();
Assert.assertEquals(group, child.getGroup());
Assert.assertEquals(group, grandchild.getGroup());
Assert.assertEquals(customPermissions, child.getPermissions());
Assert.assertEquals(customPermissions, grandchild.getPermissions());
}
use of co.cask.cdap.api.dataset.lib.FileSet in project cdap by caskdata.
the class FileSetTest method testRollbackOfNonDirectoryOutput.
@Test
public void testRollbackOfNonDirectoryOutput() throws IOException, TransactionFailureException, DatasetManagementException {
// test deletion of an output location, pointing to a non-directory file
FileSet fileSet1 = createFileset(testFileSetInstance1);
Location outputFile = fileSet1.getOutputLocation();
Assert.assertFalse(outputFile.exists());
outputFile.getOutputStream().close();
Assert.assertTrue(outputFile.exists());
((FileSetDataset) fileSet1).onFailure();
// the output file should still not be deleted
Assert.assertTrue(outputFile.exists());
}
use of co.cask.cdap.api.dataset.lib.FileSet in project cdap by caskdata.
the class PipelineTest method testTextFileSourceAndMoveAction.
@Test
public void testTextFileSourceAndMoveAction() throws Exception {
// create the pipeline config
String moveFromName = "sourceTestMoveFrom";
String inputName = "sourceTestInput";
String outputName = "sourceTestOutput";
Map<String, String> actionProperties = new HashMap<>();
actionProperties.put(FilesetMoveAction.Conf.SOURCE_FILESET, "sourceTestMoveFrom");
actionProperties.put(FilesetMoveAction.Conf.DEST_FILESET, inputName);
ETLStage moveAction = new ETLStage("moveInput", new ETLPlugin(FilesetMoveAction.NAME, Action.PLUGIN_TYPE, actionProperties, null));
Map<String, String> sourceProperties = new HashMap<>();
sourceProperties.put(TextFileSetSource.Conf.FILESET_NAME, inputName);
sourceProperties.put(TextFileSetSource.Conf.CREATE_IF_NOT_EXISTS, "true");
sourceProperties.put(TextFileSetSource.Conf.DELETE_INPUT_ON_SUCCESS, "true");
sourceProperties.put(TextFileSetSource.Conf.FILES, "${file}");
ETLStage source = new ETLStage("source", new ETLPlugin(TextFileSetSource.NAME, BatchSource.PLUGIN_TYPE, sourceProperties, null));
ETLStage sink = new ETLStage("sink", MockSink.getPlugin(outputName));
ETLBatchConfig pipelineConfig = ETLBatchConfig.builder("* * * * *").addStage(source).addStage(sink).addStage(moveAction).addConnection(moveAction.getName(), source.getName()).addConnection(source.getName(), sink.getName()).build();
// create the move from fileset
addDatasetInstance(FileSet.class.getName(), moveFromName);
// create the pipeline
ApplicationId pipelineId = NamespaceId.DEFAULT.app("textSourceTestPipeline");
ApplicationManager appManager = deployApplication(pipelineId, new AppRequest<>(APP_ARTIFACT, pipelineConfig));
// write some files that will be moved to the input fileset
DataSetManager<FileSet> moveFromManager = getDataset(moveFromName);
// this file starts with '.' and should be ignored.
Location invisibleFile = moveFromManager.get().getBaseLocation().append(".hidden");
try (OutputStream outputStream = invisibleFile.getOutputStream()) {
outputStream.write(Bytes.toBytes("this should not be read"));
}
// this file should be moved
String line1 = "Hello World!";
String line2 = "Good to meet you";
String line3 = "My name is Hal";
String inputText = line1 + "\n" + line2 + "\n" + line3;
Location inputFile = moveFromManager.get().getBaseLocation().append("inputFile");
try (OutputStream outputStream = inputFile.getOutputStream()) {
outputStream.write(Bytes.toBytes(inputText));
}
// run the pipeline
Map<String, String> runtimeArgs = new HashMap<>();
// the ${file} macro will be substituted with "inputFile" for our pipeline run
runtimeArgs.put("file", "inputFile");
WorkflowManager workflowManager = appManager.getWorkflowManager(SmartWorkflow.NAME);
workflowManager.start(runtimeArgs);
workflowManager.waitForFinish(4, TimeUnit.MINUTES);
// check the pipeline output
DataSetManager<Table> outputManager = getDataset(outputName);
Set<StructuredRecord> outputRecords = new HashSet<>();
outputRecords.addAll(MockSink.readOutput(outputManager));
Set<StructuredRecord> expected = new HashSet<>();
expected.add(StructuredRecord.builder(TextFileSetSource.OUTPUT_SCHEMA).set("position", (long) inputText.indexOf(line1)).set("text", line1).build());
expected.add(StructuredRecord.builder(TextFileSetSource.OUTPUT_SCHEMA).set("position", (long) inputText.indexOf(line2)).set("text", line2).build());
expected.add(StructuredRecord.builder(TextFileSetSource.OUTPUT_SCHEMA).set("position", (long) inputText.indexOf(line3)).set("text", line3).build());
Assert.assertEquals(expected, outputRecords);
// check that the input file does not exist in the moveFrom fileSet,
// and was deleted by the source in the input fileSet
Assert.assertFalse(moveFromManager.get().getBaseLocation().append("inputFile").exists());
DataSetManager<FileSet> inputManager = getDataset(inputName);
Assert.assertFalse(inputManager.get().getBaseLocation().append("inputFile").exists());
}
use of co.cask.cdap.api.dataset.lib.FileSet in project cdap by caskdata.
the class PipelineTest method testTextFileSinkAndDeletePostAction.
@Test
public void testTextFileSinkAndDeletePostAction() throws Exception {
// create the pipeline config
String inputName = "sinkTestInput";
String outputName = "sinkTestOutput";
String outputDirName = "users";
ETLStage source = new ETLStage("source", MockSource.getPlugin(inputName));
Map<String, String> sinkProperties = new HashMap<>();
sinkProperties.put(TextFileSetSink.Conf.FILESET_NAME, outputName);
sinkProperties.put(TextFileSetSink.Conf.FIELD_SEPARATOR, "|");
sinkProperties.put(TextFileSetSink.Conf.OUTPUT_DIR, "${dir}");
ETLStage sink = new ETLStage("sink", new ETLPlugin(TextFileSetSink.NAME, BatchSink.PLUGIN_TYPE, sinkProperties, null));
Map<String, String> actionProperties = new HashMap<>();
actionProperties.put(FilesetDeletePostAction.Conf.FILESET_NAME, outputName);
// mapreduce writes multiple files to the output directory. Along with the actual output,
// there are various .crc files that do not contain any of the output content.
actionProperties.put(FilesetDeletePostAction.Conf.DELETE_REGEX, ".*\\.crc|_SUCCESS");
actionProperties.put(FilesetDeletePostAction.Conf.DIRECTORY, outputDirName);
ETLStage postAction = new ETLStage("cleanup", new ETLPlugin(FilesetDeletePostAction.NAME, PostAction.PLUGIN_TYPE, actionProperties, null));
ETLBatchConfig pipelineConfig = ETLBatchConfig.builder("* * * * *").addStage(source).addStage(sink).addPostAction(postAction).addConnection(source.getName(), sink.getName()).build();
// create the pipeline
ApplicationId pipelineId = NamespaceId.DEFAULT.app("textSinkTestPipeline");
ApplicationManager appManager = deployApplication(pipelineId, new AppRequest<>(APP_ARTIFACT, pipelineConfig));
// write some data to the input fileset
Schema inputSchema = Schema.recordOf("test", Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("item", Schema.of(Schema.Type.STRING)));
Map<String, String> users = new HashMap<>();
users.put("samuel", "wallet");
users.put("dwayne", "rock");
users.put("christopher", "cowbell");
List<StructuredRecord> inputRecords = new ArrayList<>();
for (Map.Entry<String, String> userEntry : users.entrySet()) {
String name = userEntry.getKey();
String item = userEntry.getValue();
inputRecords.add(StructuredRecord.builder(inputSchema).set("name", name).set("item", item).build());
}
DataSetManager<Table> inputManager = getDataset(inputName);
MockSource.writeInput(inputManager, inputRecords);
// run the pipeline
Map<String, String> runtimeArgs = new HashMap<>();
// the ${dir} macro will be substituted with "users" for our pipeline run
runtimeArgs.put("dir", outputDirName);
WorkflowManager workflowManager = appManager.getWorkflowManager(SmartWorkflow.NAME);
workflowManager.start(runtimeArgs);
workflowManager.waitForFinish(4, TimeUnit.MINUTES);
// check the pipeline output
DataSetManager<FileSet> outputManager = getDataset(outputName);
FileSet output = outputManager.get();
Location outputDir = output.getBaseLocation().append(outputDirName);
Map<String, String> actual = new HashMap<>();
for (Location outputFile : outputDir.list()) {
if (outputFile.getName().endsWith(".crc") || "_SUCCESS".equals(outputFile.getName())) {
Assert.fail("Post action did not delete file " + outputFile.getName());
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(outputFile.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\|");
actual.put(parts[0], parts[1]);
}
}
}
Assert.assertEquals(actual, users);
}
Aggregations