AutoFitTableAppender

AutoFitTableAppender is used to write data into the target table.

Deprecated name: tableAppender

Constructor

Syntax

AutoFitTableAppender(String dbUrl, String tableName, DBConnection conn)

Parameters

  • dbUrl: The DFS database path. An empty string indicates an in-memory table.
  • tableName: Target DFS table.
  • conn: The declared connection object.
  • action: Actions when appending to the table. Currently, only fitColumnType is supported for converting the column types.

Examples

Constructing an AutoFitTableAppender:

AutoFitTableAppender appender=new AutoFitTableAppender("dfs://tableAppenderTest", "testAppend", conn, com.xxdb.route.AutoFitTableAppender.APPEND_ACTION.fitColumnType);

append Method

Call the append method to write a BasicTable object to the target table.
  • Since API version 3.00.1.1, the number of appended rows is returned.
  • For versions before 3.00.1.1, a table is retuned:
    • For an in-memory table, the table is returned.
    • For a DFS table, an empty table is returned.

Syntax

public Entity append(BasicTable table)

Parameters

  • table: The table to be written.

Examples

Calling the append method to write a table:

ArrayList<String> colName = new ArrayList<>();
colName.add("id");
colName.add("time");
colName.add("data");
BasicTable insertTable = new BasicTable(colName, cols);
AutoFitTableAppender appender = new AutoFitTableAppender("dfs://tableAppenderTest", "testAppend", conn);
appender.append(insertTable);

Usage Example

Below is an example of using AutoFitTableAppender to append data to a table (focus on lines 40-42).

// Connect
DBConnection conn = new DBConnection();
conn.connect("192.168.0.68", 8848, "admin", "123456");

// Prepare the table to be written
int size=100000;
int[] id=new int[size];
double[] data=new double[size];
BasicTimestampVector timeVector=new BasicTimestampVector(size);
Random rand=new Random();
for(int i=0;i<size;++i){
    LocalDateTime dt=LocalDateTime.now();
    timeVector.setTimestamp(i,dt);
    id[i]= rand.nextInt();
    data[i]=rand.nextDouble();
}
BasicIntVector idVector=new BasicIntVector(id);
BasicDoubleVector dataVector=new BasicDoubleVector(data);
ArrayList<Vector> cols=new ArrayList<>();
cols.add(idVector);
cols.add(timeVector);
cols.add(dataVector);
ArrayList<String> colName=new ArrayList<>();
colName.add("id");
colName.add("time");
colName.add("data");
BasicTable insertTable = new BasicTable(colName, cols);

// Create a table
conn.run("\n" +
        "login(`admin,`123456)\n" +
        "dbPath = \"dfs://autoFitTableAppenderTest\"\n" +
        "if(existsDatabase(dbPath))\n" +
        "dropDatabase(dbPath)\n" +
        "t = table(100:0,`id`time`data,[INT,TIME,DOUBLE])\n" +
        "db=database(dbPath,HASH, [INT,10])\n" +
        "pt = db.createPartitionedTable(t,`testAppend,`id)");
        
// Create AutoFitTableAppender and write to the table
AutoFitTableAppender appender=new AutoFitTableAppender("dfs://autoFitTableAppenderTest","testAppend",conn);
appender.append(insertTable);
Entity assertRet= conn.run("exec count(*) from loadTable(\"dfs://autoFitTableAppenderTest\", \"testAppend\")");

// Get the number of rows written
String ret=assertRet.getString();
System.out.println(ret);

Execution result:

100000