本文整理汇总了Java中org.bson.codecs.DocumentCodec类的典型用法代码示例。如果您正苦于以下问题:Java DocumentCodec类的具体用法?Java DocumentCodec怎么用?Java DocumentCodec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DocumentCodec类属于org.bson.codecs包,在下文中一共展示了DocumentCodec类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getScanStats
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
@Override
public ScanStats getScanStats() {
try{
MongoClient client = storagePlugin.getClient();
MongoDatabase db = client.getDatabase(scanSpec.getDbName());
MongoCollection<Document> collection = db.getCollection(scanSpec.getCollectionName());
long numDocs = collection.count();
float approxDiskCost = 0;
if (numDocs != 0) {
//toJson should use client's codec, otherwise toJson could fail on
// some types not known to DocumentCodec, e.g. DBRef.
final DocumentCodec codec =
new DocumentCodec(client.getMongoClientOptions().getCodecRegistry(), new BsonTypeClassMap());
String json = collection.find().first().toJson(codec);
approxDiskCost = json.getBytes().length * numDocs;
}
return new ScanStats(GroupScanProperty.EXACT_ROW_COUNT, numDocs, 1, approxDiskCost);
} catch (Exception e) {
throw new DrillRuntimeException(e.getMessage(), e);
}
}
示例2: loadResource
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
@Override
public void loadResource(Resource resource) throws IOException {
final MongoCollection<Document> collection = handler.getCollection(uri);
if (!resource.getContents().isEmpty()) {
resource.getContents().clear();
}
final String id = uri.segment(2);
final Document filter = new Document(MongoHandler.ID_FIELD, id);
final Document document = collection
.find(filter)
.limit(1)
.first();
if (document == null) {
throw new IOException("Cannot find document with " + MongoHandler.ID_FIELD + " " + id);
}
JsonWriter writer = new JsonWriter(new StringWriter());
new DocumentCodec().encode(writer, document,
EncoderContext.builder()
.isEncodingCollectibleDocument(true)
.build());
readJson(resource, writer.getWriter().toString());
}
示例3: MongoClientWrapper
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
@Inject
public MongoClientWrapper(MongoClientConfiguration configuration) throws
UnreachableMongoServerException {
try {
MongoClientOptions options = toMongoClientOptions(configuration);
ImmutableList<MongoCredential> credentials = toMongoCredentials(configuration);
testAddress(configuration.getHostAndPort(), options);
this.configuration = configuration;
this.driverClient = new com.mongodb.MongoClient(
new ServerAddress(
configuration.getHostAndPort().getHostText(),
configuration.getHostAndPort().getPort()),
credentials,
options
);
version = calculateVersion();
codecRegistry = CodecRegistries.fromCodecs(new DocumentCodec());
closed = false;
} catch (com.mongodb.MongoException ex) {
throw new UnreachableMongoServerException(configuration.getHostAndPort(), ex);
}
}
示例4: getEncoder
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
/**
* Customizations for the document.toJson output.
* <p>
* http://mongodb.github.io/mongo-java-driver/3.0/bson/codecs/
*
* @return the toJson encoder.
*/
private Encoder<Document> getEncoder() {
ArrayList<Codec<?>> codecs = new ArrayList<>();
if (config.getElastic().getDateFormat() != null) {
// Replace default DateCodec class to use the custom date formatter.
codecs.add(new CustomDateCodec(config.getElastic().getDateFormat()));
}
if (config.getElastic().getLongToString()) {
// Replace default LongCodec class
codecs.add(new CustomLongCodec());
}
if (codecs.size() > 0) {
BsonTypeClassMap bsonTypeClassMap = new BsonTypeClassMap();
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(codecs),
MongoClient.getDefaultCodecRegistry());
return new DocumentCodec(codecRegistry, bsonTypeClassMap);
} else {
return new DocumentCodec();
}
}
示例5: JsonCodec
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public JsonCodec( TypeReference<T> tr, Class<T> clazz, Function<T, String> idFunc ) {
this.clazz = clazz;
this.idFunc = idFunc;
documentCodec = new DocumentCodec();
fileReader = Binder.json.readerFor( tr );
fileWriter = Binder.json.writerFor( tr );
}
示例6: get
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
@Override
@SuppressWarnings( "unchecked" )
public <T> Codec<T> get( Class<T> clazz, final CodecRegistry registry )
{
if ( clazz.isPrimitive() )
{
clazz = Primitives.wrap( clazz );
}
if ( codecs.containsKey( clazz ) )
{
return (Codec<T>) codecs.get( clazz );
}
if ( Entity.class.isAssignableFrom( clazz ) )
{
// there are two possible class types we can get. Some are the real interfaces and the other classes are
// proxy based
Class<?> eclass = Proxy.isProxyClass( clazz ) ? clazz.getInterfaces()[0] : clazz;
return (Codec<T>) new EntityCodec( db, EntityFactory.getProperties( (Class<? extends Entity>) eclass ) );
}
if ( Document.class.isAssignableFrom( clazz ) )
{
return (Codec<T>) new DocumentCodec( registry, mapping );
}
if ( List.class.isAssignableFrom( clazz ) )
{
return (Codec<T>) new ListCodec( registry, mapping );
}
if ( clazz.isArray() )
{
return (Codec<T>) new ArrayCodec( registry, mapping );
}
return null;
}
示例7: readFrom
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public <T> T readFrom(Document document, Class<T> target) {
BsonDocument bsonDocument = document.toBsonDocument(target, CodecRegistries.fromCodecs(new DocumentCodec()));
return (T) TDocument.fromBsonDocument(bsonDocument, target).getEquivalentPOJO(this);
}
示例8: VerseCodec
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public VerseCodec() {
this.documentCodec = new DocumentCodec();
}
示例9: BookCodec
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public BookCodec() {
this.documentCodec = new DocumentCodec();
}
示例10: BibleCodec
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public BibleCodec() {
this.documentCodec = new DocumentCodec();
}
示例11: UserCodec
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public UserCodec() {
this.documentCodec = new DocumentCodec();
}
示例12: ChapterCodec
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public ChapterCodec() {
this.documentCodec = new DocumentCodec();
}
示例13: mongoDocumentToByteArray
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public static byte[] mongoDocumentToByteArray(Document mongoDocument) {
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
new DocumentCodec().encode(writer, mongoDocument, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
return outputBuffer.toByteArray();
}
示例14: byteArrayToMongoDocument
import org.bson.codecs.DocumentCodec; //导入依赖的package包/类
public static Document byteArrayToMongoDocument(byte[] byteArray) {
BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(byteArray));
return new DocumentCodec().decode(bsonReader, DecoderContext.builder().build());
}