
com.stratio.connector.cassandra.utils.CassandraMetadataHelper Maven / Gradle / Ivy
The newest version!
/*
* Licensed to STRATIO (C) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. The STRATIO (C) 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 com.stratio.connector.cassandra.utils;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import com.datastax.driver.core.DataType;
import com.stratio.crossdata.common.metadata.ColumnType;
/**
* Cassandra mapping of META column types to their underlying Java
* class implementations. The following conversions are considered:
*
* Cassandra Type Cassandra Java class META Column Type
* BIGINT Long BIGINT
* BOOLEAN Boolean BOOLEAN
* COUNTER Long NATIVE
* DOUBLE Double DOUBLE
* FLOAT Float FLOAT
* INT Integer INT
* TEXT String TEXT
* VARCHAR String VARCHAR
* TEXT String TEXT
*
*/
public class CassandraMetadataHelper {
/**
* Class logger.
*/
private static final Logger LOG = Logger.getLogger(CassandraMetadataHelper.class.getName());
/**
* Mapping of native datatypes to SQL types.
*/
private static Map nativeODBCType = new HashMap<>();
private static Map dbType = new HashMap<>();
private static Map> dbClass = new HashMap<>();
static {
dbClass.put(ColumnType.BIGINT, Long.class);
dbClass.put(ColumnType.BOOLEAN, Boolean.class);
dbClass.put(ColumnType.DOUBLE, Double.class);
dbClass.put(ColumnType.FLOAT, Float.class);
dbClass.put(ColumnType.INT, Integer.class);
dbClass.put(ColumnType.TEXT, String.class);
dbClass.put(ColumnType.VARCHAR, String.class);
dbType.put(ColumnType.BIGINT, "BIGINT");
dbType.put(ColumnType.BOOLEAN, "BOOLEAN");
dbType.put(ColumnType.DOUBLE, "DOUBLE");
dbType.put(ColumnType.FLOAT, "FLOAT");
dbType.put(ColumnType.INT, "INT");
dbType.put(ColumnType.TEXT, "TEXT");
dbType.put(ColumnType.VARCHAR, "VARCHAR");
nativeODBCType.put(DataType.Name.COUNTER, "SQL_INTEGER");
}
/**
* Mapping between Cassandra datatypes and META datatypes.
*/
private static Map typeMapping = new HashMap<>();
/**
* Class constructor.
*/
public CassandraMetadataHelper() {
for (Map.Entry entry : dbType.entrySet()) {
typeMapping.put(entry.getValue(), entry.getKey());
}
}
/**
* Transform a Cassandra type to Crossdata type.
* @param dbTypeName The Cassandra column type.
* @return The Crossdata ColumnType.
*/
public ColumnType toColumnType(String dbTypeName) {
ColumnType result = typeMapping.get(dbTypeName.toUpperCase());
if (result == null) {
try {
DataType.Name cassandraType = DataType.Name.valueOf(dbTypeName.toUpperCase());
result = ColumnType.NATIVE;
result.setDBMapping(cassandraType.name(), cassandraType.asJavaClass());
result.setODBCType(nativeODBCType.get(cassandraType));
} catch (IllegalArgumentException iae) {
LOG.error("Invalid database type: " + dbTypeName, iae);
result = null;
}
} else {
result.setDBMapping(dbType.get(result), dbClass.get(result));
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy