Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.gobblin.converter.jdbc;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.Schema.Type;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.apache.gobblin.configuration.ConfigurationKeys;
import org.apache.gobblin.configuration.State;
import org.apache.gobblin.configuration.WorkUnitState;
import org.apache.gobblin.converter.Converter;
import org.apache.gobblin.converter.DataConversionException;
import org.apache.gobblin.converter.SchemaConversionException;
import org.apache.gobblin.converter.SingleRecordIterable;
import org.apache.gobblin.converter.initializer.AvroToJdbcEntryConverterInitializer;
import org.apache.gobblin.converter.initializer.ConverterInitializer;
import org.apache.gobblin.source.workunit.WorkUnitStream;
import org.apache.gobblin.writer.commands.JdbcWriterCommandsFactory;
/**
* Converts Avro Schema into JdbcEntrySchema
* Converts Avro GenericRecord into JdbcEntryData
* Converts Avro field name for JDBC counterpart.
*
* This converter is written based on Avro 1.7.7 specification https://avro.apache.org/docs/1.7.7/spec.html
*/
public class AvroToJdbcEntryConverter extends Converter {
public static final String CONVERTER_AVRO_JDBC_DATE_FIELDS = "converter.avro.jdbc.date_fields";
private static final String AVRO_NESTED_COLUMN_DELIMITER = ".";
private static final String JDBC_FLATTENED_COLUMN_DELIMITER = "_";
private static final String AVRO_NESTED_COLUMN_DELIMITER_REGEX_COMPATIBLE = "\\.";
private static final Splitter AVRO_RECORD_LEVEL_SPLITTER = Splitter.on(AVRO_NESTED_COLUMN_DELIMITER).omitEmptyStrings();
private static final Logger LOG = LoggerFactory.getLogger(AvroToJdbcEntryConverter.class);
private static final Map AVRO_TYPE_JDBC_TYPE_MAPPING =
ImmutableMap. builder()
.put(Type.BOOLEAN, JdbcType.BOOLEAN)
.put(Type.INT, JdbcType.INTEGER)
.put(Type.LONG, JdbcType.BIGINT)
.put(Type.FLOAT, JdbcType.FLOAT)
.put(Type.DOUBLE, JdbcType.DOUBLE)
.put(Type.STRING, JdbcType.VARCHAR)
.put(Type.ENUM, JdbcType.VARCHAR).build();
private static final Set AVRO_SUPPORTED_TYPES =
ImmutableSet. builder()
.addAll(AVRO_TYPE_JDBC_TYPE_MAPPING.keySet())
.add(Type.UNION)
.add(Type.RECORD)
.build();
private static final Set JDBC_SUPPORTED_TYPES =
ImmutableSet. builder()
.addAll(AVRO_TYPE_JDBC_TYPE_MAPPING.values())
.add(JdbcType.DATE)
.add(JdbcType.TIME)
.add(JdbcType.TIMESTAMP)
.build();
private Optional