我刚刚开始探索 PlayOrm 并尝试运行网络上可用的基本代码来插入和读取数据。当我插入数据时,一切似乎都工作正常,但在读取相同的数据时,它返回的行数不正确。例如。如果我插入 3 条记录,有时会得到 0 条记录,有时会返回所有记录。
按照我正在使用的代码:
用于插入/读取数据的类:
@NoSqlEntity
@NoSqlQueries({
@NoSqlQuery(name="findByLastName", query="PARTITIONS t('lastName',:lastName) select * FROM TABLE as t"),
@NoSqlQuery(name="findByName", query="PARTITIONS e('name',:name) select * FROM TABLE as e")
})
public class User {
@NoSqlId
private String id;
@NoSqlPartitionByThisField
private String name;
@NoSqlIndexed
private int age;
@NoSqlPartitionByThisField
@NoSqlIndexed
private String lastName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/*
* Queries:
* @NoSqlQuery(name="findByLastName", query="PARTITIONS t('lastName',:lastName) select * FROM TABLE as t"),
* @NoSqlQuery(name="findByName", query="PARTITIONS e('name',:name) select * FROM TABLE as e")
*/
public static List<User> findByLastName(NoSqlEntityManager mgr, String lastName) {
Query<User> query = mgr.createNamedQuery(User.class, "findByLastName");
System.out.println("Using last name "+lastName);
query.setParameter("lastName", lastName);
return query.getResultList(0, 100);
}
public static List<User> findByName(NoSqlEntityManager mgr, String name) {
Query<User> query = mgr.createNamedQuery(User.class, "findByName");
System.out.println("Using name "+name);
query.setParameter("name", name);
return query.getResultList(0, 100);
}
}
使用此类的代码:
import java.awt.List;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import com.alvazan.orm.api.base.Bootstrap;
import com.alvazan.orm.api.base.DbTypeEnum;
import com.alvazan.orm.api.base.NoSqlEntityManager;
import com.alvazan.orm.api.base.NoSqlEntityManagerFactory;
import com.netflix.astyanax.retry.SleepingRetryPolicy;
public class Example {
private static NoSqlEntityManager mgr;
public static void main(String[] args) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(Bootstrap.AUTO_CREATE_KEY, "create");
/*
* Bootstrap.createAndAddBestCassandraConfiguration(properties, clusterName, keyspace, seeds);
* NoSqlEntityManagerFactory factory = Bootstrap.create(DbTypeEnum.CASSANDRA, properties,null,null);
*/
NoSqlEntityManagerFactory factory = Bootstrap.create(DbTypeEnum.IN_MEMORY, properties,null,null);
mgr = factory.createEntityManager();
String file_to_use = userData;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println("Starting inserts with file "+file_to_use+" at "+sdf.format(Calendar.getInstance().getTime()));
BufferedReader CSVFile = null;
try {
CSVFile = new BufferedReader(new FileReader(file_to_use));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dataRow = null;
try {
dataRow = CSVFile.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Read the first line of data.
int i = 0;
while (dataRow != null){
String[] dataArray = dataRow.split(",");
User user = new User();
user.setName(dataArray[0]);
user.setAge(Integer.parseInt(dataArray[1]));
user.setLastName(dataArray[2]);
System.out.println("Inserting Values "+dataArray[0]+" "+dataArray[1]+" "+dataArray[2]);
mgr.flush();
try {
dataRow = CSVFile.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
java.util.List<User> result;
System.out.println("#### By LastName ####");
result = User.findByLastName(mgr, "Freida");
System.out.println("Result Size: "+ result.size());
System.out.println("#### By Time ####");
result = User.findByName(mgr, "John");
System.out.println("Result Size: "+ result.size());
}
}
用户数据的内容:
John,17,Freida
Jim,18,Freida
Jam,19,Freida
Johny,20,Freida
Jen,21,Freida