Manually creating tables and columns
Deephaven TableTools contains many methods for creating and modifying tables. By default, all methods of TableTools are statically imported into each Groovy session, making an import unnecessary. In Python, the TableTools methods are in the deephaven.TableTools
package and must be imported before they are used.
Some of these functions follow.
Manually creating tables
emptyTable(numRows)
- Creates an empty table with the number of specified rows, but no columns. Once created, users can update the table and add columns and other useful data.
For example:
empty = emptyTable(10).update("Column = i")
from deephaven.TableTools import emptyTable
empty = emptyTable(10).update("Column = i")
-
emptyTable(int size, TableDefinition tableDefinition)
- Creates an empty table with size rows, and column names and types fromtableDefinition
. -
newTable(ColumnHolder... columns)
- Creates a new in-memory table based on arrays, creating one column per array you pass in.
The following example starts by defining four arrays of data. These arrays are then passed into the column creation methods (col()
, intCol()
, doubleCol()
, and charCol()
), which take a name and an array of data, and create a ColumnHolder object that can be added to a table:
myStrings = ["Str1", "Str2", "Str3", "Str4"] as String[]
myInts = [1, 2, 4, 8] as int[]
myDoubles = [1.0, 2.0, 4.0, 8.0] as double[]
myCharacters = ['A', 'B', 'C', 'D'] as char[]
myTable = newTable(
col("StringColumn", myStrings),
intCol("IntegerColumn", myInts),
doubleCol("Decimals", myDoubles),
charCol("Characters" , myCharacters)
)
from deephaven import *
myStrings = ["Str1", "Str2", "Str3", "Str4"]
myInts = [1, 2, 4, 8]
myDoubles = [1.0, 2.0, 4.0, 8.0]
myCharacters = ['A', 'B', 'C', 'D']
myTable = ttools.newTable( \
ttools.col("StringColumn", myStrings), \
ttools.intCol("IntegerColumn", myInts), \
ttools.doubleCol("Decimals", myDoubles), \
ttools.charCol("Characters" , myCharacters) \
)
Tip
We recommend using newTable
for small static tables, and emptyTable
when using formulas:
For example:
x = newTable(intCol("X", 42))
is much better than
y = emptyTable(1).view("X=42")
Manually Creating Columns
-
ColumnHolder<T> col(String name, T... values)
- Creates a new data column. -
Users may want to create columns for a new table out of arrays. Two examples follow:
myStrings = ["Str1", "Str2", "Str3", "Str4"] as String[]
myInts = [1, 2, 4, 8] as int[]
myDoubles = [1.0, 2.0, 4.0, 8.0] as double[]
myCharacters = ['A', 'B', 'C', 'D'] as char[]
myTable = newTable(
stringCol("StringColumn", myStrings),
intCol("IntegerColumn", myInts),
doubleCol("Decimals", myDoubles),
charCol("Characters" , myCharacters)
)
from deephaven import *
# deephaven.TableTools module imported as as ttools
myTable1 = ttools.newTable(
ttools.stringCol("StringColumn", "Str1", "Str2", "Str3", "Str4"),
ttools.intCol("IntegerColumn", 1, 2, 4, 8),
ttools.doubleCol("DoubleColumn", 1.0, 2.0, 4.0, 8.0),
ttools.charCol("CharColumn", "A", "B", "C", "D"))
myTable2 = ttools.newTable(
ttools.stringCol("StringColumn", ["Str1", "Str2", "Str3", "Str4"]),
ttools.intCol("IntegerColumn", [1, 2, 4, 8]),
ttools.doubleCol("DoubleColumn", [1.0, 2.0, 4.0, 8.0]),
ttools.charCol("CharColumn", ["A", "B", "C", "D"]))
Each line of the query specifies the data type for the column, its name, and the data for that column in the form of an array. In the Groovy script, each array is first assigned to a variable, which is then included in the query that generates the new table.
import com.illumon.iris.db.v2.InMemoryTable
Object columnData = new Object[2]
columnNames = ["OrderType","OrderName"] as String[]
columnData[0] = ['0','1','2','3'] as byte[]
columnData[1] = ["Unknown","Market","Limit","Peg"] as String[]
OrderTypeCodes = new InMemoryTable(columnNames, columnData)
from deephaven import *
InMemoryTable = jpy.get_type("com.illumon.iris.db.v2.InMemoryTable")
columnNames = ["OrderType","OrderName"]
columnData = [
jpy.array("byte",[0,1,2,3]),
jpy.array("java.lang.String",["Unknown","Market","Limit","Peg"]),
]
orderTypeCodes = InMemoryTable(columnNames,columnData)
- To add a new column to a table definition, use
addColumns()
from theTableManagementTools
class.
Note
See also: Viewing and Loading Data