Search in sources :

Example 1 with OBJECT_ID

use of io.hetu.core.plugin.mongodb.ObjectIdType.OBJECT_ID in project hetu-core by openlookeng.

the class MongoSession method createTableMetadata.

private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns) throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    MongoDatabase db = client.getDatabase(schemaName);
    Document metadata = new Document(TABLE_NAME_KEY, tableName);
    ArrayList<Document> fields = new ArrayList<>();
    if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) {
        fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument());
    }
    fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList()));
    metadata.append(FIELDS_KEY, fields);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);
    schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
    schema.insertOne(metadata);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) OBJECT_ID(io.hetu.core.plugin.mongodb.ObjectIdType.OBJECT_ID) LoadingCache(com.google.common.cache.LoadingCache) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) Map(java.util.Map) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) RowFieldName(io.prestosql.spi.type.RowFieldName) Type(io.prestosql.spi.type.Type) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) ENGLISH(java.util.Locale.ENGLISH) Collectors.toSet(java.util.stream.Collectors.toSet) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) TINYINT(io.prestosql.spi.type.TinyintType.TINYINT) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) Domain(io.prestosql.spi.predicate.Domain) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) TypeSignature(io.prestosql.spi.type.TypeSignature) IntStream(java.util.stream.IntStream) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) StandardTypes(io.prestosql.spi.type.StandardTypes) Logger(io.airlift.log.Logger) Shorts(com.google.common.primitives.Shorts) TypeSignatureParameter(io.prestosql.spi.type.TypeSignatureParameter) MINUTES(java.util.concurrent.TimeUnit.MINUTES) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NamedTypeSignature(io.prestosql.spi.type.NamedTypeSignature) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) Verify.verify(com.google.common.base.Verify.verify) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Range(io.prestosql.spi.predicate.Range) Objects.requireNonNull(java.util.Objects.requireNonNull) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) Math.toIntExact(java.lang.Math.toIntExact) IntegerType(io.prestosql.spi.type.IntegerType) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) TupleDomain(io.prestosql.spi.predicate.TupleDomain) SignedBytes(com.google.common.primitives.SignedBytes) TypeManager(io.prestosql.spi.type.TypeManager) SchemaNotFoundException(io.prestosql.spi.connector.SchemaNotFoundException) IndexOptions(com.mongodb.client.model.IndexOptions) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) Primitives(com.google.common.primitives.Primitives) Collectors.toList(java.util.stream.Collectors.toList) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) SMALLINT(io.prestosql.spi.type.SmallintType.SMALLINT) MongoClient(com.mongodb.MongoClient) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) HOURS(java.util.concurrent.TimeUnit.HOURS) VisibleForTesting(com.google.common.annotations.VisibleForTesting) VarcharType(io.prestosql.spi.type.VarcharType) IndexOptions(com.mongodb.client.model.IndexOptions) ArrayList(java.util.ArrayList) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Throwables.throwIfInstanceOf (com.google.common.base.Throwables.throwIfInstanceOf)1 Verify.verify (com.google.common.base.Verify.verify)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Primitives (com.google.common.primitives.Primitives)1 Shorts (com.google.common.primitives.Shorts)1 SignedBytes (com.google.common.primitives.SignedBytes)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 MongoClient (com.mongodb.MongoClient)1 FindIterable (com.mongodb.client.FindIterable)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoCursor (com.mongodb.client.MongoCursor)1