Vector
The Java API vector interface is Vector
, which extends the Entity
interface.
A vector is an ordered collection of data.
This section introduces how to create, add, read, and update vectors, with usage examples.
Creating a Vector
Utils.createVector()
You can create a vector using the createVector method provided in the Utils class. The method declaration is as follows:
public static Vector createVector(DATA_TYPE type, int size, int capacity, int extraParam)
Parameters
-
type
:Specifies the type of elements stored in the vector, such as DT_INT, DT_DATE, etc. -
size
:The initial size of the vector. If set to non-zero, the elements will be random. -
capacity
:The capacity allocated for the vector at construction. Setting it reasonably can improve insertion speed. -
scale
:Used when creating decimal-related vectors. This parameter specifies the decimal scale.
Supported data types include:
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
Example:
// Create an INT vector with size 0 and capacity 5
Vector intVector = Utils.createVector(DT_INT, 0, 5);
// Create a DT_DECIMAL32 vector with size 0, capacity 5, and scale 3
Vector decimalVector = Utils.createVector(DT_DECIMAL32, 0, 5, 3);
Constructor
You can also create a vector using the constructor of each vector type.
-
You can specify size and capacity to create a vector with a given size and capacity.
-
You can also pass actual values to construct a vector, usually using an array or a List.
-
The supported Java native types for each vector type can be found in the Scalar.
Example:
// Create an INT vector with size 0 and capacity 6
BasicIntVector bbv = new BasicIntVector(0,6);
// Create an INT vector with specified elements using List<Integer>
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);
// Create an INT vector with specified elements using int[]
BasicIntVector intVector2 = new BasicIntVector(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
// Create a DT_DECIMAL32 vector with specified elements using String[]
String[] tmp_string_v = {"0.0","-123.00432","132.204234","100.0"};
BasicDecimal32Vector decimal32Vector = new BasicDecimal32Vector(tmp_string_v, 4);
Adding Data
Vector provides two ways to add elements.
add
Add a scalar element to the end of the vector.
add(Object value)
The supported Java native types for add can be found in the Scalar.
Example:
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
Add a scalar element to the end of the vector
Create a specific Scalar and append it.
public void Append(Scalar value)
Example:
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]
Add a vector element to the end of the vector
Append another vector.
public void Append(Vector value)
Example:
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());
Reading Data
get
Get the element at the specified index:
public Entity get(int index)
Example:
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
Get a sub-vector of specified indices. Returns a vector.
public Vector getSubVector(int[] indices)
Example:
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());
Updating Data
set
Add or modify a scalar element at a specified index:
public void set(int index, Object value) // Can directly add Java native types
public void set(int index, Entity value) // Can construct a specific Entity to add
The supported Java native types for set(int index, Object value)
can be found in the Scalar.
Example:
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]