Astyanax map data access cql3 - dictionary

I'm trying to access a map column type using Astyanax.
From the examples I've taken the following. I'm looping the rows and can get the data from all the columns except the column storename which has datatype MAP. I assume I need to loop over the elements of the map to get the data into my object but I can't figure out how to do get the map data from the column.
I'm trying column.getValue and have tried getByteBuffer but can't get either to work. I can get a column object and I see there is a ColumnMap interface but I can't figure out how to get the data into an object that extends or uses it. I also see a MapSerializer interfaces but not sure how to implement that either...
List<Store> stores = new ArrayList<Store>();
for (Row<Integer, String> row : result.getResult().getRows()) {
Store store = new Store();
ColumnList<String> columns = row.getColumns();
store.setStoreID(columns.getStringValue("store_id", null));
store.setShopName(columns.getStringValue("shopname", null));
// lost trying to get the columns Map data here
// should I loop the map or pass the data straight to my store object as a map?
// How do I access the map data in this column?
for ( Map<String, String> map : columns.getValue("storename", imap, result).getResult() )
{
store.setStoreNames( "lang", "storename");
}
stores.add(store);
}
I'm a bit confused with getValue() as it takes a Serializer as it's 2nd argument and a default as it's 3rd. How do I pass the MapSerializer in as the 2nd argument or should I even be using getValue() method? Is the default a key value from the Map? If I want to return all values in the map what then? Thanks.

In the end this worked for me. And I was able to loop through the entries in the Map using the technique provided for looping maps here.
Map<String, String> map = columns.getValue("storename",
new MapSerializer<String, String>(UTF8Type.instance, UTF8Type.instance)
, new HashMap<String ,String>());

Related

i need to retrieve last data row single data

I need to retrieve the last row one data field. id is the primary key of my table. I'm trying to retrieve my final row data using its id
public AddExpenses[] GetFinalExpense(int numberOfExpenses)
{
return Conn.Table<AddExpenses>()
.OrderByDescending(expenses => expenses.Id)
.Take(numberOfExpenses)
.ToArray();
}
In my view model I have
var finalexpense = database.GetFinalExpense(1);
this is my code. when I tried to use this final row data to retrieve single data
ExpenseLabel = "Your expense is"+finalexpense;
in here final expense it does not show properties of the table to call. I need my finalexpense property to call it does not work
Concatenating a string with an object uses the default implementation of ToString which will yield something like AddExpenses[] for you, if finalexpense has a value !=null, since it is an array.
First of all, you'll have to get the item in the array
var finalExpenses = database.GetFinalExpense(1);
var finalExpense = finalExpenses[0];
Furthermore you'll have to make sure that your object is formatted properly. You could implement your own ToString method in AddExpenses class, but the simplest way would be to use string interpolation
var formattedExpense = $"{finalExpense.Expense} ({finalExpense.Date}, {finalExpense.Category})";
ExpenseLabel = $"Your expense is {formattedExpense}";
How you build formattedExpense is up to you, take the proposed string as a starting point and adapt it to your needs.

Update operation in DynamoDB

Here is a question raised during our data structure design. Currently, we have a data as the following:
class {
private String id;
private Map<String, Object> map;
...
}
We will have an operation to add new entries to the map frequently. I am wondering whether the data needs to be fetched from DynamoDB first, add new entries to the map and do a DB update with the modified data or not? (That is how Mongo update works.) If not, the data structure design isn't good.
Yes, you need to fetch the data from DynamoDB first and then add a new entry to map and save the object.
If the object is of type List, then you can append values to the existing list.
List<String> nameList = new ArrayList<>();
nameList.add("1");
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", testId).withReturnValues(ReturnValue.ALL_NEW).withUpdateExpression("set #Names = list_append (#Names, :val1)").withNameMap(new NameMap().with("#Names", "Names"))
.withValueMap(new ValueMap().withList(":val1", nameList));
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);

Spring JDBC Dynamic Query into Map of objects

I have to dynamically execute queries which will come from database.The query has dynamic fields,which needs to be converted into map as key value pairs and send to view.For ex
one query may return only one fields and other may return more than two field of multiple rows.I have to write code in such way that it will work for n no.of fields and return it as map using spring jdbc.
Spring offers two ways to solve your problem.
Approach 1: use queryForList method from JdbcTemplate class. this will return List of Map populated by column names as key , and DB record as value. you have to manualy iterate over the list. each map object inside the list represents a single row in resultset.
example :
List<Map<String, Object>> result = jdbcTemplate.queryForList(query, new Object[]{123});
Iterator items = result.iterator();
while(items.hasNext()){
Map<String, Object> row = (Map<String, Object>) items.next();
System.out.println(row);
}
Approach 2 : this dosen't exactly match your requirements, but little faster than the first approach also more coding involved. you can use queryForRowSet method.
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(query, new Object[]{3576});
int columnCount = rowSet.getMetaData().getColumnCount();
System.out.println(columnCount);
while(rowSet.next()){
for(int id =1 ; id <= columnCount ; id ++){
System.out.println(rowSet.getString(id)) ;
// your custom logic goes here
}
}

Process data from service in flex

I have created a PHP service in Flex Mobile and binded the result with a List. I want to sort the records from the service based on some calculation on data fields. So, is it possible to perform operations on the resultset from the PHP service ?
Detailed question:
I have created the php service through the built in tool in Flash Builder 4.6. I also had bind the service with a List in my view. (Again using GUI controls). Now I want to rearrange the items in the list based on distance from the current location and coordinates from the rows returned by the service. So the approach I was thinking was to, get the content from the service. Extract lat,lon and calculate the distance. Then update the list again.
I tried the following line in a function:
getAllplacesResult.lastResult[1].lon // lon is a column in the table.
But it returns a blank. Is their a more sane or easier way to do this ?
The better way is to assign it on an Arraycollection and manipulate data from there.
<mx:ArrayCollection id="myAC" source="{ArrayUtil.toArray(myRO.getAllplacesResult.lastResult)}" />
Make sure you bind myAC to your List.
Sort it the way you want to:
var collection:ArrayCollection = new ArrayCollection();
var s:Sort = new Sort();
s.fields = [new SortField("lat"), new SortField("lon")];
s.compareFunction = myCompareFunction;
collection.sort = s;
collection.refresh();
private function myCompareFunction(a:Object, b:Object, fields:Array = null):int {
...
}
Also, AS3 is 0 based index. 1 will actually give you the second row (or throw out of range exception)

dynamically generate ComboBox name

I have a script that parses some complex XML. When the XML element is of a certain type, it generates a comboBox using the XML element's children to populate the box. I then want to check all of the values of the all the generated ComboBoxes against their correct answers (which is also info stored in the XML file). When creating the ComboBoxes, I added an "id" property. However, it seems that I cannot them use:
dynamicQuestion.id.selectedItem.labelField
to check the answers. However, I am able to get the labelField if I know the variable name used to create the ComboBox.
dynamicQuestion.selectedItem.labelField
This indicates (to me) that I need to dynamically generate the variable name as I'm creating new instances of the ComboBox. But how do I dynamically generate a variable name? If I use
var thisBox:String = "box"+boxCount;
var newBox:ComboBox = thisBox as ComboBox;
I get an implicit coercion error. I also tried changing the creation statement to a function that accepted an argument, "thisBox," but this didn't work either. Conceptually, this seems quite simple, but I'm having a hard time putting it to practice. It seems that the comboBox's id is what is generated by created the box using script (e.g., var thisBox). How do I dynamically generate this name?
Use an array as Stefan suggested. If you must use string identifiers, you can create an object and use it as an associative array.
var combos:Object = {};
var boxCount:Number = 1;
var thisBox:String = "box"+boxCount;
//you can store comboboxes in the object using the following syntax
combos[thisBox] = new ComboBox();
//or
combos.box2 = new ComboBox();
//or
combos["box3"] = new ComboBox();
trace(combos.box1.selectedItem.labelField);
trace(combos.box2.selectedItem.labelField);
trace(combos.box3.selectedItem.labelField);
Why don't you store all your dynamically created combo boxes in an array? When you want to evaluate them you iterate over the array and access selectedItem.labelField.

Resources