簡體   English   中英

從構建的對象上載到CloudSearch的最佳文檔

[英]Optimal document uploading to CloudSearch from built objects

我正在對JSON響應進行一些處理,並希望將結果數據上傳到cloudsearch。 我一次可以做一個文件:

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的好方法? 如果不這樣做,是否有一種簡單的方法來檢查已向流中寫入了多少空間,甚至變量正在使用多少空間?

這只是我用於下載大文件,獲取大小以及處理大小的概念。 讀取循環期間的計數將是流的每個byte [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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM