すべてのプロダクト
Search
ドキュメントセンター

MaxCompute:BufferedWriterを使用したデータのアップロード例

最終更新日:Jan 08, 2025

このトピックでは、サンプルコードを使用して、BufferedWriterインターフェイスを使用してデータをアップロードする方法について説明します。

// 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();

コードの説明

items: アップロードするデータ。 STRING型の配列を使用してデータを定義できます。 例: String[] arr={"s1","s2"}

name: STRING型のデータを書き込む列の名前。 ビジネス要件に基づいて、列名とデータ型を指定できます。

id: BIGINTタイプのデータを書き込むid列。 ビジネス要件に基づいて、列名とデータ型を指定できます。