I have created a table in my local dynamoDB. The fields are,
id (N)
name (S)
school (S)
classname (S)
Now I want to retrieve all the records for which school equals to "xschool" and print.
Tried the below code but it gives me an error in the query,
QuerySpec spec = new QuerySpec().withProjectionExpression("sid, classname, school")
.withKeyConditionExpression("school = :v_school").
withValueMap(new ValueMap().withString(":v_school", "abcschool"));
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
Any suggestions for this as I'm new to dynamoDB.
AmazonDynamoDB dynamoDBClient = createClient();
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
String tableName = "student";
Table table = dynamoDB.getTable(tableName);
Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":sc", schoolname);
ItemCollection<ScanOutcome> items = table.scan("school = :sc", // FilterExpression
"sid, school, firstname, classname", // ProjectionExpression
null, // ExpressionAttributeNames - not used in this example
expressionAttributeValues);
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
Try this, I think this will work for you
From the DynamoDB documentation:
The Query operation enables you to query a table or a secondary index. You must provide a partition key value and an equality condition.
The error message you got means that school is not the partition key of your table. To fix this you can
change the table definition so that school is the partition key or
use a scan instead of a query.
Related
Below you can find the code that I'm using to retrieve a table from a generic SQL statement (the SQL code is inputed by the user in another part of the code).
Since I'll be storing more than one table in the future, and the retrieved table will have some format functions applied to it, I'm storing the values in a:
Map<String, Map<String,List<Object>>> tables = new HashMap<String, Map<String,List<Object>>>();
The first Map is a Table, the second is a Column and finally the list hold each line of data.
The column names/order in:
Map<String, TreeMap<Integer, String>> order = new HashMap<String, TreeMap<Integer, String>>();
The Map is tha table, the TreeMap are the column number - column name
The code to create de generic table is:
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
for(int i = 1; i <= rsmd.getColumnCount(); i++){
String name = rsmd.getColumnLabel(i); //Label vs name. Label is also what's defined by the user in: "as VENDA"
answer.put(name, null);
singleOrder.put(i, name);
}
while(rs.next()){
Iterator answerIt = answer.entrySet().iterator();
while (answerIt.hasNext()) { //Get Columns in index order to compare with body HashMap (unordered)
Map.Entry columnNameValue = (Map.Entry)answerIt.next(); //key = columnName, value = List
String columnName = (String) columnNameValue.getKey();
List<Object> tmp = answer.get(columnName);
if (tmp == null) {
tmp = new ArrayList<Object>();
answer.put(columnName, tmp);
}
Object item = rs.getObject(columnName);
if(item instanceof Integer){
item = ((Integer) item).doubleValue();
} else if (item instanceof Long){
item = ((Long) item).doubleValue();
} else if (item instanceof BigDecimal){
item = ((BigDecimal) item).doubleValue();
}
tmp.add(item);
}
}
Efficiency will be key for this part of my code. How can I improve the table creation?
(I think this is a important question for Java coders, so a clear response can serve a pattern for future programmers that have the same difficulty that I'm having now)
Update 1:
Why I'm asking: I couldn't find any example of how to handle a flexible query in Java. I've created my own solution, but I fear it's some kind of monster compared to how to handle such a case properly in Java.
As example:
Lets say I'll name the result of a first query as "table1".
The query will be: Select STORE, SALES, GOAL, EXTRA from ... ;
The order variable will be like:
Map<String, TreeMap<Integer, String>> order = new HashMap<String, TreeMap<Integer, String>>();
table1,
1, "STORE"
2, "SALES"
3, "GOAL"
4, "EXTRA"
The tables variable:
Map<String, Map<String,List<Object>>> tables = new HashMap<String, Map<String,List<Object>>>();
"table1",
"STORE",
"unit1"
"unit2"
"unit3"
"unit4"
"SALES",
1312
126
1361
6823
"GOAL"
1300
160
1200
6000
"EXTRA"
"info1"
"info2"
"info3"
"info4"
Table Players:
ID | name | email | age | ...
1 | 'bob' | null | 23 | ...
This table is where instances of class Player are persisted (one row per instance, no composition etc.).
Having a Hibernate Session, how do I get the row (say with id - the PK - equal to 1) as a Java Map (key = column name, value = cell value) ?
Example usage:
Map<String,String> row = getPlayerByIdAsMap(1);
Use a query with AliasToEntityMapResultTransformer; is verbose but should works with Hibernate property definition and not with JavaBean definition (they can differ).
Map<String,Object> aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setProjection(Projections.projectionList()
.add(Projections.id().as("id"))
// Add others properties
)
.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
.uniqueResult();
A worse approch can be write a custom ResultTransformer that introspect ClassMetadata and try to extract values...
class IntrospectClassMetadata extends BasicTransformerAdapter {
PassThroughResultTransformer rt = PassThroughResultTransformer.INSTANCE;
public Object transformTuple(Object[] tuple, String[] aliases) {
final Object o = rt.transformTuple(tuple, aliases);
ClassMetadata cm = sf.getClassMetadata(o.getClass());
List<String> pns = new ArrayList<String>(Arrays.asList(cm.getPropertyNames()));
Map<String, Object> m = new HashMap<String, Object>();
for(String pn : pns) {
m.put(pn, cm.getPropertyValue(o, pn));
}
m.put(cm.getIdentifierPropertyName(), cm.getIdentifier(o));
return m;
}
}
and use
Map<String,Object> aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setResultTransformer(new IntrospectClassMetadata())
.uniqueResult();
Last chance:
Map<String,Object> map = (Map<String,Object>)s.createSQLQuery("select * from user where id = :id")
.setParameter("id",p.id)
.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
.uniqueResult();
but this doesn't map list,bags and other mapped object, but only raw column names and values...
You can use HQL and do a query for selecting the result as a new Map
select new Map(p.id as ID, p.name as name, p.email as email, p.age as age)
from Player p
It will return you a collection of maps, being each one of the maps a row in the query result.
You can use BeanUtils and do something like this:
User user = (User) session.get(User.class, userID);
Map map = BeanUtils.describe(user);
i need the hql query that should return the Map as result, I tried hql new map query but it returns the list of map like follows
Session session = sessionFactory.getCurrentSession();
String HQL_QUERY = "select new map(user.id as id, user.fullName as fullName)
from User user";
List<Map<String,String>> usersList = session.createQuery(HQL_QUERY).list();
if this is the only solution then how do i convert a list of map into a single map without looping, because if the query returns more rows then the looping take more time for convertion. Help me.
I would suggest using Criteria and then a result transformer to create a map. Have a look at this for official documentation. This gives you a clue and you can find more samples on net.
Creating a map is not the job of HQL. It's your job. Simply loop over the rows you get from the query:
String hql = "select user.id, user.fullName from User user";
List<Object[]> rows = session.createQuery(hql).list();
Map<String, String> result = new HashMap<>();
for (Object[] row : rows) {
result.put((String) row[0], (String) row[1]);
}
you can use map like below
Query query = session.createQuery("select new map(id,username) from UserDetails");
List<?> idUsernameList=query.list();
Iterator<?> iterator = idUsernameList.iterator();
Map row=null;
while(iterator.hasNext()){
row=(Map)iterator.next();
System.out.println(row);
}
Is it possible to read data from HBase based on rowKey and columnFamily. Currently I access records by rowkey by this code:
HTable table = new HTable(conf, "tablename");
Get get = new Get(rowkey.getBytes());
Result rs = table.get(get);
for (KeyValue kv : rs.raw()) {
holdvalue = new String(kv.getValue());
}
I want to add columnfamily as a filter to access specific records that belongs to that specific rowKey and columnFamily. How could I achieve this?
Thanks in advance
You can add the column family as a filter by using the addFamily method of the Get object.
HTable table = new HTable(conf, "tablename");
Get get = new Get(rowkey.getBytes());
get.addFamily(family.getBytes()); // <-----------------
Result rs = table.get(get);
for (KeyValue kv : rs.raw()) {
holdvalue = new String(kv.getValue());
}
I am trying to select a single column from a related table. I have a table (Item) with many Values. I would like to select Value.valueString.
Basically, the query is supposed to pass in a bunch of values and pull any ValueFields that contain those values. The SQL might look something like this:
select ItemValues.valueString from ItemEntity
join StockItem on ItemEntity.stockItemId = StockItem.id
join ItemValues on ItemEntity.id = ItemValues.itemId
where StockItem.vendor = vendorId
AND (ItemValues.valueString like '%test%' OR ItemValues.valueString like '%test2%'...);
Here is my code:
final CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<String> query = builder.createQuery(String.class);
final Root<ItemEntity> root = query.from(ItemEntity.class);
query.select(root.join("ItemValues").<String>get("ValueString"));
final List<Predicate> filters = new LinkedList<Predicate>();
filters.add(builder.equal(root.join("StockItem").get("id"), vendorNumber));
final List<Predicate> filterNamesCriteria = new LinkedList<Predicate>();
if (filenames.length > 0) {
for (String fileName : filenames) {
filterNamesCriteria.add(builder.like(root.join("ItemValues").<String>get("ValueString"), fileName));
}
filters.add(builder.or(filterNamesCriteria.toArray(new Predicate[0])));
}
query.where(filters.toArray(new Predicate[0]));
final TypedQuery<String> resolvedQuery = this.entityManager.createQuery(query);
return resolvedQuery.getResultList();
I want the result to return a List of Strings (valueString column), but it's not returning anything.
Am I doing something wrong? When I say "builder.createQuery(String.class)", is that correct?
I found the problem:
filters.add(builder.equal(root.join("StockItem").get("id"), vendorNumber));
I was joining based on the StockItem id and not the StockItem.itemNumber
I used two queries to solve the issue of joining the Itemvalues map (it was returning 32,000+ results)