Table
The Java API defines the data form Table
to facilitate queries and
operations on data.
Creating a Table
Constructor
The constructor of BasicTable
is as follows:
public BasicTable(final List<String> colNames, final List<Vector> cols)
Parameters
-
colNames: Column names of the table.
-
cols: Data for each column.
Retrieving Table Data via Query
You can directly obtain a table by executing a query.
DBConnection dbConnection = new DBConnection();
DBConnection dbConnection = new DBConnection();
dbConnection.connect(HOST,PORT,USER,PASSWORD);
BasicTable basicTable = (BasicTable) dbConnection.run("select * from loadTable(\"dfs://ramd\",`pt3)");
Usage
-
You can get all values of a column using the
getColumn
method, which is defined as follows:// by column index public Vector getColumn(int index) // by column name public Vector getColumn(String name)
The return value is a
com.xxdb.data.Vector
, which can be used to iterate through all data. -
You can manipulate the Vector object returned by
getColumn
to modify the content of the table. For example:public void test_set_nullvalue() throws IOException { try{ List<String> colNames = Arrays.asList("cint","cdouble","clong","cfloat","cshort"); List<Vector> colData = Arrays.asList(new BasicIntVector(1),new BasicDoubleVector(1),new BasicLongVector(1),new BasicFloatVector(1),new BasicShortVector(1)); BasicTable bt = new BasicTable(colNames,colData); bt.getColumn("cint").set(0,new BasicInt(Integer.MIN_VALUE)); bt.getColumn("cdouble").set(0,new BasicDouble(-Double.MIN_VALUE)); bt.getColumn("clong").set(0,new BasicLong(Long.MIN_VALUE)); bt.getColumn("cfloat").set(0,new BasicFloat(-Float.MAX_VALUE)); bt.getColumn("cshort").set(0,new BasicShort(Short.MIN_VALUE)); bt.getColumn("cshort").set(0,new BasicShort(Short.MIN_VALUE)); bt.getColumn("cshort").set(0,new BasicShort(Short.MIN_VALUE)); assertTrue(((Scalar)bt.getColumn("clong").get(0)).isNull()); }catch (Exception e){ e.printStackTrace(); } }
-
If you want to generate each row in JSON format, you can call the
getRowJson
method. For example:DBConnection dbConnection = new DBConnection(); dbConnection.connect(HOST, PORT, USER, PASSWORD); BasicTable basicTable = (BasicTable) dbConnection.run("select * from loadTable(\"dfs://ramd\", `pt3)"); // Get the number of rows int rows = basicTable.rows(); // Iterate through each row for (int i = 0; i < rows; i++) { System.out.println(basicTable.getRowJson(i)); }