com.datastax.driver.core.exceptions.CodecNotFoundException Java Examples

The following examples show how to use com.datastax.driver.core.exceptions.CodecNotFoundException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraAbstractDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private void registerCodecIfNotFound(CodecRegistry registry, TypeCodec<?> codec) {
    try {
        registry.codecFor(codec.getCqlType(), codec.getJavaType());
    } catch (CodecNotFoundException e) {
        registry.register(codec);
    }
}
 
Example #2
Source File: CassandraPreparedStatement.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("cast")
public void setInt(int parameterIndex, int integer) throws SQLException
   {
       checkNotClosed();
       checkIndex(parameterIndex);
       //bindValues.put(parameterIndex, JdbcInt32.instance.decompose(integer));
       try{
       	this.statement.setInt(parameterIndex-1, integer);
       }catch(CodecNotFoundException e){        	        
   		if(e.getMessage().contains("Codec not found for requested operation: [varint <-> java.lang.Integer]")){
   			this.statement.setVarint(parameterIndex-1, BigInteger.valueOf((long)integer));
   		}
   	}
       
   }
 
Example #3
Source File: CassandraTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
/**
 * Convert a Record into a fully-bound statement.
 */
@SuppressWarnings("unchecked")
private BoundStatement recordToBoundStatement(Record record) throws StageException {
  ImmutableList.Builder<Object> values = new ImmutableList.Builder<>();
  SortedSet<String> columnsPresent = Sets.newTreeSet(columnMappings.keySet());
  for (Map.Entry<String, String> mapping : columnMappings.entrySet()) {
    String columnName = mapping.getKey();
    String fieldPath = mapping.getValue();

    // If we're missing fields, skip them.
    // If a field is present, but null, also remove it from columnsPresent since we can't write nulls.
    if (!record.has(fieldPath) || record.get(fieldPath).getValue() == null) {
      columnsPresent.remove(columnName);
      continue;
    }

    final Object value = record.get(fieldPath).getValue();
    // Special cases for handling SDC Lists and Maps,
    // basically unpacking them into raw types.
    if (value instanceof List) {
      List<Object> unpackedList = new ArrayList<>();
      for (Field item : (List<Field>) value) {
        unpackedList.add(item.getValue());
      }
      values.add(unpackedList);
    } else if (value instanceof Map) {
      Map<Object, Object> unpackedMap = new HashMap<>();
      for (Map.Entry<String, Field> entry : ((Map<String, Field>) value).entrySet()) {
        unpackedMap.put(entry.getKey(), entry.getValue().getValue());
      }
      values.add(unpackedMap);
    } else {
      values.add(value);
    }
  }


  PreparedStatement stmt = statementCache.getUnchecked(columnsPresent);
  // .toArray required to pass in a list to a varargs method.
  Object[] valuesArray = values.build().toArray();
  BoundStatement boundStmt = null;
  try {
    boundStmt = stmt.bind(valuesArray);
  } catch (CodecNotFoundException | InvalidTypeException | NullPointerException e) {
    // NPE can occur if one of the values is a collection type with a null value inside it. Thus, it's a record
    // error. Note that this runs the risk of mistakenly treating a bug as a record error.
    // CodecNotFound is caused when there is no type conversion definition available from the provided type
    // to the target type.
    errorRecordHandler.onError(
        new OnRecordErrorException(
            record,
            Errors.CASSANDRA_06,
            record.getHeader().getSourceId(),
            e.toString(),
            e
        )
    );
  }
  return boundStmt;
}
 
Example #4
Source File: CassandraPreparedStatement.java    From cassandra-jdbc-wrapper with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "boxing", "unchecked" })
public final void setObject(int parameterIndex, Object object, int targetSqlType, int scaleOrLength) throws SQLException
   {
       checkNotClosed();
       checkIndex(parameterIndex);
       
       switch(targetSqlType){          
       case Types.VARCHAR:
       	this.statement.setString(parameterIndex-1, object.toString());
       	break;
       case Types.BIGINT:
       	this.statement.setLong(parameterIndex-1, Long.parseLong(object.toString()));
       	break;
       case Types.BINARY:        	
       	byte[] array = new byte[((java.io.ByteArrayInputStream)object).available()];
       	try {
			((java.io.ByteArrayInputStream)object).read(array);
		} catch (IOException e1) {
			LOG.warn("Exception while setting object of BINARY type", e1);
		}
       	this.statement.setBytes(parameterIndex-1, (ByteBuffer.wrap((array))));
       	break;        	
       case Types.BOOLEAN:
       	this.statement.setBool(parameterIndex-1, (Boolean)object);
       	break;
       case Types.CHAR:
       	this.statement.setString(parameterIndex-1, object.toString());
       	break;
       case Types.CLOB:
       	this.statement.setString(parameterIndex-1, object.toString());
       	break;          
       case Types.TIMESTAMP:
       	this.statement.setTimestamp(parameterIndex-1, (Timestamp)object);
       	break;
       case Types.DECIMAL:
       	this.statement.setDecimal(parameterIndex-1, (BigDecimal)object);
       	break;
       case Types.DOUBLE:
       	this.statement.setDouble(parameterIndex-1, (Double)object);
       	break;
       case Types.FLOAT:
       	this.statement.setFloat(parameterIndex-1, (Float)object);
       	break;
       case Types.INTEGER:
       	try{
       		this.statement.setInt(parameterIndex-1, (Integer)object);
       	}catch(CodecNotFoundException e){
       		if(e.getMessage().contains("varint")){ // sucks but works
       			this.statement.setVarint(parameterIndex-1, BigInteger.valueOf(Long.parseLong(object.toString())));
       		}
       	}
       	break;
       case Types.DATE:
       	this.statement.setTimestamp(parameterIndex-1, (Date)object);
       	break;
       case Types.ROWID:
       	this.statement.setUUID(parameterIndex-1, (java.util.UUID)object);
       	break;
       case Types.OTHER:
       	if(object.getClass().equals(com.datastax.driver.core.TupleValue.class)){
       		this.statement.setTupleValue(parameterIndex-1, (com.datastax.driver.core.TupleValue) object);
       	}
       	if(object.getClass().equals(java.util.UUID.class)){
       		this.statement.setUUID(parameterIndex-1, (java.util.UUID) object);
       	}
       	if(object.getClass().equals(java.net.InetAddress.class) || object.getClass().equals(java.net.Inet4Address.class)){
       		this.statement.setInet(parameterIndex-1, (InetAddress) object);
       	}
       	else if ( List.class.isAssignableFrom(object.getClass()))
           {
             this.statement.setList(parameterIndex-1,handleAsList(object.getClass(), object));  
           }
           else if ( Set.class.isAssignableFrom(object.getClass()))
           {
           	this.statement.setSet(parameterIndex-1,handleAsSet(object.getClass(), object)); 
           }
           else if ( Map.class.isAssignableFrom(object.getClass()))
           {
           	this.statement.setMap(parameterIndex-1,handleAsMap(object.getClass(), object));              
           } 
                   	
       	break;
       }
       
       	
       
   }