org.apache.beam.sdk.io.astra.db.mapping.BeamRowDbMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of beam-sdks-java-io-astra Show documentation
Show all versions of beam-sdks-java-io-astra Show documentation
Apache Beam SDK to work with Astra Pipelines
The newest version!
package org.apache.beam.sdk.io.astra.db.mapping;
/*-
* #%L
* Beam SDK for Astra
* --
* Copyright (C) 2023 DataStax
* --
* Licensed under the Apache License, Version 2.0
* 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.
* #L%
*/
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ColumnDefinition;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import com.datastax.oss.driver.api.core.metadata.schema.TableMetadata;
import com.datastax.oss.driver.api.core.type.CqlVectorType;
import com.datastax.oss.driver.api.core.type.CustomType;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.ListType;
import com.datastax.oss.driver.api.core.type.MapType;
import com.datastax.oss.driver.api.core.type.SetType;
import com.datastax.oss.driver.api.core.type.TupleType;
import com.datastax.oss.driver.api.core.type.UserDefinedType;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import org.apache.beam.sdk.schemas.Schema;
import org.apache.beam.sdk.schemas.Schema.Field;
import org.apache.beam.sdk.schemas.Schema.FieldType;
import org.apache.beam.sdk.values.Row;
import org.joda.time.DateTime;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* Mapping From Cassandra Row to Beam Row.
*/
public class BeamRowDbMapper implements AstraDbMapper, Serializable {
/**
* Cassandra Session.
*/
private final CqlSession session;
/**
* Current table.
*/
private final String table;
/**
* Current keyspace.
*/
private final String keyspace;
/**
* Columns of current table
*/
private final TableMetadata tableMetadata;
/**
* What is the primary key
*/
private final Set primaryKeysColumnNames;
/**
* List of columns of the current table.
*/
private List columnDefinitions;
/**
* Associated bean schema
*/
private Schema beamSchema;
/**
* Constructor used by AstraDbRowMapperFactory.
*
* @see BeamRowDbMapperFactoryFn
* @see Row
*
* @param session the Cassandra session.
* @param keyspace the Cassandra keyspace to read data from.
* @param table the Cassandra table to read from.
*/
public BeamRowDbMapper(CqlSession session, String keyspace, String table) {
this.session = session;
this.keyspace = keyspace;
this.table = table;
if (session.isClosed()) {
throw new IllegalStateException("Session is already closed");
}
KeyspaceMetadata keyspaceMetaData = session
.getMetadata().getKeyspace(keyspace)
.orElseThrow(()-> new IllegalStateException("Keyspace "
+ keyspace + " does not exist"));
this.tableMetadata = keyspaceMetaData
.getTable(table)
.orElseThrow(() -> new IllegalStateException("Table "
+ table + " does not exist for keyspace "
+ keyspace + ""));
this.primaryKeysColumnNames = tableMetadata.getPartitionKey().stream()
.map(ColumnMetadata::getName)
.map(id -> id.asCql(true))
.collect(Collectors.toSet());
}
@Override
public Row mapRow(com.datastax.oss.driver.api.core.cql.Row cassandraRow) {
List