Create counter column family in cassandra? - java

I am using cassandra-2.0.10 and hector api.
I have tried :
public static void createCounterColumnFamily(Keyspace keyspace, String ccfName) {
Mutator<String> mutator = HFactory.createMutator(keyspace,StringSerializer.get());
mutator.addCounter("salary", ccfName, HFactory.createCounterColumn("salary", 10L));
mutator.execute();}
But, I'm getting this exception :
Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:unconfigured columnfamily counter_column_family_1)
at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:45)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:265)
at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecuteOperation(ExecutingKeyspace.java:113)
at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:243)
at com.cassandra.practice.CounterColumnFamily.createCounterColumnFamily(CounterColumnFamily.java:18)
at com.cassandra.practice.Bootstrapper.main(Bootstrapper.java:33)
Caused by: InvalidRequestException(why:unconfigured columnfamily counter_column_family_1)
at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:20833)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:964)
at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:950)
at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:246)
at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:243)
at me.prettyprint.cassandra.service.Operation.executeAndSetResult(Operation.java:104)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:258)
... 4 more
Am I missing something ?

You need to explicitly create both the column family and the keyspace before you can insert data.
That said the Hector API was deprecated more than five years ago and the Cassandra version you are using is just as far behind. More significantly, the Thrift API has been deprecated completely for years and has already been removed from trunk in the upcoming 4.0 release.
Please switch to 3.11.4 of Cassandra available here:
http://cassandra.apache.org/download/
And to using CQL and the java driver for such available here:
https://github.com/datastax/java-driver/tree/3.x/manual
Out of curiosity, how did you come across the versions you are currently using?

Related

Couchbase Java SDK N1QL UPDATE issue

I'm experimenting with Java and Couchbase 6.0 Community edition using Java 2.7 SDK.
I'm trying to execute a simple update query from my java application the Couchbase Java 2.7 SDK:
String query ="UPDATE admin SET FIELDNAME='TEST'"
N1qlParams params = N1qlParams.build().adhoc(false);
N1qlQuery nquery = N1qlQuery.simple(query, params);
N1qlQueryResult nqr= this.rbucket.query(nquery);
And I am getting the following exception (the most meaningful part):
com.couchbase.client.core.CouchbaseException: N1qlQuery Error - {"msg":"syntax error - at UPDATE","code":3000}
The actual exception starts like this:
Exception in thread "main" com.couchbase.client.core.CouchbaseException: Error while preparing plan
Of course - this query works fine through the Couchbase web UI and I can update without problem.
Just for info: I tried escaping the single quotes, even tried setting the column to be equal to itself - same error.
Select queries are executed in a similar manner without any problem.
"admin' is not a good name for a bucket, as it is usually related to some reserved keywords, if you still want to use this name, you have to use backticks around it:
update `admin` set FIELDNAME = 'TEST'
It also might ask you to create a primary index if you don't have one yet.

Parse MongoDB query to Java [duplicate]

This question already has answers here:
MongoDB GUI client (cross-platform or Linux) [closed]
(5 answers)
Closed 3 years ago.
I need to perform the following MongoDB query in Java:
db.ventas.aggregate([
{
$sort: {"codArt._id": -1}
},
{
$group:{
_id: "$codArt._id",
denominacion: {"$first": "$codArt.denominacion"},
unidades: {"$sum": "$unidades"},
importe: {"$first": {"$multiply": [{"$sum": "$unidades"}, "$codArt.pvp"]}},
stock: {"$first": {"$subtract": ["$codArt.stock", "$unidades"]}}
}
}
])
Is there any library, which can do it?
Unfortunately, I can't install MongoDB Compass.
You could use something like Hibernate, but no automatic translation is provided... you'll need to build your model. It will be useful though if you do not want to build the inner query or need some portability.
http://hibernate.org/ogm/
There is a great tool to learn MongoDB Java Syntax and to work with MongoDB in general: Studio 3T, there you can generate a MongoDB query for the Java driver. Trial 30 days.
P.S. I'm not sure if this software requires admin rights to be installed.

Hibernate timestamp - millisecond precision

Seems like storing timestamps with millisecond precision is a know issue with hibernate.
My field in the db was initially set as timestamp(3), but I've tried datetime(3) as well...unfortunately, it didn't make any difference.
I've tried using Timestamp and Date classes, and recently I've started using joda-time library. After all those efforts, I still wasn't unable to save timestamps with millisecond accuracy.
My mapping for the class contains following property:
<property name="startTime" column="startTime" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime" length="3" precision="3" />
and I'v custom defined Dialect class
public class MySQLQustomDialect extends MySQL5InnoDBDialect{
protected void registerColumnType(int code, String name) {
if (code == Types.TIMESTAMP) {
super.registerColumnType(code, "TIMESTAMP(3)");
} else {
super.registerColumnType(code, name);
}
}
}
If I enter the data manually into db, hibernate manages to retrieve the sub second part.
Is there any way to solve this issue?
Are you, by any chance, using the MySQL Connector/J JDBC driver with MariaDB 5.5?
Connector/J usually sends the milliseconds part to the server only when it detects that the server is new enough, and this detection checks that the server version is >= 5.6.4. This obviously does not work correctly for MariaDB 5.5.x.
You can see the relevant part of Connector/J source here:
http://bazaar.launchpad.net/~mysql/connectorj/5.1/view/head:/src/com/mysql/jdbc/PreparedStatement.java#L796
Using MariaDB's own JDBC driver (MariaDB Java Client) might help (I haven't tried), but I accidentally discovered that adding useServerPrepStmts=true to the connection string makes this work with Connector/J, too.

Is there a way to config MySQL Connector J driver to insert empty string as 0 to decimal/integer field?

I have a requirement to upgrade company's db legacy system from MySQL ver 4.1 to ver 5.5 ,I currently found out that if i insert empty string to decimal/integer field via java program ,It will throw exception but if i write the same statement and insert it directly via mysql command line the record will be inserted normally(the empty field will become 0),so this lead me to think that there are some problem with jdbc driver , is driver enforce some rule upon statement before pass it to db? i really dont want to re-write the old program to support this change.
thx in advance for your answer :)
You can assign value null not empty string.
You are changing your DB version so all codes may support. So you have to change

Strings > 4000 to CLOB conversion using hibernate

i have a java class persisting a string (>4k) into a database table into a CLOB field.
If the string is less than 4k then it works.
I have the field annotated using #Lob and initially I was getting an exception due to batching not supported for streams so I made the batch size 0 in the Hibernate config which is giving the exception:
Caused by: java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:582)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1986)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:1144)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2152)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:2035)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2876)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:609)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2062)
... 36 more
I only get this issue when using the code from Grails. When I use the same code from a pure Java application then I don't get the issue. Both applications have the same hibernate config (except I need to set the batch size to 0 in Grails). Is the issue the difference in the Hibernate versions which is 3.2.6ga in Grails as far as I can see and 3.2.5ga for the java application. The Oracle driver is the same in both cases.
Any answers welcome.
Try with annotating the field with #Column(length = Integer.MAX_VALUE). This hibernate bug report mentions it helped in Derby.

Categories