All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qiniu.service.qoss.FileStat Maven / Gradle / Ivy

There is a newer version: 8.4.8
Show newest version
package com.qiniu.service.qoss;

import com.google.gson.*;
import com.qiniu.service.interfaces.IStringFormat;
import com.qiniu.service.line.JsonObjParser;
import com.qiniu.service.line.MapToTableFormatter;
import com.qiniu.storage.BucketManager.*;
import com.qiniu.service.interfaces.ILineProcess;
import com.qiniu.storage.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileStat extends OperationBase implements ILineProcess>, Cloneable {

    private String format;
    private String separator;
    private JsonObjParser jsonObjParser;
    private IStringFormat> stringFormatter;

    public FileStat(String accessKey, String secretKey, Configuration configuration, String bucket, String savePath,
                    String format, String separator, int saveIndex) throws IOException {
        super("stat", accessKey, secretKey, configuration, bucket, savePath, saveIndex);
        this.format = format;
        if ("csv".equals(format) || "tab".equals(format)) {
            this.separator = "csv".equals(format) ? "," : separator;
            Map indexMap = new HashMap(){{
                put("key", "key");
                put("hash", "hash");
                put("fsize", "fsize");
                put("putTime", "putTime");
                put("mimeType", "mimeType");
                put("type", "type");
                put("status", "status");
                put("endUser", "endUser");
                put("md5", "md5");
            }};
            this.jsonObjParser = new JsonObjParser(indexMap, true);
        } else if (!"json".equals(this.format)) {
            throw new IOException("please check your format for line to map.");
        }
        this.stringFormatter = new MapToTableFormatter(this.separator, null);
    }

    public FileStat(String accessKey, String secretKey, Configuration configuration, String bucket, String savePath,
                    String format, String separator) throws IOException {
        this(accessKey, secretKey, configuration, bucket, savePath, format, separator, 0);
    }

    public FileStat clone() throws CloneNotSupportedException {
        FileStat fileStat = (FileStat)super.clone();
        fileStat.stringFormatter = new MapToTableFormatter(separator, null);
        return fileStat;
    }

    public String getInputParams(Map line) {
        return line.get("key");
    }

    synchronized public BatchOperations getOperations(List> lineList) {
        batchOperations.clearOps();
        lineList.forEach(line -> {
            if (line.get("key") == null)
                errorLineList.add(String.valueOf(line) + "\tno target key in the line map.");
            else
                batchOperations.addStatOps(bucket, line.get("key"));
        });
        return batchOperations;
    }

    @Override
    public void parseBatchResult(List> processList, String result) throws IOException {
        if (result == null || "".equals(result)) throw new IOException("not valid json.");
        JsonArray jsonArray;
        try {
            jsonArray = new Gson().fromJson(result, JsonArray.class);
        } catch (JsonParseException e) {
            throw new IOException("parse to json array error.");
        }
        JsonObject jsonObject;
        for (int j = 0; j < processList.size(); j++) {
            if (j < jsonArray.size()) {
                jsonObject = jsonArray.get(j).getAsJsonObject();
                // stat 接口查询结果不包含文件名,故再加入对应的文件名
                jsonObject.get("data").getAsJsonObject().addProperty("key", processList.get(j).get("key"));
                if (jsonObject.get("code").getAsInt() == 200)
                    if (!"json".equals(format))
                        fileMap.writeSuccess(stringFormatter.toFormatString(jsonObjParser.getItemMap(
                                jsonObject.get("data").getAsJsonObject())), false);
                    else fileMap.writeSuccess(jsonObject.get("data").toString(), false);
                else
                    fileMap.writeError(processList.get(j).get("key") + "\t" + jsonObject.toString(), false);
            } else {
                fileMap.writeError(processList.get(j).get("key") + "\tempty stat result", false);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy