com.yahoo.bullet.dsl.converter.JSONBulletRecordConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bullet-dsl Show documentation
Show all versions of bullet-dsl Show documentation
This library provides a code-less way to get your data into Bullet without
The newest version!
/*
* Copyright 2021, Yahoo Inc.
* Licensed under the terms of the Apache License, Version 2.0.
* See the LICENSE file associated with the project for terms.
*/
package com.yahoo.bullet.dsl.converter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.yahoo.bullet.common.BulletConfig;
import com.yahoo.bullet.dsl.BulletDSLException;
import com.yahoo.bullet.record.BulletRecord;
import com.yahoo.bullet.typesystem.Type;
import com.yahoo.bullet.typesystem.TypedObject;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.function.UnaryOperator;
/**
* JSONBulletRecordConverter is used to convert String JSON to {@link BulletRecord} instances. The JSON must be an
* object containing the fields of the record.
*
* If a schema is not specified, numeric types will default to {@link Double}. If a schema is provided, the appropriate
* specified types will be used.
*/
public class JSONBulletRecordConverter extends MapBulletRecordConverter {
private static final long serialVersionUID = -9133702879277054842L;
private static final Gson GSON = new GsonBuilder().create();
/**
* Constructs a JSONBulletRecordConverter without a schema.
*
* @throws BulletDSLException if there is an error creating the converter.
*/
public JSONBulletRecordConverter() throws BulletDSLException {
super();
}
/**
* Constructs a JSONBulletRecordConverter from a given schema.
*
* @param schema A schema file that specifies the fields to extract and their types.
* @throws BulletDSLException if there is an error creating the converter.
*/
public JSONBulletRecordConverter(String schema) throws BulletDSLException {
super(schema);
}
/**
* Constructs a JSONBulletRecordConverter from a given configuration.
*
* @param bulletConfig The configuration that specifies the settings for a JSONBulletRecordConverter.
* @throws BulletDSLException if there is an error creating the converter.
*/
public JSONBulletRecordConverter(BulletConfig bulletConfig) throws BulletDSLException {
super(bulletConfig);
}
@Override
public BulletRecord convert(Object object, BulletRecord record) throws BulletDSLException {
String json = (String) object;
Map data = GSON.fromJson(json, new TypeToken