0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GCSにアップロードされているCSVファイルを扱う

Posted at

csvファイルを読み込む

blob.getContentでファイルの中身が取得できるが、ファイルサイズが大きい場合は困ってしまう

Bucket bucket = getBucket(bucketName);
Storage.BlobListOption option = Storage.BlobListOption.prefix(directory);

Page<Blob> blobs = bucket.list(option);
Iterator<Blob> iterator = blobs.iterateAll().iterator();

List<String> csvList = new ArrayList<>();
while (iterator.hasNext()) {
    Blob blob = iterator.next();
    byte[] content = blob.getContent(Blob.BlobSourceOption.generationMatch());
    csvList.add(new String(content));
}

for (String csv : csvList) {
    // process...
}

csvファイルを1行ずつ読み込む

getContentの代わりに、以下のようにすれば1行ずつ読みこんで処理できる

ReadChannel readChannel = blob.reader();
BufferedReader br = new BufferedReader(Channels.newReader(readChannel, "UTF-8"));

String line;
while((line = br.readLine()) != null){
    // process...
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?