表
API 中定义了 Table 的数据形式以方便用户对数据的查询与使用。
创建表
方式一:通过构造方法
public BasicTable()
public BasicTable(final List<String> colNames, final List<Vector> cols)
public BasicTable(final List<String> colNames, final Collection<?> cols)
public BasicTable(final List<String> colNames, final Object[] cols)
public BasicTable(final List<String> colNames, final List<?> cols, final DATA_TYPE[] colTypes)
public BasicTable(final List<String> colNames, final List<?> cols, final DATA_TYPE[] colTypes, final int[] colExtraParams)
public BasicTable(final List<String> colNames, final Object[] cols, final DATA_TYPE[] colTypes)
public BasicTable(final List<String> colNames, final Object[] cols, final DATA_TYPE[] colTypes, final int[] colExtraParams)
参数介绍
-
colNames:表示表的列名。
-
cols:表示每一列的具体数据。
-
colTypes:表示每一列的数据类型。
-
colExtraParams:表示部分数据类型的额外参数,如 DECIMAL 类型的 scale。
-
BasicTable() 用于创建空表,可通过 addColumn 方法逐列添加数据。
-
不指定 colTypes 时,构造方法会根据 Java 数据类型自动映射 DolphinDB 数据类型,映射规则如下:
Java 类型 DolphinDB 数据类型 Boolean BOOL Byte CHAR Short SHORT Integer INT Long LONG Float FLOAT Double DOUBLE String STRING YearMonth MONTH LocalDate DATE LocalTime NANOTIME LocalDateTime NANOTIMESTAMP Date TIMESTAMP Calendar TIMESTAMP byte[] BLOB 对于 DECIMAL、SYMBOL、UUID、IPADDR、INT128、POINT、COMPLEX 等需要精确控制的数据类型,建议先构造对应的 Vector,再创建 BasicTable 或调用 addColumn(String colName, Vector col)。
-
BasicTable(final List<String> colNames, final Collection<?> cols) 中,cols 仅支持传入 List,每一列支持 Java List 或数组。
-
BasicTable(final List<String> colNames, final Object[] cols) 中,每一列需使用 Java 对象数组,如 Integer[]、String[];不支持直接将 primitive array,如 int[]、double[] 作为该构造方法的列数据。
-
指定 colTypes 时,构造方法支持传入 Java 原生数据,或传入 DolphinDB Vector。Java 原生数据和 Vector 不能混用。
-
若传入 Vector,colTypes 和 colExtraParams 可传 null;若传入则会校验 Vector 的类型及额外参数。
// 创建空表,并通过 addColumn 添加列
BasicTable table1 = new BasicTable();
table1.addColumn("id", Arrays.asList(1, 2, 3));
table1.addColumn("name", Arrays.asList("a", "b", "c"));
// 通过 Vector 构造表
List<String> colNames2 = Arrays.asList("id", "name");
List<Vector> cols2 = Arrays.asList(
new BasicIntVector(new int[]{1, 2, 3}),
new BasicStringVector(new String[]{"a", "b", "c"})
);
BasicTable table2 = new BasicTable(colNames2, cols2);
// 通过 Java List 构造表,自动映射数据类型
List<String> colNames3 = Arrays.asList("id", "name");
List<Object> cols3 = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList("a", "b", "c")
);
BasicTable table3 = new BasicTable(colNames3, cols3);
// 通过 Java 数组构造表,自动映射数据类型
List<String> colNames4 = Arrays.asList("id", "name");
Object[] cols4 = new Object[]{
new Integer[]{1, 2, 3},
new String[]{"a", "b", "c"}
};
BasicTable table4 = new BasicTable(colNames4, cols4);
// 通过 Java List 构造表,并指定数据类型
List<String> colNames5 = Arrays.asList("id", "name");
List<Object> cols5 = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList("a", "b", "c")
);
DATA_TYPE[] colTypes5 = new DATA_TYPE[]{
DATA_TYPE.DT_INT,
DATA_TYPE.DT_STRING
};
BasicTable table5 = new BasicTable(colNames5, cols5, colTypes5);
// 通过 Java List 构造表,并指定 DECIMAL 类型的 scale
List<String> colNames6 = Arrays.asList("id", "price");
List<Object> cols6 = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList("12.34", "56.78", "90.12")
);
DATA_TYPE[] colTypes6 = new DATA_TYPE[]{
DATA_TYPE.DT_INT,
DATA_TYPE.DT_DECIMAL64
};
int[] colExtraParams6 = new int[]{-1, 2};
BasicTable table6 = new BasicTable(colNames6, cols6, colTypes6, colExtraParams6);
// 通过 Java 数组构造表,并指定数据类型
List<String> colNames7 = Arrays.asList("id", "name");
Object[] cols7 = new Object[]{
new Integer[]{1, 2, 3},
new String[]{"a", "b", "c"}
};
DATA_TYPE[] colTypes7 = new DATA_TYPE[]{
DATA_TYPE.DT_INT,
DATA_TYPE.DT_STRING
};
BasicTable table7 = new BasicTable(colNames7, cols7, colTypes7);
// 通过 Java 数组构造表,并指定 DECIMAL 类型的 scale
List<String> colNames8 = Arrays.asList("id", "price");
Object[] cols8 = new Object[]{
new Integer[]{1, 2, 3},
new String[]{"12.34", "56.78", "90.12"}
};
DATA_TYPE[] colTypes8 = new DATA_TYPE[]{
DATA_TYPE.DT_INT,
DATA_TYPE.DT_DECIMAL64
};
int[] colExtraParams8 = new int[]{-1, 2};
BasicTable table8 = new BasicTable(colNames8, cols8, colTypes8, colExtraParams8);方式二:通过查询获取表数据
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)");
表的基本使用
public void addColumn(String colName, Vector col)
public void addColumn(String colName, List<?> values)
public void addColumn(String colName, Object[] values)参数介绍
-
colName:表示列名。
-
col:表示 DolphinDB Vector 类型的列数据。
-
values:表示 Java List 或数组类型的列数据。
-
addColumn(String colName, List<?> values) 和 addColumn(String colName, Object[] values) 会根据 Java 数据类型自动映射 DolphinDB 数据类型,映射规则如下:
Java 类型 DolphinDB 数据类型 Boolean BOOL Byte CHAR Short SHORT Integer INT Long LONG Float FLOAT Double DOUBLE String STRING YearMonth MONTH LocalDate DATE LocalTime NANOTIME LocalDateTime NANOTIMESTAMP Date TIMESTAMP Calendar TIMESTAMP byte[] BLOB -
addColumn(String colName, Object[] values) 支持 Java 对象数组,如 Integer[]、String[];不支持 primitive array,如 int[]、double[]。
-
第一次调用 addColumn 时,该列长度作为表的行数;后续添加的列长度需与已有列一致。
-
若需要精确指定 DolphinDB 数据类型,建议先构造 Vector,再调用 addColumn(String colName, Vector col)。
// 通过 Vector 添加列
BasicTable table1 = new BasicTable();
table1.addColumn("id", new BasicIntVector(new int[]{1, 2, 3}));
// 通过 Java List 添加列,自动映射数据类型
BasicTable table2 = new BasicTable();
table2.addColumn("id", Arrays.asList(1, 2, 3));
table2.addColumn("name", Arrays.asList("a", "b", "c"));
// 通过 Java 数组添加列,自动映射数据类型
BasicTable table3 = new BasicTable();
table3.addColumn("id", new Integer[]{1, 2, 3});
table3.addColumn("name", new String[]{"a", "b", "c"});2. 通过 getColumn 方法获取某一列的所有值,方法定义如下:
// 第一种定义,可以通过列号来获取
public Vector getColumn(int index)
// 第二种定义,可以通过列名来获取
public Vector getColumn(String name)
返回值是 Vector 对象(com.xxdb.data.Vector),可以通过这个 Vector 遍历所有数据。
3. 直接操作 getColumn 返回的 Vector 对象来修改表中的内容,示例:
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();
}
}
3. 如果想直接生成每一行 JSON 格式的数据,可以调用 getRowJson 方法:
DBConnection dbConnection = new DBConnection();
dbConnection.connect(HOST,PORT,USER,PASSWORD);
BasicTable basicTable = (BasicTable) dbConnection.run("select * from loadTable(\"dfs://ramd\",`pt3)");
// 获取行数
int rows = basicTable.rows();
// 遍历每一行
for(int i=0;i<rows;i++){
System.out.println(basicTable.getRowJson(i));
}
