-
Couldn't load subscription status.
- Fork 0
DataFrames
The Claymore library implements the DataFrame specification version 2.0 for Java.
The unified documentation can be accessed here.
All features provided by the Java implementation which are not officially defined by the specification are documented on this page for further reference.
Sometimes you may want to use a DataFrame with a static column structure. Static in this context means that the column structure of your DataFrame does not change at runtime. If that is the case, then you can use the row annotation feature introduced in Claymore version 2.0.0
Basically, with this feature you can declare your own class that should represent a row for your static DataFrame. This makes working with rows a bit neater because you won't have to deal with arrays of Objects anymore. Additionally, you can also construct a new DataFrame based on the structure of your custom class, which overall reduces code.
So let's take a look at a quick example. Imagine you want to have a DataFrame that holds data about different people. Again, if you don't plan to add, remove or insert any columns at runtime, then your DataFrame is considered static and you could quickly write a custom class representing a row in that particular DataFrame. That class may look like this:
public class FamilyGuy implements Row {
@RowItem("id")
private int id;
@RowItem("name")
private String name;
@RowItem("age")
private byte age;
public FimilyGuy(){ }
//getters and setters ...
}So all you have to do is let your custom class implement Row and annotate all fields that correspond to a particular column with RowItem. Row is just a marker interface, so you won't have to actually implement any methods. The RowItem annotation has only one attribute which lets you specify the name of the column the annotated field belongs to. When this attribute is omitted, the identifying name of the annotated field is used instead. In the above code that would certainly work, but it is recommended to always specify the column name in the annotation. An important thing to note is that you must always write the default no-arguments constructor. Of course, in most cases it makes sense to also write getters and setters for your members but that's not necessarily required by this feature.
Now you can for example add a row to your DataFrame like this:
df.addRow(myGuy);where myGuy is an instance of the FamilyGuy class we just wrote. Getting a row from our DataFrame will now also return an instance of FamilyGuy with all annotated fields already set.
FamilyGuy guy = df.getRow(0, FamilyGuy.class);will return the row at index 0 (the first row) as a FamilyGuy object.
If you want a DataFrame with the appropriate column structure created for you, then you can use a specific constructor which infers that column structure from the annotated fields in your custom class. In the above example we could have created the DataFrame like this:
DataFrame df = new DefaultDataFrame(FamilyGuy.class);As you can see, this is much simpler than having to construct all columns and their labels manually.