Search in sources :

Example 1 with VersionedSchema

use of com.linkedin.databus2.schemas.VersionedSchema in project databus by linkedin.

the class DevRelayConfigGenerator method generateRelayConfig.

public static void generateRelayConfig(String schemaRegistryLocation, String dbName, String uri, String outputDir, List<String> srcNames, SchemaMetaDataManager manager) throws Exception {
    PhysicalSourceConfig config = new PhysicalSourceConfig();
    FileSystemSchemaRegistryService s = FileSystemSchemaRegistryService.build(new FileSystemSchemaRegistryService.StaticConfig(new File(schemaRegistryLocation), 0, false, false));
    dbName = dbName.trim().toLowerCase();
    config.setName(dbName);
    config.setUri(uri);
    for (String srcName : srcNames) {
        VersionedSchema schema = null;
        schema = s.fetchLatestVersionedSchemaBySourceName(srcName);
        String dbObjectName = SchemaHelper.getMetaField(schema.getSchema(), "dbFieldName");
        LogicalSourceConfig c = new LogicalSourceConfig();
        c.setId(manager.getSrcId(srcName));
        c.setName(srcName);
        c.setUri(dbName + "." + dbObjectName);
        c.setPartitionFunction("constant:1");
        config.addSource(c);
    }
    DatabusRelaySourcesInFiles relaySourcesInFiles = new DatabusRelaySourcesInFiles(outputDir);
    relaySourcesInFiles.add(dbName, config);
    boolean success = relaySourcesInFiles.save();
    if (!success)
        throw new RuntimeException("Unable to create the dev relay config for DB :" + dbName);
}
Also used : PhysicalSourceConfig(com.linkedin.databus2.relay.config.PhysicalSourceConfig) LogicalSourceConfig(com.linkedin.databus2.relay.config.LogicalSourceConfig) DatabusRelaySourcesInFiles(com.linkedin.databus2.relay.config.DatabusRelaySourcesInFiles) FileSystemSchemaRegistryService(com.linkedin.databus2.schemas.FileSystemSchemaRegistryService) File(java.io.File) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 2 with VersionedSchema

use of com.linkedin.databus2.schemas.VersionedSchema in project databus by linkedin.

the class GenericRecordDtailPrinter method printEvent.

/**
   * @see com.linkedin.databus2.tools.dtail.DtailPrinter#printEvent(com.linkedin.databus.core.DbusEventInternalReadable, com.linkedin.databus.client.pub.DbusEventDecoder)
   */
@Override
public ConsumerCallbackResult printEvent(DbusEventInternalReadable e, DbusEventDecoder eventDecoder) {
    DbusEventAvroDecoder avroDecoder = (DbusEventAvroDecoder) eventDecoder;
    switch(_metadataOutput) {
        case NONE:
            GenericRecord payload = eventDecoder.getGenericRecord(e, null);
            return payload != null ? printGenericRecord(payload) : ConsumerCallbackResult.SUCCESS;
        case ONLY:
            GenericRecord metadata = avroDecoder.getMetadata(e, null);
            return null != metadata ? printGenericRecord(metadata) : ConsumerCallbackResult.SUCCESS;
        case INCLUDE:
            GenericRecord payload1 = avroDecoder.getGenericRecord(e, null);
            GenericRecord metadata1 = avroDecoder.getMetadata(e, null);
            Schema pschema = Schema.createUnion(Arrays.asList(avroDecoder.getPayloadSchema(e).getSchema(), Schema.create(Type.NULL)));
            Field pfield = new Field("payload", pschema, "payload", null);
            VersionedSchema metaschema = avroDecoder.getMetadataSchema(e);
            Schema mschema = null != metaschema ? Schema.createUnion(Arrays.asList(metaschema.getSchema(), Schema.create(Type.NULL))) : Schema.createUnion(Arrays.asList(Schema.create(Type.INT), Schema.create(Type.NULL)));
            Field mfield = new Field("metadata", mschema, "metadata", null);
            Schema combined = Schema.createRecord(Arrays.asList(pfield, mfield));
            GenericRecord r = new GenericData.Record(combined);
            r.put(0, payload1);
            r.put(1, metadata1);
            return printGenericRecord(r);
        default:
            LOG.error("unknown metadata output mode: " + _metadataOutput);
            return ConsumerCallbackResult.ERROR_FATAL;
    }
}
Also used : Field(org.apache.avro.Schema.Field) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) Schema(org.apache.avro.Schema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 3 with VersionedSchema

use of com.linkedin.databus2.schemas.VersionedSchema in project databus by linkedin.

the class RegisterRequestProcessor method getMetadataSchemas.

/**
   * Returns list of versioned metadata schemas.
   * TODO (DDSDBUS-2093):  implement this.
   */
private // IN
void getMetadataSchemas(// IN
SchemaRegistryService schemaRegistry, // OUT
ArrayList<RegisterResponseMetadataEntry> registeredMetadata) throws RequestProcessingException {
    Map<SchemaId, VersionedSchema> versionedSchemas = null;
    try {
        VersionedSchemaSet schemaSet = schemaRegistry.fetchAllMetadataSchemaVersions();
        if (schemaSet != null) {
            versionedSchemas = schemaSet.getAllVersionsWithSchemaId(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE);
        }
    } catch (DatabusException ie) {
        HttpStatisticsCollector relayStatsCollector = _relay.getHttpStatisticsCollector();
        if (relayStatsCollector != null)
            relayStatsCollector.registerInvalidRegisterCall();
        throw new RequestProcessingException(ie);
    }
    if (versionedSchemas != null && !versionedSchemas.isEmpty()) {
        for (SchemaId id : versionedSchemas.keySet()) {
            VersionedSchema entry = versionedSchemas.get(id);
            if (entry.getOrigSchemaStr() == null) {
                throw new RequestProcessingException("Null schema string for metadata version " + entry.getVersion());
            }
            registeredMetadata.add(new RegisterResponseMetadataEntry((short) entry.getVersion(), entry.getOrigSchemaStr(), id.getByteArray()));
        }
    }
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) RegisterResponseMetadataEntry(com.linkedin.databus2.core.container.request.RegisterResponseMetadataEntry) HttpStatisticsCollector(com.linkedin.databus2.core.container.monitoring.mbean.HttpStatisticsCollector) SchemaId(com.linkedin.databus2.schemas.SchemaId) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 4 with VersionedSchema

use of com.linkedin.databus2.schemas.VersionedSchema in project databus by linkedin.

the class BootstrapTableReader method init.

public static void init(String[] args) throws Exception {
    parseArgs(args);
    BootstrapSeederMain.Config bsConf = new BootstrapSeederMain.Config();
    ConfigLoader<BootstrapSeederMain.StaticConfig> configLoader = new ConfigLoader<BootstrapSeederMain.StaticConfig>("databus.reader.", bsConf);
    _bsStaticConfig = configLoader.loadConfig(_sBootstrapConfigProps);
    Config qConf = new Config();
    ConfigLoader<StaticConfig> configLoader2 = new ConfigLoader<StaticConfig>("databus.query.", qConf);
    _queryStaticConfig = configLoader2.loadConfig(_sQueryConfigProps);
    SchemaRegistryService schemaRegistry = FileSystemSchemaRegistryService.build(_bsStaticConfig.getSchemaRegistry().getFileSystem());
    LOG.info("Schema = " + schemaRegistry.fetchLatestSchemaBySourceName(_queryStaticConfig.getSourceName()));
    _schema = Schema.parse(schemaRegistry.fetchLatestSchemaBySourceName(_queryStaticConfig.getSourceName()));
    VersionedSchema vs = new VersionedSchema(_schema.getFullName(), (short) 1, _schema, null);
    VersionedSchemaSet schemaSet = new VersionedSchemaSet();
    schemaSet.add(vs);
    _decoder = new DbusEventAvroDecoder(schemaSet);
}
Also used : ConfigLoader(com.linkedin.databus.core.util.ConfigLoader) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) FileSystemSchemaRegistryService(com.linkedin.databus2.schemas.FileSystemSchemaRegistryService) SchemaRegistryService(com.linkedin.databus2.schemas.SchemaRegistryService) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 5 with VersionedSchema

use of com.linkedin.databus2.schemas.VersionedSchema in project databus by linkedin.

the class DbusEventAvroDecoder method getTypedValue.

@Override
public <T extends SpecificRecord> T getTypedValue(DbusEvent e, T reuse, Class<T> targetClass) {
    if (null == reuse) {
        try {
            reuse = targetClass.newInstance();
        } catch (InstantiationException e1) {
            LOG.error("getTypedValue class instantiation error (" + e1.getMessage() + ") for event " + e, e1);
            return null;
        } catch (IllegalAccessException e1) {
            LOG.error("getTypedValue access error (" + e1.getMessage() + ") for event " + e, e1);
            return null;
        }
    }
    byte[] md5 = new byte[16];
    e.schemaId(md5);
    SchemaId schemaId = new SchemaId(md5);
    VersionedSchema writerSchema = _schemaSet.getById(schemaId);
    if (null == writerSchema) {
        LOG.error("Unable to find schema for id " + schemaId + "; event = " + e);
        throw new DatabusRuntimeException("No schema available to decode event " + e);
    }
    ByteBuffer valueBuffer = e.value();
    byte[] valueBytes = new byte[valueBuffer.remaining()];
    valueBuffer.get(valueBytes);
    try {
        //JsonDecoder jsonDec = new JsonDecoder(sourceSchema.getSchema(),new ByteArrayInputStream(valueBytes));
        binDecoder.set(DecoderFactory.defaultFactory().createBinaryDecoder(valueBytes, binDecoder.get()));
        SpecificDatumReader<SpecificRecord> reader = new SpecificDatumReader<SpecificRecord>(writerSchema.getSchema(), reuse.getSchema());
        return targetClass.cast(reader.read(reuse, binDecoder.get()));
    } catch (IOException e1) {
        LOG.error("getTypedValue IO error (" + e1.getMessage() + ") for event " + e, e1);
    }
    return reuse;
}
Also used : SpecificRecord(org.apache.avro.specific.SpecificRecord) SchemaId(com.linkedin.databus2.schemas.SchemaId) IOException(java.io.IOException) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ByteBuffer(java.nio.ByteBuffer) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException)

Aggregations

VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)17 Schema (org.apache.avro.Schema)6 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)5 SchemaId (com.linkedin.databus2.schemas.SchemaId)4 DbusEventAvroDecoder (com.linkedin.databus.client.DbusEventAvroDecoder)3 DbusEventPart (com.linkedin.databus.core.DbusEventPart)3 DatabusException (com.linkedin.databus2.core.DatabusException)3 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 IdNamePair (com.linkedin.databus.core.util.IdNamePair)2 RegisterResponseMetadataEntry (com.linkedin.databus2.core.container.request.RegisterResponseMetadataEntry)2 FileSystemSchemaRegistryService (com.linkedin.databus2.schemas.FileSystemSchemaRegistryService)2 VersionedSchemaSet (com.linkedin.databus2.schemas.VersionedSchemaSet)2 TableMapEvent (com.google.code.or.binlog.impl.event.TableMapEvent)1 Column (com.google.code.or.common.glossary.Column)1 Row (com.google.code.or.common.glossary.Row)1 BitColumn (com.google.code.or.common.glossary.column.BitColumn)1 BlobColumn (com.google.code.or.common.glossary.column.BlobColumn)1 DateColumn (com.google.code.or.common.glossary.column.DateColumn)1