I have simple JTable object with 2 columns. I want to put here values from file.properties but I don't know how do this.
For example file.properties looks like:
some1.text1=Text1
some1.text2=Text2
some2.text1=Text_1
some2.text2=Text_2
And now I want to add these data to TableModel like this(it's example from swing):
Object rowData[][] = { { some1.text1, some2.text1 }, ... };
How can I do this?
You would NOT create a 2 dimensional array since you may not know how many properties you have.
Instead you would create one row of data for each property and then add the row to the DefaultTableModel. The basic logic would be something like:
String columnNames = { "Column1", "Column2" };
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for (each property pair)
{
Vector<String> row = new Vector<String>(2);
row.addElement( get first value );
row.addElement( get second value );
model.addRow( row );
}
JTable table = new JTable( model );
I found one way to do this using 'new Property()'
This read my file.propertieswell but now I'm interesting something else. How can I read my file in other way, for example my file.property looks like:
some.1.name=...
some.1.value=...
some.2.name=...
some.2.value=...
I can read each of them like this
#ResourceBundleBean(key="some.1.name")
private String some_1_name;
#ResourceBundleBean(key="some.1.value")
private String some_1_value;
etc...
But if is there possibility to use only one String field for for name and value(Value is String too) OR only 1 String field to get each property some.1. some.2. etc and get from this field name and value?
For example if my file.properties will have many item only with name/value some like:
some.1.name=...
some.1.value=...
...
some.200.name=...
some.200.value=...
I do not want to create 200 fields to do this. Is it possible?
Or if it is not possible how can I read arrays from property?
Instead of upper properties make some like this:
some.[1].name=n1
some.[1].value=v1
...
some.[200].name=n200
some.[200].value=v200
And how can I read this array to use it for output some like:
n1 - v1
...
n200 - v200
Related
I decided it would be easiest to store my data in a custom Row object based on this post
Java data structure to store tabular data
But Im wondering how I actually go onto then create a JTable with this Array of row objects
My current attempt starts out looking like this:
public class TestTable extends JTable {
private final String[] columnNames = {"col1", "col2", "col3"};
public TestTable() {
DefaultTableModel model = new DefaultTableModel(columnNames, 10); //10 is just a placeholder for the number of rows as I dont know how to add the data yet
}
}
My data would be stored in an ArrayList that looks like this:
Row row1 = new Row("1", "2", "3");
ArrayList<String> data = new ArrayList<String>;
data.add(row1);
data.add(row2);
...
data.add(row10);
where my final ArrayList would just look like a list of row objects
List<String> data = List.of("1", "2", "3" );
model.addRow(data.toArray());
would be a simple approach
How to pass multiple values in objType field to a method parameter
Currently i'm storing single row in objType field and passing that as an input to an oracle sp, now i need to store and pass multiple rows in objType. How to achieve that?
I've tried creating objType like multidimensional one:
Object[] objType = new Object[3][3],
it doesn't help.
Please see my sample code below and help.
Object[] objType = new Object[3];
objType[0] = new Integer(lineNo);
objType[1] = new String(itemCode);
objType[2] = new Integer(ORDER_QTY));
structs[index]=conn.createStruct("XXHDB_REC", objType);
Array reportsArray = ((OracleConnection)
conn).createOracleArray("XXHDB_TBL_TYPE", structs);
//Input to oracle package
oracleCallableStmt.setArray(4, reportsArray);
I need to store 'n' of rows with fixed 3 columns. [n][3].
Stored Proc Definition:
create_booking(p_reservation_id => p_reservation_id,
p_Hybris_Cust_nbr => p_Hybris_Cust_nbr,
p_cust_nbr => p_cust_order_no,
p_group => j.GROUP_ID,
p_order_lines => v_rec) --> this is the input field
Your example code Object[] objType = new Object[3][3] does not work as objType has the wrong type, it should be Object[][] objType = new Object[n][3]. However I strongly recommend to not use Object as type if you know the values you want to store, instead you could use some sort of container for them like:
class Container {
private Integer lineNo;
private String itemCode;
private Integer ORDER_QTY;
// rest omitted
}
And then use this as your type your array or whatever you want to use. Container[] objType = new Container[n];.
I have this java code, where a spark UDF takes a Row as an input and returns a Row. There is also a broadcast variable which is a HashMap.
All the UDF does is it checks if the broadcast HashMap contains the rowKey and if it does, returns a new row with some existing values from input row and some updated values from the broadcast HashMap. If not, returns the input row as is. I do this as I want to update the row column values based on values in the HashMap. Here is the code:
Broadcast variable
final Broadcast<HashMap<String, HashMap<String, String>>> broadcastVariable = jsc.broadcast(someHashMap);
UDF Definition
UDF1<Row, Row> myUDF = new UDF1<Row, Row> () {
#Override
public Row call(Row inputRow) {
String myKey = inputRow.getString(3);
if (broadcastVariable.value().containsKey(myKey)){
Map<String, String> valuesToUpdate = broadcastVariable.value().get(myKey);
String col1 = inputRow.getString(0);
String col2 = inputRow.getString(1);
String col3 = inputRow.getString(2);
for (Map.Entry<String, String> entry : valuesToUpdate.entrySet())
{
String columnName = entry.getKey();
switch(columnName) {
case "col1" :
col1 = entry.getValue();
break;
case "col2" :
col2 = entry.getValue();
break;
case "col3" :
col3 = entry.getValue();
break;
}
}
return RowFactory.create(col1,col2,col3,myKey);
}
return inputRow;
}
};
UDF Registration
hiveContext.udf().register("myUDF", myUDF, DataTypes.createStructType(DF1.schema().fields()));
UDF Call
DataFrame DF2 = DF1.select(org.apache.spark.sql.functions.callUDF
("myUDF", org.apache.spark.sql.functions.struct(DF1.col("col1"),
DF1.col("col2"),
DF1.col("col3"),
DF1.col("myKey"))));
I have the following questions,
How can I pass all the columns in the dataframe to the UDF without listing them one by one? The reason I'm asking this is the actual DataFrame has more than 50 columns. I saw this example, but couldn't get it to work in Java.
Is there a way I can access the row columns by name within the UDF? Right now I'm using getString(int).
The UDF output, is a Struct with a name myUDF(struct(col1,col2,col3,myKey)). It gets really long with 50+ columns. How can I alias this?
Any help is appreciated!
TL;DR Use Dataset.map (and replace the UDF with a map function).
How can I pass all the columns in the dataframe to the UDF without listing them one by one?
dataframe.schema.fieldNames
See Dataset API.
Is there a way I can access the row columns by name within the UDF?
Quoting the scaladoc of Row.fieldIndex:
fieldIndex(name: String): Int Returns the index of a given field name.
and use the index.
It gets really long with 50+ columns. How can I alias this?
Looks like your code would benefit from some refactoring and composition. Working with 50 fields in a single pipeline might get a bit unwieldy.
You don't need to know the column names in advance!
You can have Row type as one of the arguments of your udf. For example:
import org.apache.spark.sql.functions._
val myUdf = udf((row: Row) => <here comes the code inside your udf>)
You call that udf like this:
df.withColumn(newColumnName, myUdf(struct(df.columns map col: _*)))
and then you can access the dataframe row (both structure and data) inside the udf for anything you need, for example - convert the row to a map of (column_name -> column_value):
val myUdf = udf((row: Row) => row.getValuesMap(row.schema.fieldNames))
I try to set data from list to two multidimensional array.
Normally I can set data to two multidimensional array like this:
Object[][] newData = {
{ "test", "test2", 15.98, 0.14, "+0.88%",
32157250 },
{ "AAPL", "Apple Inc.", 126.57, -1.97, "-1.54%", 31367143 }"
However I want to set data dynamically from list .
I have a method wich return a list :
List<User> user = listUser(id);
static User {
private int id;
private String name;
and getter(...),setter(..).
I need to set user from listUser(id) method to Object[][] array.
I try to do it but I couldnt get succesfull result :
for (int i=0;i<user.size();i++){
for(int j=0;j<user.size();j++){
newData[i][j]=user.get(i).getName();
}
}
Could you help me ?
The columns are fixed, i.e., 0,1,2,etc.. and you need to iterate and set the data for each row as shown below:
for (int i=0;i<user.size();i++){
for(int j=0;j<user.size();j++){
newData[i][j]=user.get(i).getId();//get id for each rowand set to 0th column
newData[i][j]=user.get(i).getName();
newData[i][j]=user.get(i).getX();//other fields
newData[i][j]=user.get(i).getY();//other fields
}
}
Also, it is not a good idea to use Object[][] (not sure for what reason you are using this) as it requires explicit casting while retrieving/using the fields.
It looks like the data is well structured. I would create a class and keep a single-dimension array of your private "CompanyStock" class instead of using a dangerous 2d Object[][] array.
I need to build a dynamic dataTable, which carry an object from the database with the following structure:
I need to build a dynamic dataTable, which carry an object from the database with the following structure:
Object:
Name = Ana
Cpf = 12364547
I need this data to be displayed in two columns, for example:
column 1
name
cpf
column 2
Ana
12364547
I have the following code:
public void criarTabelaDinamica(Object obj){
fc = FacesContext.getCurrentInstance();
this.tableMensagem = (HtmlDataTable) fc.getApplication().createComponent(HtmlDataTable.COMPONENT_TYPE);
HtmlColumn column1 = new HtmlColumn();
tableMensagem.getChildren().add(column1);
HtmlColumn column2 = new HtmlColumn();
tableMensagem.getChildren().add(column2);
tableMensagem.setValue(obj); //how to put the items in the two columns of the object? Here is my question!!!
}
note: The object varies on the amount of items in my example are 2, you may have 5 for example.
Thanks!!!