Search in sources :

Example 1 with Resource

use of org.openstreetmap.atlas.streaming.resource.Resource in project atlas-checks by osmlab.

the class ConfigurationResolver method loadConfiguration.

/**
 * Resolves a {@link Configuration} for {@link Command}s given the {@link CommandMap} and
 * {@link Command.Switch}s available
 *
 * @param commandMap
 *            the {@link Command}'s {@link CommandMap}
 * @param keyFiles
 *            the {@link Command.Switch} containing a list of URIs for configuration
 * @param keyJson
 *            the {@link Command.Switch} containing inline JSON configuration
 * @return a new {@link Configuration}
 */
public static Configuration loadConfiguration(final CommandMap commandMap, final Command.Switch<StringList> keyFiles, final Command.Switch<String> keyJson) {
    final List<InputStream> configurationSources = new ArrayList<>();
    ConfigurationResolver.getResourceAsStream("application.json").ifPresent(configurationSources::add);
    commandMap.getOption(keyFiles).ifPresent(files -> ((StringList) files).forEach(uri -> ConfigurationResolver.getResourceAsStream(URI.create(uri)).ifPresent(configurationSources::add)));
    commandMap.getOption(keyJson).map(value -> new ByteArrayInputStream(value.toString().getBytes(StandardCharsets.UTF_8))).ifPresent(configurationSources::add);
    final List<Resource> configurationResources = configurationSources.stream().map(InputStreamResource::new).collect(Collectors.toList());
    final Configuration configuration;
    Throwable thrown = null;
    try {
        configuration = new MergedConfiguration(Iterables.head(configurationResources), Iterables.tail(configurationResources));
    } finally {
        for (final InputStream source : configurationSources) {
            try {
                source.close();
            } catch (final Throwable throwable) {
                thrown = throwable;
            }
        }
    }
    if (thrown != null) {
        throw new CoreException("Failed to load configuration", thrown);
    }
    return configuration;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) StandardConfiguration(org.openstreetmap.atlas.utilities.configuration.StandardConfiguration) LoggerFactory(org.slf4j.LoggerFactory) HttpStatus(org.apache.http.HttpStatus) ArrayList(java.util.ArrayList) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) URI(java.net.URI) Command(org.openstreetmap.atlas.utilities.runtime.Command) File(org.openstreetmap.atlas.streaming.resource.File) MergedConfiguration(org.openstreetmap.atlas.utilities.configuration.MergedConfiguration) Logger(org.slf4j.Logger) Resource(org.openstreetmap.atlas.streaming.resource.Resource) CoreException(org.openstreetmap.atlas.exception.CoreException) CommandMap(org.openstreetmap.atlas.utilities.runtime.CommandMap) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) GetResource(org.openstreetmap.atlas.streaming.resource.http.GetResource) List(java.util.List) Iterables(org.openstreetmap.atlas.utilities.collections.Iterables) InputStreamResource(org.openstreetmap.atlas.streaming.resource.InputStreamResource) Optional(java.util.Optional) StringResource(org.openstreetmap.atlas.streaming.resource.StringResource) StringList(org.openstreetmap.atlas.utilities.collections.StringList) Assert(org.junit.Assert) InputStream(java.io.InputStream) MergedConfiguration(org.openstreetmap.atlas.utilities.configuration.MergedConfiguration) StandardConfiguration(org.openstreetmap.atlas.utilities.configuration.StandardConfiguration) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) MergedConfiguration(org.openstreetmap.atlas.utilities.configuration.MergedConfiguration) CoreException(org.openstreetmap.atlas.exception.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(org.openstreetmap.atlas.streaming.resource.Resource) GetResource(org.openstreetmap.atlas.streaming.resource.http.GetResource) InputStreamResource(org.openstreetmap.atlas.streaming.resource.InputStreamResource) StringResource(org.openstreetmap.atlas.streaming.resource.StringResource)

Example 2 with Resource

use of org.openstreetmap.atlas.streaming.resource.Resource in project atlas-checks by osmlab.

the class CheckFlagFileProcessorTest method processCompleteAndValidate.

private void processCompleteAndValidate(final int eventCount) {
    // Generate
    final File tempDirectory = Files.createTempDir();
    final CheckFlagFileProcessor processor = new CheckFlagFileProcessor(new SparkFileHelper(FILE_SYSTEM_CONFIG), tempDirectory.getAbsolutePath());
    for (int index = 0; index < eventCount; index++) {
        processor.process(SAMPLE_EVENT);
    }
    processor.process(new ShutdownEvent());
    // Validate
    final List<Resource> files = FileSystemHelper.resources(tempDirectory.getAbsolutePath(), FILE_SYSTEM_CONFIG);
    Assert.assertEquals((int) Math.floor(eventCount / (double) BATCH_SIZE) + 1, files.size());
    int actualEventCount = 0;
    for (final Resource file : files) {
        for (final String line : file.lines()) {
            actualEventCount++;
            compareJsonAndEventObject(line, SAMPLE_EVENT);
        }
    }
    Assert.assertEquals(eventCount, actualEventCount);
    // Cleanup
    tempDirectory.delete();
}
Also used : Resource(org.openstreetmap.atlas.streaming.resource.Resource) SparkFileHelper(org.openstreetmap.atlas.generator.tools.spark.utilities.SparkFileHelper) File(java.io.File)

Example 3 with Resource

use of org.openstreetmap.atlas.streaming.resource.Resource in project atlas-checks by osmlab.

the class CheckFlagGeoJsonProcessorTest method processCompleteAndValidate.

private void processCompleteAndValidate(final int eventCount) {
    // Generate
    final File tempDirectory = Files.createTempDir();
    final CheckFlagGeoJsonProcessor processor = new CheckFlagGeoJsonProcessor(new SparkFileHelper(FILE_SYSTEM_CONFIG), tempDirectory.getAbsolutePath()).withBatchSizeOverride(25);
    for (int index = 0; index < eventCount; index++) {
        processor.process(this.setup.getCheckFlagEvent());
    }
    processor.process(new ShutdownEvent());
    // Validate
    final List<Resource> files = FileSystemHelper.resources(tempDirectory.getAbsolutePath(), FILE_SYSTEM_CONFIG);
    Assert.assertEquals((int) Math.ceil(eventCount / (double) processor.computeBatchSize()), files.size());
    int actualEventCount = 0;
    for (final Resource file : files) {
        final JsonObject found = GSON_BUILDER.fromJson(new InputStreamReader(file.read()), JsonObject.class);
        actualEventCount += found.getAsJsonArray("features").size();
    }
    Assert.assertEquals(eventCount, actualEventCount);
    // Cleanup
    tempDirectory.delete();
}
Also used : InputStreamReader(java.io.InputStreamReader) Resource(org.openstreetmap.atlas.streaming.resource.Resource) JsonObject(com.google.gson.JsonObject) SparkFileHelper(org.openstreetmap.atlas.generator.tools.spark.utilities.SparkFileHelper) File(java.io.File)

Example 4 with Resource

use of org.openstreetmap.atlas.streaming.resource.Resource in project atlas-checks by osmlab.

the class MetricFileGeneratorTest method processCompleteAndValidate.

private void processCompleteAndValidate(final int eventCount) throws IOException {
    // Generate
    final File tempDirectory = this.tempFolder.newFolder();
    final MetricFileGenerator processor = new MetricFileGenerator("some-file-name.csv", new SparkFileHelper(FILE_SYSTEM_CONFIG), tempDirectory.getAbsolutePath());
    for (int index = 0; index < eventCount; index++) {
        processor.process(SAMPLE_EVENT);
    }
    processor.process(new ShutdownEvent());
    // Validate
    final List<Resource> files = FileSystemHelper.resources(tempDirectory.getAbsolutePath(), FILE_SYSTEM_CONFIG);
    Assert.assertEquals((int) Math.floor(eventCount / (double) BATCH_SIZE) + 1, files.size());
    int actualEventCount = 0;
    for (final Resource file : files) {
        for (final String line : file.lines()) {
            // This is first line
            if (actualEventCount == 0) {
                Assert.assertEquals(MetricEvent.header(), line);
            } else {
                Assert.assertEquals("a-metric-name,60000", line);
            }
            actualEventCount++;
        }
    }
    // Plus 1 is for the header
    Assert.assertEquals(eventCount + 1, actualEventCount);
}
Also used : Resource(org.openstreetmap.atlas.streaming.resource.Resource) SparkFileHelper(org.openstreetmap.atlas.generator.tools.spark.utilities.SparkFileHelper) File(java.io.File)

Example 5 with Resource

use of org.openstreetmap.atlas.streaming.resource.Resource in project atlas-checks by osmlab.

the class AtlasDataSource method load.

/**
 * Loads an {@link Atlas} from the input location. Intermediate {@link Atlas}es created are
 * submitted to the provided {@link Consumer} to allow for any additional handling.
 *
 * @param input
 *            location of the {@link Atlas} source
 * @param country
 *            country of the {@link Atlas}
 * @param intermediateAtlasHandler
 *            handler given intermediate {@link Atlas} files when created
 * @return {@link Atlas} representation of the data source
 */
public Atlas load(final String input, final String country, final Consumer<Atlas> intermediateAtlasHandler) {
    // Path filters for supported file types
    final PathFilter pbfFilter = new OsmPbfFilePathFilter();
    final PathFilter atlasFilter = new CountrySpecificAtlasFilePathFilter(country);
    final Optional<Resource> resource = this.loadHelper.collectSourceFile(input, pbfFilter, atlasFilter);
    if (resource.isPresent()) {
        final Resource dataSource = resource.get();
        if (Atlas.isAtlas(dataSource)) {
            return new AtlasResourceLoader().load(dataSource);
        } else if (FileSuffix.resourceFilter(FileSuffix.PBF).test(dataSource)) {
            this.logger.info("Loading Atlas from OSM protobuf {}", input);
            final Atlas atlas = this.loadPbf(dataSource, country);
            intermediateAtlasHandler.accept(atlas);
            return atlas;
        }
    } else {
        final String directory = this.pathResolver.resolvePath(input, country);
        final List<Resource> atlasResources = this.loadHelper.collectSourceFiles(directory, true, atlasFilter);
        if (atlasResources.size() > 0) {
            return new AtlasResourceLoader().load(atlasResources);
        } else {
            final List<Resource> pbfResources = this.loadHelper.collectSourceFiles(directory, true, pbfFilter);
            final int pbfCount = pbfResources.size();
            if (pbfCount > 0) {
                this.logger.info("Loading Atlas from {} OSM protobuf(s) found in {}", pbfCount, input);
                final List<Atlas> atlases = pbfResources.parallelStream().map(dataSource -> this.loadPbf(dataSource, country)).peek(intermediateAtlasHandler).collect(Collectors.toList());
                return new MultiAtlas(atlases);
            }
        }
    }
    return null;
}
Also used : AtlasResourceLoader(org.openstreetmap.atlas.geography.atlas.AtlasResourceLoader) Atlas(org.openstreetmap.atlas.geography.atlas.Atlas) MultiAtlas(org.openstreetmap.atlas.geography.atlas.multi.MultiAtlas) CountrySpecificAtlasFilePathFilter(org.openstreetmap.atlas.checks.atlas.CountrySpecificAtlasFilePathFilter) PathFilter(org.apache.hadoop.fs.PathFilter) OsmPbfFilePathFilter(org.openstreetmap.atlas.checks.atlas.OsmPbfFilePathFilter) CountrySpecificAtlasFilePathFilter(org.openstreetmap.atlas.checks.atlas.CountrySpecificAtlasFilePathFilter) MultiAtlas(org.openstreetmap.atlas.geography.atlas.multi.MultiAtlas) Resource(org.openstreetmap.atlas.streaming.resource.Resource) OsmPbfFilePathFilter(org.openstreetmap.atlas.checks.atlas.OsmPbfFilePathFilter)

Aggregations

Resource (org.openstreetmap.atlas.streaming.resource.Resource)5 File (java.io.File)3 SparkFileHelper (org.openstreetmap.atlas.generator.tools.spark.utilities.SparkFileHelper)3 JsonObject (com.google.gson.JsonObject)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 PathFilter (org.apache.hadoop.fs.PathFilter)1 HttpStatus (org.apache.http.HttpStatus)1 Assert (org.junit.Assert)1 CountrySpecificAtlasFilePathFilter (org.openstreetmap.atlas.checks.atlas.CountrySpecificAtlasFilePathFilter)1 OsmPbfFilePathFilter (org.openstreetmap.atlas.checks.atlas.OsmPbfFilePathFilter)1