Kundera persistence entity definition with partition key and cluster key - cassandra

I am trying to use Kundera for connecting to Cassandra. In my case I have an Entity X with Partition Key {A,B} and cluster key {C}.
We have multiple values of C for each A and B combination(primary key).
So in such case how should be define the entity?
#Embeddable
public class PrimayKey implements Serializable{
#Column(name = "A")
private String a;
#Column(name = "B")
private String b;
}
#Entity
#Table(name = "X")
public class X{
#EmbeddedId
private PrimayKey key;
#Column(name = "C")
private String c;
#Column(name = "D")
private String d;
}
Here if I go for a find by Primary Key its not working as we have multiple values of C and D for each A and B combination. In such case how should we define entity?

You can create an embeddable for partition key and then use it as a field in primary key. Kundera will automatically read the entities and consider the fields other than partition keys as clustering keys.
Cassandra equivalent Primary key : ((A, B), C)
Entity :
#Embeddable
public class PartitionKey {
#Column(name = "A")
private String a;
#Column(name = "B")
private String b;
}
#Embeddable
public class PrimayKey {
private PartitionKey key;
#Column(name = "C")
private String c;
}
Please refer this sample entity for more info.

Related

Pass Excel value into multiple methods of same class

I'm using selenium and Java to create my automated scripts. I need one help from you all.
I have an excel which contains 5 columns - say names are Col1 , Col2 , Col3 , Col4, Col5.
I have a class.
public class testone{
#Test(Dataprovider="test")
public void methodone(String Col1) {
}
#Test(Dataprovider="test")
public void methodtwo(String Col2, String Col3){
}
#Test(Dataprovider="test")
public void methodthree(String Col4, String Col5){
}
#DataProvider(name="test")
public Object[][] getData() {
String sheetName = "headercomptests";
int rows = excel.getRowCount(sheetName);
int cols = excel.getColumnCount(sheetName);
Object[][] data = new Object[rows - 1][cols];
for (int rowNum = 2; rowNum <= rows; rowNum++) {
for (int colNum = 0; colNum < cols; colNum++) {
data[rowNum - 2][colNum] = excel.getCellData(sheetName, colNum, rowNum); } }
return data; } //end of dataprovider class
For now it throws error in all methods as the number of cols don't match the arguments. How can i get it as mentioned.
If the number of parameters match excel cols , it works.
Thank you.
Do not over-complicate your code. Since your tests need different data, use two different data providers. One for each test method. Each data provider would provide its own set of columns.
You can use HashMap in your #DataProvider and #Test method.
Update DataProvider to return HashMap
#DataProvider(name="test")
public Object[][] getData() {
Object[][] data = new Object[rows - 1][cols];
for (int rowNum = 2; rowNum <= rows; rowNum++)
{
// Create Hashmap after every row iteration
HashMap<Object, Object> testData = new HashMap<Object, Object>();
for (int colNum = 0; colNum < cols; colNum++)
{
//Assuming excel sheet has Col1 , Col2 , Col3 , Col4, Col5 as name in 1st row
testData.put(excel.getCellData(sheetName, colNum, 0),excel.getCellData(sheetName, colNum, rowNum));
}
// Add every row in hashmap
data[r][0] = testData;
}
return data;
}
Please update above code as per excel sheet for proper mapping with HashMap
Update all #Test methods by passing HashMap as parameter and then inside each #Test method, retrieve column values are per requirement :
#Test(Dataprovider="test")
public void methodone(Map<Object, Object> testData) {
//Get value of column 'col1' from testData HashMap
String Col1 = (String)testData.get("Col1");
}

jpa select from two tables without relation

I have two jpa entities. Say A and B. A and B doesn't have any relation. Both of them have a common column "id". This id of A and id of B are completely different in the sense they are not related to each other.
Consider A:
id col1 col2
1 g l
5 h p
Consider B:
id col3 col4 col5
2 p h i
7 j l k
Now I should get a list consisting of both A and B entities sorted by their id values like below. Also need a extra new column that signifies its type:
type A 1 g 1
type B 2 p h i
type A 5 h p
type B 7 j l k
So this list should be returned in a result set and I should be able to add each resultant entity into its own respective list of A and B entity type finally.Can any one please help me with a query accomplish this. How should we do this through jpql?
Im not sure if this is good design but what you can do it is have a marker class and use that to have id and then have A & B extending marker class and use marker class to combine them.
Code Snippet:
Marker Class :
public class MarkerClass {
private int id;
public int getId() {
return id
}
// you need to implement a hash code and equals here
// you can have other common fields here
}
Class A :
public class A extends MarkerClass {
private String col1;
private String col2;
....
// getters for them
}
Class B
public class B extends MarkerClass {
private String col1;
private String col2;
....
// getters for them
}
Now you can combine them like
List< MarkerClass > listOfAllAAndB = new ArrayList();
listOfAllAAndB.addAll(allA);
listOfAllAAndB.addAll(allB);
Collections.sort(listOfAllAAndB, myCustomComparator);
Hope this helps.

JSR-303 bean validation with Groovy traits (Hibernate Validator)

I run into a problem with JSR-303 bean validation with a Groovy class that implements Traits with additional fields (plus their constraints). Let me show you a simple example. I have a class called ClientInfo which looks like this:
class ClientInfo implements AddressInfo, ContactInfo, BusinessEntity { }
It implements 3 traits that provides additional information. AddressInfo may look like this:
trait AddressInfo {
#Size(max = 128)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String contactName
#Size(max = 255)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String addressLine1
#Size(max = 255)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String addressLine2
#Size(max = 32)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String postalCode
#Size(max = 64)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String city
#Size(max = 3)
#SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
String countryCode
}
So far everything looks good. The problem shows up when the constraint validation fails. For example, if countryCode field fails during ClientInfo instance validation, it holds error message under the AddressInfo__countryCode field name:
{"AddressInfo__countryCode":"size must be between 0 and 3"}
It is caused by the fact that the fields that come from the trait are compiled into e.g. AddressInfo__countryCode, AddressInfo__addressLine1 and so one, providing getters and setters for those fields.
I tried to use Jackson annotations like #JsonProperty('countryCode') over the countryCode field in the AddressInfo trait, but it didn't help (the annotation was added to compiled class).
My question is: is it possible to use e.g. Jackson property mappers to render field names provided in #JsonProperty('...') annotation for the fields provided by implemented trait? I will really appreciate your help.
PS: I'm using 'org.hibernate:hibernate-validator:5.1.3.Final' bean validation implementation (maybe this one should be change).

c# Sorting a object list by combination of three values in the same object

List<string[]> travelerList = new List<string[]>();
string[] traveler1 = {"c","3","b"};
string[] traveler2 = {"a","1","d"};
string[] traveler3 = {"d","4","a"};
string[] traveler4 = {"b","2","c"};
travelerList.Add(traveler1);
travelerList.Add(traveler2);
travelerList.Add(traveler3);
travelerList.Add(traveler4);
I have three columns in a table in Database which will look like
Column1 Column2 Column3
a 1 d
b 2 c
c 3 b
d 4 a
I need to sort the travelerList by combination of above three columns so that after sorting it looks like
{"a","1","d"}
{"b","2","c"}
{"c","3","b"}
{"d","4","a"}
If you can guarantee that the string[] will always have at least 3 elements, you can sort easily enough:
travelerList.OrderBy(t => t[0]).ThenBy(t => t[1]).ThenBy(t => t[2])
Of course, a strongly-typed object is a bit more secure in the matter than a string[]. Something as simple as:
class Traveler
{
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
}
Then if the list is a List<Traveler> you could be somewhat safer with:
travelerList.OrderBy(t => t.Column1).ThenBy(t => t.Column2).ThenBy(t => t.Column3)
You could even take it a step further and define the object to implement IComparable so that it internally knows how to compare itself with other instances. Then you'd just need:
travelerList.OrderBy(t => t)
This approach would lean more toward object oriented principles, keeping the logic encapsulated within the model so it doesn't need to be repeated by multiple instances of consuming code.
A custom comparator is what you're looking for. Article Here
I'm not sure how this is related to your database since your showing a List<string[]>.
However, ignoring that you can use OrderBy + ThenBy:
travelerList = travelerList
.OrderBy(t => t[0])
.ThenBy(t => t[1])
.ThenBy(t => t[2])
.ToList();

How do I assign a String at Array1[x] to an int at Array2[x]?

I'm trying to organize data I am given from a text file, there are for 4 pieces of info on each line (City, country, population, and date). I wanted to have an array for each so I first put it all into one big String array and started to separate them into 4 arrays but I needed to change the Population info to an int array but it says *
"Type mismatch: cannot convert from element type int to String"
//Separate the information by commas
while(sc.hasNextLine()){
String line = sc.nextLine();
input = line.split(",");
//Organize the data into 4 seperate arrays
for(int x=0; x<input.length;x++){
if(x%4==0){
cities[x] = input[x];
}
if(x%4==1){
countries[x] = input[x];
}
if(x%4==2){
population[x] = Integer.parseInt(input[x]);
}
if(x%4==3){
dates[x] = input[x];
}
}
}
And when I print out the arrays they have a bunch of nulls in between each data. I'm planning to create objects that have the 4 pieces of data so that I can then sort them by population, dates etc... I'm pretty new to working with objects so if anyone has a better way of getting the 4 pieces of data into an object cause I haven't figured a way yet :/ My end goal was to have an array of these objects that I can u different sorting methods on them
I would recommend doing something like this:
public class MyData {
private String city;
private String country;
private Integer population;
private String date;
public MyData(String city, String, country, Integer population, String date) {
this.city = city;
this.country = country;
this.population = population;
this.date = date;
}
// Add getters and setters here
}
And then in the file you're posting about:
...
ArrayList<MyData> allData = new ArrayList<MyData>();
while(sc.hasNextLine()) {
String[] values = sc.nextLine().split(",");
allData.add(new MyData(values[0], values[1], Integer.parseInt(values[2]), values[3]));
}
...
You need an object to store the data in so that you keep the relationship between the values in each column.
Also, I'm just assuming you're using Java here. Which language we're talking about is something you should include in your question or as a tag.
The problem is with your x index. If you look carefully at your "for" you will see that it will insert a value at every 3 positions.
try
int index = 0;
while(sc.hasNextLine()){
String line = sc.nextLine();
input = line.split(",");
//Organize the data into 4 seperate arrays
for(int x=0; x<input.length;x++){
if(x%4==0){
cities[index] = input[x];
}
if(x%4==1){
countries[index] = input[x];
}
if(x%4==2){
population[index] = Integer.parseInt(input[x]);
}
if(x%4==3){
dates[index] = input[x];
}
}
++index;
}

Resources