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:表示每一列的具体数据。可传入 Vector 列表、Java 原生类型列表或 Java 原生类型数组。BasicTable(final List<String> colNames, final Collection<?> cols) 中,cols 仅支持传入 List,其中每列数据可为 Java List 或数组。

colTypes:表示每一列的数据类型。

colExtraParams:表示部分数据类型的额外参数,如 DECIMAL 类型的 scale。

说明

  • 传入 Vector 列表时,列类型由对应的 Vector 决定。

  • 不指定 colTypes 时,Java 原生类型会自动映射为 DolphinDB 数据类型,具体规则见“自动类型映射规则”。

  • 指定 colTypes 时,会按照指定的 DolphinDB 数据类型构造列。

  • 对于 DECIMAL 类型,可以通过 colExtraParams 指定 scale。

  • 通过数组传入列数据时,不支持传入原生类型数组,例如 int[]、float[] 等。请使用对应的包装类型数组,例如 Integer[]、Float[]。

自动类型映射规则

不指定 colTypes 时,Java 原生类型会按照以下规则自动映射为 DolphinDB 数据类型。该规则也适用于通过 addColumn 传入 Java List 或数组的场景。

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,再通过构造方法传入。

示例
// 创建空表,并通过 addColumn 添加列
BasicTable table1 = new BasicTable();
table1.addColumn("id", new BasicIntVector(new int[]{1, 2, 3}));

// 通过 Vector 构造表
List<String> colNames2 = Arrays.asList("id");
List<Vector> cols2 = Arrays.asList(new BasicIntVector(new int[]{1, 2, 3}));
BasicTable table2 = new BasicTable(colNames2, cols2);

// 通过 Java List 构造表,自动映射数据类型
List<String> colNames3 = Arrays.asList("id", "name");
List<?> 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", "value");
List<?> cols5 = Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(1.1, 2.2, 3.3));
DATA_TYPE[] colTypes5 = new DATA_TYPE[]{DATA_TYPE.DT_INT, DATA_TYPE.DT_DOUBLE};
BasicTable table5 = new BasicTable(colNames5, cols5, colTypes5);

// 通过 Java List 构造表,并指定 DECIMAL 类型的 scale
List<String> colNames6 = Arrays.asList("price");
List<?> cols6 = Arrays.asList(Arrays.asList("1.23", "4.56", "7.89"));
DATA_TYPE[] colTypes6 = new DATA_TYPE[]{DATA_TYPE.DT_DECIMAL32};
int[] colExtraParams6 = new int[]{2};
BasicTable table6 = new BasicTable(colNames6, cols6, colTypes6, colExtraParams6);

// 通过 Java 数组构造表,并指定数据类型
List<String> colNames7 = Arrays.asList("id", "value");
Object[] cols7 = new Object[]{new Integer[]{1, 2, 3}, new Double[]{1.1, 2.2, 3.3}};
DATA_TYPE[] colTypes7 = new DATA_TYPE[]{DATA_TYPE.DT_INT, DATA_TYPE.DT_DOUBLE};
BasicTable table7 = new BasicTable(colNames7, cols7, colTypes7);

// 通过 Java 数组构造表,并指定 DECIMAL 类型的 scale
List<String> colNames8 = Arrays.asList("price");
Object[] cols8 = new Object[]{new String[]{"1.23", "4.56", "7.89"}};
DATA_TYPE[] colTypes8 = new DATA_TYPE[]{DATA_TYPE.DT_DECIMAL32};
int[] colExtraParams8 = new int[]{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)");

表的基本使用

1. 通过 addColumn 方法向表中添加列,方法定义如下:
public void addColumn(String colName, Vector col)
public void addColumn(String colName, List<?> values)
public void addColumn(String colName, Object[] values)

参数介绍

colName:列名。

col:要添加的列数据,类型为 Vector。

values:要添加的列数据,可传入 Java List 或数组。

说明

  • 传入 Vector 时,列类型由该 Vector 决定。

  • 传入 Java List 或数组时,Java 原生类型会自动映射为 DolphinDB 数据类型,具体规则见“自动类型映射规则”。

  • 通过数组传入列数据时,不支持传入原生类型数组,例如 int[]、float[] 等。请使用对应的包装类型数组,例如 Integer[]、Float[]。

  • 新列长度必须与表中已有列的长度一致。空表不受此限制。

  • 对于需要精确指定类型或暂不支持自动映射的类型,建议先构造对应的 Vector,再调用 addColumn

示例
// 通过 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));
}