如何从box-java-sdk中分块读取CSV文件内容
How to read CSV file content from box-java-sdk in chunks
您好,我正在使用 [box][1]
[1]: https://www.box.com/ 存储我的 csv 文件。文件大小为 2GB。现在我想处理文件的每条记录,并根据文件内容做一些操作。
我做了什么:
public class BoxConnector {
public static void main(String[] args) throws IOException {
BoxAPIConnection api = new BoxAPIConnection("My access token");
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
for (BoxItem.Info itemInfo : rootFolder) {
System.out.format("[%s] %s\n", itemInfo.getID(), itemInfo.getName());
BoxFile file = new BoxFile(api, itemInfo.getID());
BoxFile.Info info = file.getInfo();
try {
System.out.println(info.getSize());
File tmpFile = File.createTempFile("file", "temp");
FileOutputStream fsTmpStrem = new FileOutputStream(tmpFile);
long blockSize = 1000;
long roundChunks = info.getSize() / blockSize;
long startByteRange = 0;
long endByteRange = blockSize;
for (long start = 0; start < roundChunks; start++) {
file.downloadRange(fsTmpStrem, startByteRange, endByteRange);
processFile(tmpFile);
startByteRange = endByteRange;
endByteRange = endByteRange + blockSize;
}
//last download block
file.downloadRange(fsTmpStrem, blockSize * roundChunks, info.getSize());
processFile(tmpFile);
} finally {
}
}
}
private static void processFile(File tmpFile) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(tmpFile)));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("Process line record");
}
br.close();
//after each process lets delete the temp file
tmpFile.delete();
}
}
有了这个我就可以得到我上传到 box.com 的文件名。现在我想阅读每条记录和过程。
但是我需要一个 API 允许我访问文件块。
根据开始和结束字节范围定义的块下载此文件 flag.however 由于块下载,我很少有记录被破坏。意思是我没有得到完整的行说下面是我的记录
16F11C78-D004-4600-8D28-445C087D2A7D
31C99F3D-D4C7-418A-9ACC-D9A382BCD53A
30C1AA92-B5B7-4ABF-A631-A8C150D90C4F
D9FC1DBF-B309-4BB1-8955-D9F48F643E97
我得到
16F11C78-D004-4600-8D28-445C087D2A7D
31C99F3D-D4C7-418A-9ACC-D9A382BCD53A
30C1AA92-B5B7-4ABF-A631-A8C150D90C4F
D9FC1DBF-
i.e. B309-4BB1-8955-D9F48F643E97
最后一行的部分缺失。
我应该如何通过下载来管理它 API?
Box API 目前无法做到这一点。您只能下载整个文件。
您好,我正在使用 [box][1]
[1]: https://www.box.com/ 存储我的 csv 文件。文件大小为 2GB。现在我想处理文件的每条记录,并根据文件内容做一些操作。
我做了什么:
public class BoxConnector {
public static void main(String[] args) throws IOException {
BoxAPIConnection api = new BoxAPIConnection("My access token");
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
for (BoxItem.Info itemInfo : rootFolder) {
System.out.format("[%s] %s\n", itemInfo.getID(), itemInfo.getName());
BoxFile file = new BoxFile(api, itemInfo.getID());
BoxFile.Info info = file.getInfo();
try {
System.out.println(info.getSize());
File tmpFile = File.createTempFile("file", "temp");
FileOutputStream fsTmpStrem = new FileOutputStream(tmpFile);
long blockSize = 1000;
long roundChunks = info.getSize() / blockSize;
long startByteRange = 0;
long endByteRange = blockSize;
for (long start = 0; start < roundChunks; start++) {
file.downloadRange(fsTmpStrem, startByteRange, endByteRange);
processFile(tmpFile);
startByteRange = endByteRange;
endByteRange = endByteRange + blockSize;
}
//last download block
file.downloadRange(fsTmpStrem, blockSize * roundChunks, info.getSize());
processFile(tmpFile);
} finally {
}
}
}
private static void processFile(File tmpFile) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(tmpFile)));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("Process line record");
}
br.close();
//after each process lets delete the temp file
tmpFile.delete();
}
}
有了这个我就可以得到我上传到 box.com 的文件名。现在我想阅读每条记录和过程。 但是我需要一个 API 允许我访问文件块。
根据开始和结束字节范围定义的块下载此文件 flag.however 由于块下载,我很少有记录被破坏。意思是我没有得到完整的行说下面是我的记录
16F11C78-D004-4600-8D28-445C087D2A7D
31C99F3D-D4C7-418A-9ACC-D9A382BCD53A
30C1AA92-B5B7-4ABF-A631-A8C150D90C4F
D9FC1DBF-B309-4BB1-8955-D9F48F643E97
我得到
16F11C78-D004-4600-8D28-445C087D2A7D
31C99F3D-D4C7-418A-9ACC-D9A382BCD53A
30C1AA92-B5B7-4ABF-A631-A8C150D90C4F
D9FC1DBF-
i.e. B309-4BB1-8955-D9F48F643E97
最后一行的部分缺失。
我应该如何通过下载来管理它 API?
Box API 目前无法做到这一点。您只能下载整个文件。