Search in sources :

Example 1 with Field

use of org.apache.pulsar.client.api.schema.Field in project pulsar by apache.

the class GenericJsonRecord method getField.

@Override
public Object getField(String fieldName) {
    JsonNode fn = jn.get(fieldName);
    if (fn == null) {
        return null;
    }
    if (fn.isContainerNode()) {
        AtomicInteger idx = new AtomicInteger(0);
        List<Field> fields = Lists.newArrayList(fn.fieldNames()).stream().map(f -> new Field(f, idx.getAndIncrement())).collect(Collectors.toList());
        return new GenericJsonRecord(schemaVersion, fields, fn, schemaInfo);
    } else if (fn.isBoolean()) {
        return fn.asBoolean();
    } else if (fn.isFloatingPointNumber()) {
        return fn.asDouble();
    } else if (fn.isBigInteger()) {
        if (fn.canConvertToLong()) {
            return fn.asLong();
        } else {
            return fn.asText();
        }
    } else if (fn.isNumber()) {
        return fn.numberValue();
    } else if (fn.isBinary()) {
        try {
            return fn.binaryValue();
        } catch (IOException e) {
            return fn.asText();
        }
    } else if (isBinaryValue(fieldName)) {
        try {
            return fn.binaryValue();
        } catch (IOException e) {
            return fn.asText();
        }
    } else if (fn.isNull()) {
        return null;
    } else {
        return fn.asText();
    }
}
Also used : List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Field(org.apache.pulsar.client.api.schema.Field) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) SchemaType(org.apache.pulsar.common.schema.SchemaType) Field(org.apache.pulsar.client.api.schema.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 2 with Field

use of org.apache.pulsar.client.api.schema.Field in project pulsar by apache.

the class MultiVersionGenericJsonReader method loadReader.

@Override
protected SchemaReader<GenericRecord> loadReader(BytesSchemaVersion schemaVersion) {
    SchemaInfo schemaInfo = getSchemaInfoByVersion(schemaVersion.get());
    if (schemaInfo != null) {
        LOG.info("Load schema reader for version({}), schema is : {}", SchemaUtils.getStringSchemaVersion(schemaVersion.get()), schemaInfo.getSchemaDefinition());
        Schema readerSchema;
        if (useProvidedSchemaAsReaderSchema) {
            readerSchema = this.readerSchema;
        } else {
            readerSchema = parseAvroSchema(schemaInfo.getSchemaDefinition());
        }
        return new GenericJsonReader(schemaVersion.get(), readerSchema.getFields().stream().map(f -> new Field(f.name(), f.pos())).collect(Collectors.toList()), schemaInfo);
    } else {
        LOG.warn("No schema found for version({}), use latest schema : {}", SchemaUtils.getStringSchemaVersion(schemaVersion.get()), this.readerSchema);
        return providerSchemaReader;
    }
}
Also used : Field(org.apache.pulsar.client.api.schema.Field) Schema(org.apache.avro.Schema) SchemaUtil.parseAvroSchema(org.apache.pulsar.client.impl.schema.util.SchemaUtil.parseAvroSchema) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo)

Example 3 with Field

use of org.apache.pulsar.client.api.schema.Field in project pulsar by apache.

the class GenericJsonRecordTest method testGetNativeRecord.

@Test
public void testGetNativeRecord() throws Exception {
    byte[] json = "{\"somefield\":null}".getBytes(UTF_8);
    GenericJsonRecord record = new GenericJsonReader(Collections.singletonList(new Field("somefield", 0))).read(json, 0, json.length);
    assertEquals(SchemaType.JSON, record.getSchemaType());
    assertSame(record.getNativeObject(), record.getJsonNode());
}
Also used : Field(org.apache.pulsar.client.api.schema.Field) Test(org.testng.annotations.Test)

Example 4 with Field

use of org.apache.pulsar.client.api.schema.Field in project pulsar by apache.

the class JSONSchemaTest method testJsonGenericRecordBuilder.

@Test
public void testJsonGenericRecordBuilder() {
    JSONSchema<Seller> sellerJsonSchema = JSONSchema.of(Seller.class);
    RecordSchemaBuilder sellerSchemaBuilder = SchemaBuilder.record("seller");
    sellerSchemaBuilder.field("state").type(SchemaType.STRING);
    sellerSchemaBuilder.field("street").type(SchemaType.STRING);
    sellerSchemaBuilder.field("zipCode").type(SchemaType.INT64);
    SchemaInfo sellerSchemaInfo = sellerSchemaBuilder.build(SchemaType.JSON);
    GenericSchemaImpl sellerGenericSchema = GenericSchemaImpl.of(sellerSchemaInfo);
    JSONSchema<PC> pcJsonSchema = JSONSchema.of(PC.class);
    RecordSchemaBuilder pcSchemaBuilder = SchemaBuilder.record("pc");
    pcSchemaBuilder.field("brand").type(SchemaType.STRING);
    pcSchemaBuilder.field("model").type(SchemaType.STRING);
    pcSchemaBuilder.field("gpu").type(SchemaType.STRING);
    pcSchemaBuilder.field("year").type(SchemaType.INT64);
    pcSchemaBuilder.field("seller", sellerGenericSchema).type(SchemaType.JSON).optional();
    SchemaInfo pcGenericSchemaInfo = pcSchemaBuilder.build(SchemaType.JSON);
    GenericSchemaImpl pcGenericSchema = GenericSchemaImpl.of(pcGenericSchemaInfo);
    Seller seller = new Seller("USA", "oakstreet", 9999);
    PC pc = new PC("dell", "g3", 2020, GPU.AMD, seller);
    byte[] bytes = pcJsonSchema.encode(pc);
    Assert.assertTrue(bytes.length > 0);
    Object pc2 = pcJsonSchema.decode(bytes);
    assertEquals(pc, pc2);
    GenericRecord sellerRecord = sellerGenericSchema.newRecordBuilder().set("state", "USA").set("street", "oakstreet").set("zipCode", 9999).build();
    GenericRecord pcRecord = pcGenericSchema.newRecordBuilder().set("brand", "dell").set("model", "g3").set("year", 2020).set("gpu", GPU.AMD).set("seller", sellerRecord).build();
    byte[] bytes3 = pcGenericSchema.encode(pcRecord);
    Assert.assertTrue(bytes3.length > 0);
    GenericRecord pc3Record = pcGenericSchema.decode(bytes3);
    for (Field field : pc3Record.getFields()) {
        assertTrue(pcGenericSchema.getFields().contains(field));
    }
    assertEquals("dell", pc3Record.getField("brand"));
    assertEquals("g3", pc3Record.getField("model"));
    assertEquals(2020, pc3Record.getField("year"));
    assertEquals(GPU.AMD.toString(), pc3Record.getField("gpu"));
    GenericRecord seller3Record = (GenericRecord) pc3Record.getField("seller");
    assertEquals("USA", seller3Record.getField("state"));
    assertEquals("oakstreet", seller3Record.getField("street"));
    assertEquals(9999, seller3Record.getField("zipCode"));
    assertTrue(pc3Record instanceof GenericJsonRecord);
    Assertions.assertThatCode(() -> pc3Record.getField("I_DO_NOT_EXIST")).doesNotThrowAnyException();
}
Also used : GenericSchemaImpl(org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl) Field(org.apache.pulsar.client.api.schema.Field) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) Test(org.testng.annotations.Test)

Example 5 with Field

use of org.apache.pulsar.client.api.schema.Field in project pulsar by apache.

the class InfluxDBGenericRecordSink method buildPoint.

@Override
protected Point buildPoint(Record<GenericRecord> message) throws Exception {
    Map<String, String> tags;
    Map<String, Object> fields = Maps.newHashMap();
    GenericRecord record = message.getValue();
    Object measurementField = getFiled(record, "measurement");
    if (null == measurementField) {
        throw new SchemaSerializationException("measurement is a required field.");
    }
    String measurement = measurementField.toString();
    // Looking for tags
    Object tagsField = getFiled(record, "tags");
    if (null == tagsField) {
        tags = ImmutableMap.of();
    } else if (Map.class.isAssignableFrom(tagsField.getClass())) {
        tags = ((Map<Object, Object>) tagsField).entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().toString(), entry -> entry.getValue().toString()));
    } else {
        // Field 'tags' that is not of Map type will be ignored
        tags = ImmutableMap.of();
    }
    // Just insert the current time millis
    long timestamp = System.currentTimeMillis();
    for (Field field : record.getFields()) {
        String fieldName = field.getName();
        if (FIELDS_TO_SKIP.contains(fieldName)) {
            continue;
        }
        Object fieldValue = record.getField(field);
        if (null != fieldValue) {
            fields.put(fieldName, fieldValue);
        }
    }
    Point.Builder builder = Point.measurement(measurement).time(timestamp, TimeUnit.MILLISECONDS).tag(tags).fields(fields);
    return builder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) Set(java.util.Set) Field(org.apache.pulsar.client.api.schema.Field) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Slf4j(lombok.extern.slf4j.Slf4j) Map(java.util.Map) Point(org.influxdb.dto.Point) Record(org.apache.pulsar.functions.api.Record) Field(org.apache.pulsar.client.api.schema.Field) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) Point(org.influxdb.dto.Point) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

Field (org.apache.pulsar.client.api.schema.Field)19 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)12 Map (java.util.Map)4 Slf4j (lombok.extern.slf4j.Slf4j)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 KeyValue (org.apache.pulsar.common.schema.KeyValue)3 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)3 Test (org.testng.annotations.Test)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 IOException (java.io.IOException)2 Set (java.util.Set)2 TimeUnit (java.util.concurrent.TimeUnit)2 PulsarClient (org.apache.pulsar.client.api.PulsarClient)2 SchemaType (org.apache.pulsar.common.schema.SchemaType)2 Test (org.junit.jupiter.api.Test)2 CassandraSourceConnectorConfig (com.datastax.oss.cdc.CassandraSourceConnectorConfig)1 DataSpec.dataSpecMap (com.datastax.oss.cdc.DataSpec.dataSpecMap)1 AbstractField (com.datastax.oss.common.sink.AbstractField)1 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1