将文档从构建对象上传到云搜索的最佳效果

Optimal document uploading to CloudSearch from built objects

本文关键字:搜索 最佳 文档 构建 对象      更新时间:2023-09-26

我正在对 JSON 响应进行一些处理,并希望将结果数据上传到云搜索。我可以一次做一个文档:

const AWS = require("aws-sdk");
const JSONStream = require("JSONStream");
const jsonStream = JSONStream.parse("*");
const csd = AWS.CloudSearchDomain(config);
jsonStream.on("data", processData);
request.get(resultUrl).pipe(jsonStream);
function processData(data) {
  data = doProcessData(data);
  /*
   * `data` is now a JSON object ready to be uploaded to CloudSearch
   * e.g. {type: "add", id: "random-id", fields: {field: "a"}}
   */
  csd.uploadDocuments({contentType: "application/json", documents: [data]});
}

这有效,但 AWS 建议:

为了获得最佳的上载性能,请将接近最大批大小的添加和删除操作分组

我在想我可以将文档写入文件并检查文件大小并在文件为 3MB 时上传文档。我可以接近 5MB,但我不想超过批量大小:

/* Please ignore semantic errors */
filename = "/tmp/foo.json";
file = fs.createWriteStream(filename);
file.write("[");
// in `processData`
  file.write(JSON.stringify(data));
  const stats = file.stat(filename);
  if (stats.size > 3000000) {
    file.write("]");
    csd.uploadDocuments({documents: fs.createReadStream(filename)});
    fs.trunate(filename);
  }
  else {
    file.write(",");
  }

此方法是可以的,但是最好有更好的方法来确定文件是否已准备好上传。如果可以的话,我也希望避免使用文件系统。

我也可以通过执行以下操作在内存中执行此操作:

  const stringifier = JSONStream.stringify("[", ",", "]");
  // in `processData`
    csd.uploadDocuments({documents: stringifier});
    stringifier.write(data);

但是,批量大小可能超过 5MB。我也不确定如何检查已写入 JSON 流的数量。

有没有将派生文档写入 CloudSearch 的好方法?如果做不到这一点,有没有一种简单的方法来检查有多少空间被写入流,甚至变量使用了多少空间?

这只是我用来下载大文件、获取大小和处理大小的概念。读取循环期间的计数将是流的每个字节[1024]块。

@Override
protected String doInBackground(String... f_url) {
    try {
        int count;
        File directory = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "/");
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                throw new Exception("Could not create directory?!?!?");
            }
        }
        String filePath = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/ItemLocalStorage.db";
        URL url = new URL(f_url[0]);
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();
        int contentLength = urlConnection.getContentLength();
        InputStream input = new BufferedInputStream(url.openStream(), 8192);
        OutputStream output = new FileOutputStream(filePath);
        byte data[] = new byte[1024];
        int byt = 0;
        int mb = 0;
        int mbLength;
        while ((count = input.read(data)) != -1) {
            if (!isCancelled()) {
                byt += count;
                mb = ((byt/1024)/1024
                mbLength = ((contentLength / 1024) / 1024);
                output.write(data, 0, count);
            }
        }
        if (isCancelled()) {
            output.write(0);
        }
        output.flush();
        output.close();
        input.close();
        return filePath;
    } catch (Exception e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }
}