All Products
Search
Document Center

MaxCompute:Example of data upload by using BufferedWriter

Last Updated:Nov 22, 2023

This topic uses sample code to describe how to upload data by using the BufferedWriter interface.

// Initialize the code of MaxCompute and MaxCompute Tunnel. 
RecordWriter writer = null;
TableTunnel.UploadSession uploadSession = tunnel.createUploadSession(projectName, tableName);
try {
  int i = 0;
  // Construct a BufferedWriter for MaxCompute Tunnel. 
  writer = uploadSession.openBufferedWriter();
  Record product = uploadSession.newRecord();
  for (String item : items) {
    product.setString("name", item);
    product.setBigint("id", i);
    // Call the write interface to write data. 
    writer.write(product);
    i += 1;
  }
} finally {
  if (writer != null) {
    // Disable the BufferedWriter for MaxCompute Tunnel.
    writer.close();
  }
}
// Commit the upload session to end the data upload. 
uploadSession.commit();

Code description

items: the data that you want to upload. You can define the data by using arrays of the STRING type. Example: String[] arr={"s1","s2"}.

name: the name column to which you want to write data of the STRING type. You can specify the column name and data type based on your business requirements.

id: the id column to which you want to write data of the BIGINT type. You can specify the column name and data type based on your business requirements.