How to retrieve persisted POJOs with Cassandra and Hector Object Mapper - jpa

I'm trying to play a little bit with the Hector Object Mapper to map pojos to/from Cassandra. Since I'm a newbie in JPA standard, I'm simply trying to persist some basic beans to Cassandra and then get them back. I'm using Hector-object-mapper 3.0-04, Hectore-core 1.0-5, Cassandra server 1.2.2.
So I save my SimpleBean pojo to cassandra with
EntityManagerImpl em = new EntityManagerImpl(keyspace, "it.nosql.tests");
SimpleBean simpleBean = new SimpleBean();
simpleBean.setCassandraId(UUID.randomUUID());
simpleBean.setInteger(1000);
simpleBean.setStr("cassandraCrossing");
em.persist(simpleBean);
and then i retrieve it specifying the UUID
SimpleBean newSB = em.find(SimpleBean.class, simpleBean.getCassandraId());
Is there a way to retrieve all the SimpleBean instances persisted to Cassandra without specifying an ID? I expected to have something similar
List<SimpleBean> simpleBeanList = em.find(SimpleBean.class).asList();
Thanks,
F.

Related

google-cloud-datastore java client: Is there a way to infer schema and/or retrieve results as Json?

I am working on datastore datasource for apache-spark based on spark datasource V2 api. I was able to implement using hard-coded single entity but couldn't generalize it. Either I need to infer entity schema and translate entity record into Spark Row or read entity record as json and let the user translate into scala product (datastore java client is REST based so the payload is being pulled as json). I could see "entity.properties" as json key-values from within IntelliJ debugger which includes everything I need (column name, value, type etc.) but I can't use entity.properties due to access restrictions. Appreciate any ideas.
fixed by switching to low level API https://github.com/GoogleCloudPlatform/google-cloud-datastore
full source for spark-datastore-connector https://github.com/sgireddy/spark-datastore-connector

How can we store BinaryDocument in springData

I am able to create BinaryDocuemnt in CouchBase dB using couch base java library.
JsonDocument doc = JsonDocument.create("document_id", JsonObject.create().put("some", "value"));
System.out.println(bucket.insert(doc));
Can we store JsonDocument using Spring-data repository method ?
Spring Data offers an abstraction over the JsonDocument, storing POJO as JSON, so one could say JsonDocument is supported indirectly. There is no support for BinaryDocument, as this is orthogonal to Spring Data's concerns.
However you can transitively access the SDK from the Spring Data Couchbase CouchbaseRepository and CouchbaseOperations:
CouchbaseRepository<String> couchRepo;
// ^ note this is the explicit couchbase interface for repositories
Bucket bucket = couchRepo.getCouchbaseOperations().getCouchbaseBucket();
//work directly with the couchbase Bucket SDK from here

Saving an Iterable of a Spring Data Cassandra entity yields exception

I have a very simple Groovy entity annotated with Spring Data Cassandra annotations. I've written a repo that is based on the CrudRepository interface. In my unit test, I'm testing the various methods in the repo, using Cassandra Unit and Spring Test. All the other methods in the interface that I'm testing seem to work fine. I can insert, delete, search etc from my application using the annotated entity. But when I try to save an Iterable of the entity through the repo, it fails with the following exception:
org.springframework.data.cassandra.mapping.VerifierMappingExceptions: java.util.ArrayList:
Cassandra entities must have the #Table, #Persistent or #PrimaryKeyClass Annotation
at org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntityMetadataVerifier.verify(BasicCassandraPersistentEntityMetadataVerifier.java:45)
at org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntity.verify(BasicCassandraPersistentEntity.java:198)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:317)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:179)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:139)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:66)
at org.springframework.data.cassandra.core.CassandraTemplate.getTableNamndraTemplate.java:217)
at org.springframework.data.cassandra.core.CassandraTemplate.doInsert(CassandraTemplate.java:641)
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:237)
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:232)
at x.y.z.repositories.UserRepositoryImpl.save(UserRepositoryImpl.java:44)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at x.y.z.repositories.UserDataRepositorySpec.verify saving a list(UserDataRepositorySpec.groovy:186)
So, saving, deleting, or modifying a single entity instance works fine, but as soon as I create a List (backed by an ArrayList), it fails with this error. The entity itself looks something like this:
#InheritConstructors
#EqualsAndHashCode
#ToString
#Table("user_data")
class UserData {
#PrimaryKey("id")
long userId
#Column
String name
}
It seems counter intuitive that I would have to create a special type of List just to save a collection of entities. Am I missing something? I'm using Spring Cassandra 1.3.4 with the DataStax driver core 2.1.9. Thanks.

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?
I am trying to use Spring-Mongodb-Data module for CRUD operations against Mongo database and going through examples and articles my assumption is that databasename should be pre-defined in spring context xml when defining MongoTemplate bean.
In my case I have an multi-tenant application that will accept requests over http and my application should create the mongodatabase on-the-fly and use the name provided in the input http request to create the database and then load the data into collection in the newly created database.
I am trying to figure out if there is a way to dynamically populate the databasename in MongoTemplate or MongoRepository without having to provide it in spring context.xml?
Please help me.
Thanks
-RK
Have you tried the following instead of going through the pre-defined spring context configuration.
MongoTemplate getMongoTemplate(Mongo mongo, String database) {
return new MongoTemplate(mongo, database);
}

Spring Data MongoDB prevent persisting certain fields

I am using Spring Data for MongoDB to persist my domain objects. I was wondering if there is a way (perhaps with an Annotation?) to prevent Spring Data from persisting certain fields into MongoDB?
Does someone know how to do that or do I have to write my own Mapper?
Thanks.
In this case use the #Transient annotation for the field you need to ignore.
Look more over here - Transient
In case you are looking for the actual package like I was, this one will work:
import org.springframework.data.annotation.Transient;
Which is from the Spring framework API documentation.
But this one, which is a JPA annotation, will not work for Spring Data's MongoDB:
import javax.persistence.Transient;
Which is part of the Java Persistence API.