Search in sources :

Example 46 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class ValidateSegments method run.

@Override
public void run() {
    if (directories.size() != 2) {
        throw new IAE("Please provide two segment directories to compare");
    }
    final Injector injector = makeInjector();
    final IndexIO indexIO = injector.getInstance(IndexIO.class);
    try {
        String dir1 = directories.get(0);
        String dir2 = directories.get(1);
        indexIO.validateTwoSegments(new File(dir1), new File(dir2));
        log.info("Segments [%s] and [%s] are identical", dir1, dir2);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : IndexIO(io.druid.segment.IndexIO) Injector(com.google.inject.Injector) IAE(io.druid.java.util.common.IAE) File(java.io.File)

Example 47 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class DruidJsonValidator method run.

@Override
public void run() {
    File file = new File(jsonFile);
    if (!file.exists()) {
        System.out.printf("File[%s] does not exist.%n", file);
    }
    final Injector injector = makeInjector();
    final ObjectMapper jsonMapper = injector.getInstance(ObjectMapper.class);
    registerModules(jsonMapper, Iterables.concat(Initialization.getFromExtensions(injector.getInstance(ExtensionsConfig.class), DruidModule.class), Arrays.asList(new FirehoseModule(), new IndexingHadoopModule(), new IndexingServiceFirehoseModule(), new LocalDataStorageDruidModule(), new ParsersModule())));
    final ClassLoader loader;
    if (Thread.currentThread().getContextClassLoader() != null) {
        loader = Thread.currentThread().getContextClassLoader();
    } else {
        loader = DruidJsonValidator.class.getClassLoader();
    }
    if (toLogger) {
        logWriter = new NullWriter() {

            private final Logger logger = new Logger(DruidJsonValidator.class);

            @Override
            public void write(char[] cbuf, int off, int len) {
                logger.info(new String(cbuf, off, len));
            }
        };
    }
    try {
        if (type.equalsIgnoreCase("query")) {
            jsonMapper.readValue(file, Query.class);
        } else if (type.equalsIgnoreCase("hadoopConfig")) {
            jsonMapper.readValue(file, HadoopDruidIndexerConfig.class);
        } else if (type.equalsIgnoreCase("task")) {
            jsonMapper.readValue(file, Task.class);
        } else if (type.equalsIgnoreCase("parse")) {
            final StringInputRowParser parser;
            if (file.isFile()) {
                logWriter.write("loading parse spec from file '" + file + "'");
                parser = jsonMapper.readValue(file, StringInputRowParser.class);
            } else if (loader.getResource(jsonFile) != null) {
                logWriter.write("loading parse spec from resource '" + jsonFile + "'");
                parser = jsonMapper.readValue(loader.getResource(jsonFile), StringInputRowParser.class);
            } else {
                logWriter.write("cannot find proper spec from 'file'.. regarding it as a json spec");
                parser = jsonMapper.readValue(jsonFile, StringInputRowParser.class);
            }
            if (resource != null) {
                final CharSource source;
                if (new File(resource).isFile()) {
                    logWriter.write("loading data from file '" + resource + "'");
                    source = Resources.asByteSource(new File(resource).toURL()).asCharSource(Charset.forName(parser.getEncoding()));
                } else if (loader.getResource(resource) != null) {
                    logWriter.write("loading data from resource '" + resource + "'");
                    source = Resources.asByteSource(loader.getResource(resource)).asCharSource(Charset.forName(parser.getEncoding()));
                } else {
                    logWriter.write("cannot find proper data from 'resource'.. regarding it as data string");
                    source = CharSource.wrap(resource);
                }
                readData(parser, source);
            }
        } else {
            throw new UOE("Unknown type[%s]", type);
        }
    } catch (Exception e) {
        System.out.println("INVALID JSON!");
        throw Throwables.propagate(e);
    }
}
Also used : CharSource(com.google.common.io.CharSource) IndexingServiceFirehoseModule(io.druid.guice.IndexingServiceFirehoseModule) IndexingHadoopModule(io.druid.indexer.IndexingHadoopModule) LocalDataStorageDruidModule(io.druid.guice.LocalDataStorageDruidModule) UOE(io.druid.java.util.common.UOE) Logger(io.druid.java.util.common.logger.Logger) HadoopDruidIndexerConfig(io.druid.indexer.HadoopDruidIndexerConfig) NullWriter(org.apache.commons.io.output.NullWriter) IOException(java.io.IOException) FirehoseModule(io.druid.guice.FirehoseModule) IndexingServiceFirehoseModule(io.druid.guice.IndexingServiceFirehoseModule) Injector(com.google.inject.Injector) StringInputRowParser(io.druid.data.input.impl.StringInputRowParser) ParsersModule(io.druid.guice.ParsersModule) ExtensionsConfig(io.druid.guice.ExtensionsConfig) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 48 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class BaseJettyTest method setup.

@Before
public void setup() throws Exception {
    setProperties();
    Injector injector = setupInjector();
    final DruidNode node = injector.getInstance(Key.get(DruidNode.class, Self.class));
    port = node.getPort();
    lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    ClientHolder holder = injector.getInstance(ClientHolder.class);
    server = injector.getInstance(Server.class);
    client = holder.getClient();
}
Also used : Server(org.eclipse.jetty.server.Server) Injector(com.google.inject.Injector) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) DruidNode(io.druid.server.DruidNode) Self(io.druid.guice.annotations.Self) Before(org.junit.Before)

Example 49 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class MetricsModuleTest method testSimpleInjectionWithValues.

@Test
public void testSimpleInjectionWithValues() {
    final String dataSource = "some datasource";
    final String taskId = "some task";
    final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {

        @Override
        public void configure(Binder binder) {
            JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null));
            binder.bind(Key.get(String.class, Names.named(DataSourceTaskIdHolder.DATA_SOURCE_BINDING))).toInstance(dataSource);
            binder.bind(Key.get(String.class, Names.named(DataSourceTaskIdHolder.TASK_ID_BINDING))).toInstance(taskId);
        }
    }));
    final DataSourceTaskIdHolder dimensionIdHolder = new DataSourceTaskIdHolder();
    injector.injectMembers(dimensionIdHolder);
    Assert.assertEquals(dataSource, dimensionIdHolder.getDataSource());
    Assert.assertEquals(taskId, dimensionIdHolder.getTaskId());
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) DruidNode(io.druid.server.DruidNode) Self(io.druid.guice.annotations.Self) Module(com.google.inject.Module) Test(org.junit.Test)

Example 50 with Injector

use of com.google.inject.Injector in project druid by druid-io.

the class CalciteTests method createOperatorTable.

public static DruidOperatorTable createOperatorTable() {
    try {
        final Injector injector = Guice.createInjector(new Module() {

            @Override
            public void configure(final Binder binder) {
                // This Module is just to get a LookupReferencesManager with a usable "lookyloo" lookup.
                final LookupReferencesManager mock = EasyMock.createMock(LookupReferencesManager.class);
                EasyMock.expect(mock.get(EasyMock.eq("lookyloo"))).andReturn(new LookupExtractorFactory() {

                    @Override
                    public boolean start() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean close() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean replaces(@Nullable final LookupExtractorFactory other) {
                        throw new UnsupportedOperationException();
                    }

                    @Nullable
                    @Override
                    public LookupIntrospectHandler getIntrospectHandler() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public LookupExtractor get() {
                        return new MapLookupExtractor(ImmutableMap.of("a", "xa", "abc", "xabc"), false);
                    }
                }).anyTimes();
                EasyMock.replay(mock);
                binder.bind(LookupReferencesManager.class).toInstance(mock);
            }
        });
        final Set<SqlAggregator> aggregators = new HashSet<>();
        final Set<SqlExtractionOperator> extractionOperators = new HashSet<>();
        for (Class<? extends SqlAggregator> clazz : SqlModule.DEFAULT_AGGREGATOR_CLASSES) {
            aggregators.add(injector.getInstance(clazz));
        }
        for (Class<? extends SqlExtractionOperator> clazz : SqlModule.DEFAULT_EXTRACTION_OPERATOR_CLASSES) {
            extractionOperators.add(injector.getInstance(clazz));
        }
        return new DruidOperatorTable(aggregators, extractionOperators);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : LookupExtractorFactory(io.druid.query.lookup.LookupExtractorFactory) LookupReferencesManager(io.druid.query.lookup.LookupReferencesManager) DruidOperatorTable(io.druid.sql.calcite.planner.DruidOperatorTable) LookupExtractor(io.druid.query.lookup.LookupExtractor) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) LookupIntrospectHandler(io.druid.query.lookup.LookupIntrospectHandler) Binder(com.google.inject.Binder) SqlExtractionOperator(io.druid.sql.calcite.expression.SqlExtractionOperator) Injector(com.google.inject.Injector) SqlAggregator(io.druid.sql.calcite.aggregation.SqlAggregator) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) Module(com.google.inject.Module) SqlModule(io.druid.sql.guice.SqlModule) Nullable(javax.annotation.Nullable) HashSet(java.util.HashSet)

Aggregations

Injector (com.google.inject.Injector)2159 AbstractModule (com.google.inject.AbstractModule)623 Test (org.junit.Test)513 Module (com.google.inject.Module)364 Test (org.testng.annotations.Test)131 Before (org.junit.Before)116 Binder (com.google.inject.Binder)114 Properties (java.util.Properties)110 Key (com.google.inject.Key)84 Map (java.util.Map)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)78 Provider (com.google.inject.Provider)74 IOException (java.io.IOException)71 TypeLiteral (com.google.inject.TypeLiteral)70 Set (java.util.Set)64 BeforeClass (org.junit.BeforeClass)61 File (java.io.File)60 CConfiguration (co.cask.cdap.common.conf.CConfiguration)55 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)55 PrivateModule (com.google.inject.PrivateModule)55