从DynamoDB表生成CSV //重复条目

问题描述

我正在遍历DynamoDB表,并且要对所有表行的csv文件进行迭代。但是,csv中的大多数条目都是重复的。 DynamoDB表中大约有350K唯一记录(所有记录都是唯一的)。生成文件(使用以下代码)只有大约4K个唯一条目,其余所有都是重复的。

public void fetchItems() throws IOException {
    AmazonDynamoDB amazonDynamoDB = DynamoDBClient.getInstance().getConnection();
    ScanResult result = null;
    ScanRequest req = new ScanRequest().withTableName("TABLE_NAME");
    List<Map<String,AttributeValue>> records = new ArrayList<Map<String,AttributeValue>>();
    do {
        if (result != null) {
            req.setExclusiveStartKey(result.getLastEvaluatedKey());
        }
        result = amazonDynamoDB.scan(req);
        List<Map<String,AttributeValue>> rows = result.getItems();
        Map<String,AttributeValue> staticColumnRecord = new HashMap<String,AttributeValue>();
        for (Map<String,AttributeValue> map : rows) {
            List<String> sNowFlakeColumnsListApparel = new ArrayList<String>(Arrays.asList(apparelColumns));
            for (String key : sNowFlakeColumnsListApparel) {
                AttributeValue value = map.get(key);
                if (value != null) {
                    staticColumnRecord.put(key,value);
                } else {
                    staticColumnRecord.put(key,new AttributeValue().withS("null"));
                }
            }
            records.add(staticColumnRecord);
        }
        
    } while (result.getLastEvaluatedKey() != null);
    buildCSV(records);
}

private void buildCSV(List<Map<String,AttributeValue>> changedRecords) throws IOException {
    List<String> headers = changedRecords.stream().flatMap(map -> map.keySet().stream()).distinct()
            .collect(Collectors.toList());
    try (FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter bwr = new OutputStreamWriter(fos,StandardCharsets.UTF_8)) {
        StringBuffer headerContent = new StringBuffer();
        for (String string : headers) {
            headerContent.append(string);
            headerContent.append(",");
        }
        StringBuffer strBuilder = new StringBuffer();
        for (Map<String,AttributeValue> lmap : changedRecords) {
            StringBuilder stringBuilder = new StringBuilder("");
            String sep = "";
            for (Entry<String,AttributeValue> string2 : lmap.entrySet()) {
                String value = string2.getValue().getS();
                stringBuilder.append(sep).append("\"").append(value).append("\"");
                sep = ",";
            }
            if (!stringBuilder.toString().isEmpty()) {
                strBuilder.append(stringBuilder).append(System.getProperty("line.separator"));
            }
        }
        headerContent.append("\n");
        headerContent.append(strBuilder);
        bwr.write(headerContent.toString());

    } catch (IOException e) {
        e.printstacktrace();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...