org.apache.avro.io.JsonDecoder Java Examples

The following examples show how to use org.apache.avro.io.JsonDecoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: YarnClusterSubmissionFromCS.java    From reef with Apache License 2.0 6 votes vote down vote up
static YarnClusterSubmissionFromCS readYarnClusterSubmissionFromCSFromInputStream(
    final InputStream appInputStream, final InputStream jobInputStream) throws IOException {
  final JsonDecoder appDecoder = DecoderFactory.get().jsonDecoder(
      AvroYarnAppSubmissionParameters.getClassSchema(), appInputStream);
  final SpecificDatumReader<AvroYarnAppSubmissionParameters> appReader = new SpecificDatumReader<>(
      AvroYarnAppSubmissionParameters.class);
  final AvroYarnAppSubmissionParameters yarnClusterAppSubmissionParameters = appReader.read(null, appDecoder);

  final JsonDecoder jobDecoder = DecoderFactory.get().jsonDecoder(
      AvroYarnClusterJobSubmissionParameters.getClassSchema(), jobInputStream);
  final SpecificDatumReader<AvroYarnClusterJobSubmissionParameters> jobReader = new SpecificDatumReader<>(
      AvroYarnClusterJobSubmissionParameters.class);
  final AvroYarnClusterJobSubmissionParameters yarnClusterJobSubmissionParameters = jobReader.read(null, jobDecoder);

  return new YarnClusterSubmissionFromCS(yarnClusterAppSubmissionParameters, yarnClusterJobSubmissionParameters);
}
 
Example #2
Source File: AvroMultiRuntimeAppSubmissionParametersSerializer.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Reads avro object from input stream.
 *
 * @param inputStream The input stream to read from
 * @return Avro object
 * @throws IOException
 */
AvroMultiRuntimeAppSubmissionParameters fromInputStream(final InputStream inputStream) throws IOException {
  final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(
          AvroMultiRuntimeAppSubmissionParameters.getClassSchema(), inputStream);
  final SpecificDatumReader<AvroMultiRuntimeAppSubmissionParameters> reader = new SpecificDatumReader<>(
          AvroMultiRuntimeAppSubmissionParameters.class);
  return reader.read(null, decoder);
}
 
Example #3
Source File: YarnBootstrapDriverConfigGenerator.java    From reef with Apache License 2.0 5 votes vote down vote up
static AvroYarnJobSubmissionParameters readYarnJobSubmissionParametersFromInputStream(
    final InputStream inputStream) throws IOException {
  final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(
      AvroYarnJobSubmissionParameters.getClassSchema(), inputStream);
  final SpecificDatumReader<AvroYarnJobSubmissionParameters> reader = new SpecificDatumReader<>(
      AvroYarnJobSubmissionParameters.class);
  return reader.read(null, decoder);
}
 
Example #4
Source File: YarnBootstrapDriverConfigGenerator.java    From reef with Apache License 2.0 5 votes vote down vote up
static AvroYarnAppSubmissionParameters readYarnAppSubmissionParametersFromInputStream(
    final InputStream inputStream) throws IOException {
  final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(
      AvroYarnAppSubmissionParameters.getClassSchema(), inputStream);
  final SpecificDatumReader<AvroYarnAppSubmissionParameters> reader = new SpecificDatumReader<>(
      AvroYarnAppSubmissionParameters.class);
  return reader.read(null, decoder);
}
 
Example #5
Source File: AzureBatchBootstrapREEFLauncher.java    From reef with Apache License 2.0 5 votes vote down vote up
private static AvroAzureBatchJobSubmissionParameters readAvroJobSubmissionParameters(
    final File paramsFile) throws IOException {
  final AvroAzureBatchJobSubmissionParameters avroAzureBatchJobSubmissionParameters;
  try (FileInputStream fileInputStream = new FileInputStream(paramsFile)) {
    final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(
        AvroAzureBatchJobSubmissionParameters.getClassSchema(), fileInputStream);
    final SpecificDatumReader<AvroAzureBatchJobSubmissionParameters> reader =
        new SpecificDatumReader<>(AvroAzureBatchJobSubmissionParameters.class);
    avroAzureBatchJobSubmissionParameters = reader.read(null, decoder);
  }
  return avroAzureBatchJobSubmissionParameters;
}
 
Example #6
Source File: AvroYarnJobSubmissionParametersSerializer.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Reads avro object from input stream.
 *
 * @param inputStream The input stream to read from
 * @return Avro object
 * @throws IOException
 */
AvroYarnJobSubmissionParameters fromInputStream(final InputStream inputStream) throws IOException {
  final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(
          AvroYarnJobSubmissionParameters.getClassSchema(), inputStream);
  final SpecificDatumReader<AvroYarnJobSubmissionParameters> reader = new SpecificDatumReader<>(
          AvroYarnJobSubmissionParameters.class);
  return reader.read(null, decoder);
}
 
Example #7
Source File: JHFMRVer2Parser.java    From eagle with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings( {"rawtypes", "deprecation"})
@Override
public void parse(InputStream is) throws Exception {
    int eventCtr = 0;
    try {
        final long start = System.currentTimeMillis();
        DataInputStream in = new DataInputStream(is);
        String version = in.readLine();
        if (!"Avro-Json".equals(version)) {
            throw new IOException("Incompatible event log version: " + version);
        }

        Schema schema = Schema.parse(in.readLine());
        SpecificDatumReader datumReader = new SpecificDatumReader(schema);
        JsonDecoder decoder = DecoderFactory.get().jsonDecoder(schema, in);

        Event wrapper;
        while ((wrapper = getNextEvent(datumReader, decoder)) != null) {
            ++eventCtr;
            reader.handleEvent(wrapper);
        }
        reader.parseConfiguration();
        // don't need put to finally as it's a kind of flushing data
        reader.close();
        logger.info("reader used " + (System.currentTimeMillis() - start) + "ms");
    } catch (Exception ioe) {
        logger.error("Caught exception parsing history file after " + eventCtr + " events", ioe);
        throw ioe;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
 
Example #8
Source File: HoodieAvroUtils.java    From hudi with Apache License 2.0 5 votes vote down vote up
/**
 * Convert json bytes back into avro record.
 */
public static GenericRecord jsonBytesToAvro(byte[] bytes, Schema schema) throws IOException {
  ByteArrayInputStream bio = new ByteArrayInputStream(bytes);
  JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, bio);
  GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
  return reader.read(null, jsonDecoder);
}
 
Example #9
Source File: LegacyAvroSchemaUtil.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
static GenericRecord deserializeJson(LegacyAvroSchema schema, String json) {
  String transformedJson = json;

  //if we had to apply any transforms to the schema to fix it (like escape illegal chars in identifiers)
  //we need to apply those same transformations to the json object to make it readable.
  for (SchemaTransformStep transform : schema.getTransforms()) {
    transformedJson = transform.applyToJsonObject(transformedJson);
  }

  List<PayloadTransformStep> stepsTaken = new ArrayList<>(1);
  while (true) {
    try {
      JsonDecoder decoder = AvroCompatibilityHelper.newJsonDecoder(schema.getFixedSchema(), transformedJson);
      DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema.getFixedSchema());
      return reader.read(null, decoder);
    } catch (Exception issue) {
      if (stepsTaken.size() > MAX_STEPS_ATTEMPTED) {
        throw new IllegalArgumentException("unable to deserialize json");
      }

      PayloadTransformStep step = findFixFor(schema, issue);
      if (step == null) {
        //if we got here we have no idea what the issue is nor how to fix it
        throw new IllegalStateException("unhandled", issue);
      }
      String fixedJson = step.applyToJsonPayload(transformedJson);
      if (fixedJson.equals(transformedJson)) {
        throw new IllegalStateException("made no progress fixing json");
      }
      transformedJson = fixedJson;
      stepsTaken.add(step);
    }
  }
}
 
Example #10
Source File: Avro17Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #11
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static JsonDecoder newJsonDecoder(Schema schema, String input) throws IOException {
  return FACTORY.newJsonDecoder(schema, input);
}
 
Example #12
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static JsonDecoder newJsonDecoder(Schema schema, InputStream input) throws IOException {
  return FACTORY.newJsonDecoder(schema, input);
}
 
Example #13
Source File: Poller.java    From spanner-event-exporter with Apache License 2.0 4 votes vote down vote up
private String getLastProcessedTimestamp() {

    String timestamp = "";
    try {
      final SubscriberStubSettings subscriberStubSettings =
          SubscriberStubSettings.newBuilder()
              .setTransportChannelProvider(
                  SubscriberStubSettings.defaultGrpcTransportProviderBuilder()
                      .setMaxInboundMessageSize(20 << 20) // 20MB
                      .build())
              .build();

      try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {
        final String subscriptionName = ProjectSubscriptionName.format(PROJECT_ID, tableName);
        final PullRequest pullRequest =
            PullRequest.newBuilder()
                .setMaxMessages(1)
                .setReturnImmediately(true)
                .setSubscription(subscriptionName)
                .build();

        final PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);
        final DatumReader<GenericRecord> datumReader =
            new GenericDatumReader<GenericRecord>(avroSchema);

        for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) {
          final JsonDecoder decoder =
              DecoderFactory.get()
                  .jsonDecoder(avroSchema, message.getMessage().getData().newInput());

          final GenericRecord record = datumReader.read(null, decoder);
          timestamp = record.get("Timestamp").toString();

          log.debug("---------------- Got Timestamp: " + timestamp);
        }
      }
    } catch (IOException e) {
      log.error("Could not get last processed timestamp from pub / sub", e);

      // If we cannot find a previously processed timestamp, we will default
      // to the one present in the config file.
      return startingTimestamp;
    }

    return timestamp;
  }
 
Example #14
Source File: Avro17Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #15
Source File: Avro14Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException {
  return new JsonDecoder(schema, in);
}
 
Example #16
Source File: Avro14Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException {
  return new JsonDecoder(schema, in);
}
 
Example #17
Source File: Avro19Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #18
Source File: Avro19Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #19
Source File: Avro18Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #20
Source File: Avro18Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #21
Source File: Avro15Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #22
Source File: Avro15Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #23
Source File: Avro16Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #24
Source File: Avro16Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException {
  return DecoderFactory.get().jsonDecoder(schema, in);
}
 
Example #25
Source File: AvroUtils.java    From brooklin with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Decode and deserialize the Json byte array into an instance of an Avro record
 * @param schema schema describing the expected information of the bytes.
 * @param bytes Json string in bytes to decode
 * @return decoded instance of GenericRecord
 */
public static <T> T decodeJsonAsAvroGenericRecord(Schema schema, byte[] bytes, T reuse) throws IOException {
  JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, new String(bytes, StandardCharsets.UTF_8));
  GenericDatumReader<T> reader = new GenericDatumReader<>(schema);
  return reader.read(reuse, jsonDecoder);
}
 
Example #26
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * constructs a {@link JsonDecoder} on top of the given {@link String} for the given {@link Schema}
 * @param schema a schema
 * @param in a String containing a json-serialized avro payload
 * @return a decoder
 * @throws IOException on io errors
 */
public static JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException {
  assertAvroAvailable();
  return ADAPTER.newJsonDecoder(schema, in);
}
 
Example #27
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * constructs a {@link JsonDecoder} on top of the given {@link InputStream} for the given {@link Schema}
 * @param schema a schema
 * @param in an input stream
 * @return a decoder
 * @throws IOException on io errors
 */
public static JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException {
  assertAvroAvailable();
  return ADAPTER.newJsonDecoder(schema, in);
}
 
Example #28
Source File: AvroAdapter.java    From avro-util with BSD 2-Clause "Simplified" License votes vote down vote up
JsonDecoder newJsonDecoder(Schema schema, InputStream in) throws IOException; 
Example #29
Source File: AvroAdapter.java    From avro-util with BSD 2-Clause "Simplified" License votes vote down vote up
JsonDecoder newJsonDecoder(Schema schema, String in) throws IOException;