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.
/**
* Copyright 2015 Thomas Feng
*
* 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 me.tfeng.toolbox.mongodb;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.Schema.Type;
import org.apache.avro.generic.GenericData.Record;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.Decoder;
import org.apache.avro.specific.SpecificDatumReader;
import org.bson.BSONObject;
import org.bson.Document;
import org.bson.types.Binary;
import org.mortbay.util.ajax.JSON;
/**
* @author Thomas Feng ([email protected])
*/
public class RecordConverter {
public static final String MONGO_CLASS_PROPERTY = "mongo-class";
public static final String MONGO_NAME_PROPERTY = "mongo-name";
public static final String MONGO_TYPE_PROPERTY = "mongo-type";
public static Document toDocument(IndexedRecord record) {
Document document = new Document();
Schema schema = record.getSchema();
for (Field field : schema.getFields()) {
Object value = record.get(field.pos());
if (value != null) {
document.put(getFieldName(field), getDocument(field.schema(), value));
}
}
return document;
}
public static T toRecord(Class recordClass, BSONObject object) {
SpecificDatumReader reader = new SpecificDatumReader(recordClass);
try {
Decoder decoder = new DocumentDecoder(recordClass, object);
return reader.read(null, decoder);
} catch (IOException e) {
throw new RuntimeException("Unable to convert MongoDB object " + object + " into Avro record", e);
}
}
public static T toRecord(Class recordClass, Document document) {
SpecificDatumReader reader = new SpecificDatumReader(recordClass);
try {
Decoder decoder = new DocumentDecoder(recordClass, document);
return reader.read(null, decoder);
} catch (IOException e) {
throw new RuntimeException("Unable to convert MongoDB document " + document + " into Avro record", e);
}
}
public static Record toRecord(Schema schema, BSONObject object, ClassLoader classLoader) throws IOException {
GenericDatumReader reader = new GenericDatumReader<>(schema);
return reader.read(null, new DocumentDecoder(schema, object, classLoader));
}
public static Record toRecord(Schema schema, Document document, ClassLoader classLoader) throws IOException {
GenericDatumReader reader = new GenericDatumReader<>(schema);
return reader.read(null, new DocumentDecoder(schema, document, classLoader));
}
protected static String getFieldName(Field field) {
String mongoName = field.getProp(MONGO_NAME_PROPERTY);
if (mongoName != null) {
return mongoName;
} else {
return field.name();
}
}
@SuppressWarnings("unchecked")
private static Object getDocument(Schema schema, Object object) {
if (schema.getType() == Type.UNION) {
List types = schema.getTypes();
if (types.size() != 2 && types.get(0).getType() != Type.NULL && types.get(1).getType() != Type.NULL) {
throw new RuntimeException("In a union type, only null unioned with exactly one other type is supported: "
+ schema);
}
if (types.get(0).getType() == Type.NULL) {
return getDocument(types.get(1), object);
} else {
return getDocument(types.get(0), object);
}
} else if (object == null) {
return null;
} else if (object instanceof IndexedRecord) {
return toDocument((IndexedRecord) object);
} else if (object instanceof Collection) {
return getDocuments(schema, (Collection