public class EmployeeTest extends AbstractTest {
@Autowired
private CassandraSessionProvider cassandraSessionProvider;
@Test
public void weirdCodecIssue() throws Exception {
String clusterName = "Test Cluster";
String contactPoints = "127.0.0.1:9042,127.0.0.2:9042,127.0.0.3:9042";
String keyspace = "mykeyspace";
try (final Session session = cassandraSessionProvider.getSession(clusterName, contactPoints, keyspace)) {
session.execute("USE " + keyspace);
// create Employee table
session.execute(
"CREATE TABLE mykeyspace.my_table ("
+ " name TEXT,"
+ " password BLOB,"
+ " expires_in_days INT,"
+ " password_reset_date TIMESTAMP,"
+ " PRIMARY KEY (name)"
+ ");");
// populate employee data
String employeeName = "foo";
BoundStatement employeeStatement =
session.prepare("INSERT INTO mykeyspace.my_table (name, password, expires_in_days, password_reset_date) VALUES (?, ?, ?, ?)").bind();
employeeStatement.setString("name", employeeName);
employeeStatement.setBytes("password", ByteBuffer.wrap("123abc".getBytes("UTF-8")));
employeeStatement.setInt("expires_in_days", 30);
employeeStatement.setTimestamp("password_reset_date", new Date());
session.execute(employeeStatement);
// retrieve data
MappingManager mappingManager = new MappingManager(session);
Mapper<EmployeeEntity> employeeEntityMapper = mappingManager.mapper(EmployeeEntity.class);
Statement employeeQuery = employeeEntityMapper.getQuery(employeeName);
ResultSet resultSet = session.execute(employeeQuery);
Row employeeRow = resultSet.one();
if (employeeRow == null) {
throw new Exception();
}
byte[] employeePassword = Bytes.getArray(employeeRow.getBytes(1));
int expiresInDays = employeeRow.getInt(2);
Date passwordResetOn = employeeRow.getTimestamp(3);
System.out.println(employeePassword);
System.out.println(expiresInDays);
System.out.println(passwordResetOn);
}
}
}
com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [int <-> java.nio.ByteBuffer]
com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [timestamp <-> java.nio.ByteBuffer]
22:18:29.132 [Test Cluster-worker-3] DEBUG c.d.driver.core.ControlConnection - [Control connection] Refreshing schema for mykeyspace.my_table (TABLE)
22:18:29.139 [Native-Transport-Requests-3] DEBUG o.apache.cassandra.db.SystemKeyspace - stored prepared statement for logged keyspace 'mykeyspace': 'INSERT INTO mykeyspace.my_table (name, password, expires_in_days, password_reset_on) VALUES (?, ?, ?, ?)'22:28:37.808 [main] DEBUG com.datastax.driver.mapping.Mapper - Preparing query SELECT name AS col1,password_reset_on AS col2,password AS col3,expires_in_days AS col4 FROM mykeyspace.my_table WHERE name=?;22:28:37.822 [Native-Transport-Requests-1] DEBUG o.apache.cassandra.db.SystemKeyspace - stored prepared statement for logged keyspace 'mykeyspace': 'SELECT name AS col1,password_reset_date AS col2,password AS col3,expires_in_days AS col4 FROM mykeyspace.my_table WHERE name=?;'22:28:37.832 [main] DEBUG com.datastax.driver.core.Connection - Connection[/127.0.0.1:9142-4, inFlight=0, closed=true] closing connection22:28:37.832 [main] DEBUG com.datastax.driver.core.Host.STATES - [/127.0.0.1:9142] Connection[/127.0.0.1:9142-4, inFlight=0, closed=true] closed, remaining = 1.............................................................................................................
com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [timestamp <-> java.nio.ByteBuffer]
at com.datastax.driver.core.CodecRegistry.notFound(CodecRegistry.java:741) at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:588) at com.datastax.driver.core.CodecRegistry.access$500(CodecRegistry.java:137) at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:246) at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:232) at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524) at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2250) at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2133) at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2046) at com.google.common.cache.LocalCache.get(LocalCache.java:3963) at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3967) at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4952) at com.datastax.driver.core.CodecRegistry.lookupCodec(CodecRegistry.java:522) at com.datastax.driver.core.CodecRegistry.codecFor(CodecRegistry.java:485) at com.datastax.driver.core.CodecRegistry.codecFor(CodecRegistry.java:467) at com.datastax.driver.core.AbstractGettableByIndexData.codecFor(AbstractGettableByIndexData.java:69) at com.datastax.driver.core.AbstractGettableByIndexData.getBytes(AbstractGettableByIndexData.java:233) at com.datastax.driver.core.AbstractGettableData.getBytes(AbstractGettableData.java:26) at com.abc.provisioner.sample.EmployeeTest.weirdCodecIssue(EmployeeTest.java:61)
final byte[] employeePassword = Bytes.getArray(employeeRow.getBytes(1));
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
byte[] employeePassword = Bytes.getArray(employeeRow.getBytes("password"));
int expiresInDays = employeeRow.getInt("expires_in_days");
Date passwordResetOn = employeeRow.getTimestamp("password_reset_on");
java.lang.IllegalArgumentException: password is not a column defined in this metadata
at com.datastax.driver.core.ColumnDefinitions.getAllIdx(ColumnDefinitions.java:274) at com.datastax.driver.core.ColumnDefinitions.getFirstIdx(ColumnDefinitions.java:280) at com.datastax.driver.core.ArrayBackedRow.getIndexOf(ArrayBackedRow.java:81) at com.datastax.driver.core.AbstractGettableData.getBytes(AbstractGettableData.java:151) at com.abc.provisioner.sample.EmployeeTest.weirdCodecIssue(EmployeeTest.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
EmployeeEntity employee = employeeEntityMapper.get(employeeName);
byte[] employeePassword = Bytes.getArray(ByteBuffer.wrap(employee.getPassword()));
int expiresInDays = employee.getExpiresInDays();
Date passwordResetOn = employee.getPasswordResetOn();
com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [blob <-> [B]
at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:56) at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:25) at com.datastax.driver.mapping.DriverThrowables.propagateCause(DriverThrowables.java:41) at com.datastax.driver.mapping.Mapper.get(Mapper.java:453) at com.abc.provisioner.sample.EmployeeTest.weirdCodecIssue(EmployeeTest.java:58)
EmployeeEntity employee = employeeEntityMapper.get(employeeName);
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
Hi,
I think the problem is that you can’t assume the position of a column in the response if you didn’t make the select statement yourself. Even if you did, it’s always better to access columns by name, just in case. The fix would be:
byte[]
employeePassword =
Bytes.getArray(employeeRow.getBytes("password"));
int
expiresInDays
= employeeRow.getInt("expires_in_days");
Date
passwordResetOn = employeeRow.getTimestamp("password_reset_date");
Regards,
--
Jacques-Henri Berthemet
--
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
I see in the execution log that columns have been renamed by the mapper, it looks like you’re not using the mapper correctly, if you don’t want to use it you should make the select statement yourself:
SELECT * FROM mykeyspace.my_table WHERE name=?;
Or only use the mapper to access the data so that it takes care of column names, according to the below article:
http://techblog.poppulo.com/cassandra-object-mapping/
It should be something like:
MappingManager
mappingManager =
new
MappingManager(session);
Mapper<EmployeeEntity>
employeeEntityMapper =
mappingManager.mapper(EmployeeEntity.class);
EmployeeEntity
employee =
employeeEntityMapper.get(employeeName);
Regards,
--
Jacques-Henri Berthemet
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.