向量

Java API 向量接口为 Vector,继承自 Entity 接口。

向量是一组有序排列的数据。

本节介绍向量的创建、添加、读取和更新方式及使用示例。

创建向量

方式一:Utils.createVector()

使用 Utils 中提供的 createVector 方法来创建向量,方法声明如下:

public static Vector createVector(DATA_TYPE type, int size, int capacity, int extraParam)

参数:

  • type:表示 Vector 中存储元素的类型,如 DT_INT,DT_DATE 等。
  • size:表示 Vector 的初始大小。如果设置不为0,则元素的内容随机。
  • capacity:表示 Vector 在构造时分配的容量。合理设置容量可以提高插入数据时的速度。
  • scale:在创建 Decimal 相关类型的 Vector 时使用,该参数可用来传入 Decimal 的 scale。

注意,支持的数据类型有:

DT_VOID, DT_BOOL, DT_BYTE, DT_SHORT, DT_INT, DT_LONG,DT_DATE, DT_MONTH, DT_TIME, 
DT_MINUTE, DT_SECOND, DT_DATETIME, DT_TIMESTAMP, DT_NANOTIME, DT_NANOTIMESTAMP, 
DT_FLOAT,DT_DOUBLE, DT_SYMBOL, DT_STRING, DT_UUID, DT_DATEHOUR, DT_DATEMINUTE, 
DT_IPADDR,DT_INT128, DT_BLOB, DT_DECIMAL, DT_COMPLEX, DT_POINT, DT_DURATION, 
DT_DECIMAL32, DT_DECIMAL64, DT_DECIMAL128

使用示例:

// 创建一个 INT 类型、size 为0、capacity 为5的向量
Vector intVector = Utils.createVector(DT_INT, 0, 5);

// 创建一个 DT_DECIMAL32 类型、size 为0、capacity 为5、scale 为3的向量
Vector intVector = Utils.createVector(DT_DECIMAL32, 0, 5, 3);

方式二:构造方法

可以通过各类型 vector 的构造方法来创建。

  • 可以指定 size、capacity 来创建指定大小、容量的 vector;
  • 在构造的时候传入具体的值来构造,值一般使用数组或者 List 来传入。注意,各类型 vector 构造支持的 Java 原生类型可参考标量

使用示例:

// 创建一个 size 为0、capacity 为6的 INT 类型数组
BasicIntVector bbv = new BasicIntVector(0,6);

// 通过传入 List<Integer> 来创建包含指定元素的 INT 向量
List<Integer> list = new ArrayList<>();
list.add(5);
list.add(7);
list.add(8);
list.add(Integer.MIN_VALUE);
list.add(null);
BasicIntVector intVector = new BasicIntVector(list);

// 通过传入 int[] 来创建包含指定元素的 INT 向量
BasicIntVector intVector = new BasicIntVector(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});

// 通过传入 String[] 来创建包含指定元素的 DT_DECIMAL32 向量
String[] tmp_string_v = {"0.0","-123.00432","132.204234","100.0"};
BasicDecimal32Vector decimal32Vector = new BasicDecimal32Vector(tmp_string_v, 4);

添加数据

Vector 提供两种方式添加元素

方式一:add

用于往向量的末尾添加标量元素。

add(Object value)

注意,各类型 vector add 方法支持的 Java 原生类型可参考标量

代码示例:

Vector intVector = Utils.createVector(DT_INT, 0, 5);
intVector.add(10);
System.out.println(intVector.getString());
// [10]

BasicIntVector intVector = new BasicIntVector(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9});
intVector.add(10);
System.out.println(intVector.getString());
// [1,2,3,4,5,6,7,8,9,10]

方式二:Append

1)往向量的末尾添加标量元素

通过构造具体的 Scalar 来添加。

public void Append(Scalar value)

使用示例:

BasicDoubleVector bbv = new BasicDoubleVector(6,6);
bbv.set(0, 1.11);
bbv.set(1, null);
bbv.set(2, -1.33);
bbv.set(3, 2.99);
bbv.set(4, 0.0);
bbv.set(5, 4.13);
bbv.Append(new BasicDouble(2.23)); // Append(Scalar value)
System.out.println(bbv.getString());
// [1.11,,-1.33,2.99,0,4.13,2.23]

2)往向量的末尾添加向量元素

通过构造具体的 Vector 来添加。

public void Append(Vector value)

使用示例:

BasicIntVector intVector = new BasicIntVector(new int[]{1, 2, 3, 4});
intVector.Append(new BasicIntVector(new int[]{26,31,23,24}));
System.out.println(intVector.getString());

读取数据

get

获取指定位置的元素。

public Entity get(int index)

使用示例:

BasicIntVector intVector = new BasicIntVector(new int[]{1, 2, 3, 4});
intVector.Append(new BasicIntVector(new int[]{26,31,23,24}));
Entity elem = intVector.get(5);
System.out.println(elem.getString());
// 31

getSubVector

通过 getSubVector 获取指定的几行,返回值为一个向量。

public Vector getSubVector(int[] indices)

使用示例:

BasicIntVector intVector = new BasicIntVector(new int[]{1, 2, 3, 4});
intVector.Append(new BasicIntVector(new int[]{26,31,23,24}));
Vector subVector = intVector.getSubVector(new int[]{0, 3, 5});
System.out.println(subVector.getString());

更新数据

set

用于往向量中指定位置添加或修改标量元素:

public void set(int index, Object value) // 可以添加直接添加 java 原生类型
public void set(int index, Entity value) // 可以构造具体的 Entity 来添加

注意,各类型 vector set(int index, Object value) 方法支持的 Java 原生类型可参考标量

使用示例:

BasicDoubleVector bbv = new BasicDoubleVector(6,6);
bbv.set(0, 1.11);
bbv.set(1, null);
bbv.set(2, -1.33);
bbv.set(3, 2.99);
bbv.set(4, 0.0);
bbv.set(5, 4.13);
System.out.println(bbv.getString());
// [1.11,,-1.33,2.99,0,4.13]