解析CSV文件以填充数据库 [英] Parsing a csv file to populate database

查看:97
本文介绍了解析CSV文件以填充数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我有一个这样的csv文件

Given I have a csv file such as this

str_name,int_points,int_bonus
joe,2,5
Moe,10,15
Carlos,25,60

我可以拥有具有x列数和y行数的csv文件,因此我试图开发一种通用方法来对其进行解析并将数据填充到dynamodb表中。

I can have csv file with x number of columns and y number of rows so i am trying to develop a generic method to parse it and populate data in to dynamodb table.

填充dynamodb表,我会做类似的事情

In order to populate the dynamodb table i would do something like this

String line = "";
    String cvsSplitBy = ",";

    try (BufferedReader br = new BufferedReader(
                                new InputStreamReader(objectData, "UTF-8"));

        while ((line = br.readLine()) != null) {

            // use comma as separator
            String[] elements = line.split(cvsSplitBy);

            try {
                table.putItem(new Item()
                    .withPrimaryKey("name", elements[0])
                    .withInt("points", elements[1])
                    .withInt("bonus", elements[2])
                    .....);

                System.out.println("PutItem succeeded: " + elements[0]);

            } catch (Exception e) {
                System.err.println("Unable to add user: " + elements);
                System.err.println(e.getMessage());
                break;
            }

        }

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

但是我并不总是知道我要插入int还是字符串,它取决于csv文件,所以我有点不知道如何创建通用函数,该函数将读取csv文件的第一行并利用前缀(表示特定列是int还是字符串)。

However i would not always know wether i am inserting a int or a string, it is depenedent on the csv file so i was kinda lost on how to create a generic function which would read the first line of my csv file and take advantage of prefix which indicates if the particular column is a int or a string.

推荐答案

好的,我不能将其发布为注释,所以我写了一个简单的示例。请注意,我不熟悉您使用的Amazon API,但是您应该了解我将如何使用它(我基本上已经重写了您的代码)

OK, I can't post this as a comment so I wrote a simple example. Note that I'm not familiar with that Amazon API you're using but you should get the idea how I'd go about it (I've basically rewritten your code)

        String line = "";
        String cvsSplitBy = ",";

        try (BufferedReader br = new BufferedReader(
                            new InputStreamReader(objectData, "UTF-8"));

     String[]  colNames = br.readLine().split(cvsSplitBy);      //first line just to get the column names
     while ((line = br.readLine()) != null) {
        String currColumnName = colNames.get(i);
        // use comma as separator
        String[] elements = line.split(cvsSplitBy);
        boolean isInt ;
        for (int i = 0; i < elements.length;i++){

        try {
            try{
            int iVal = new Integer(elements[i]);
            isInt = true;
            }catch(NumberFormatException e){
            //process exception
            isInt = false;
            }
            if(isInt){
            table.putItem.(new Item().withInt(currColumnName,iVal));
            }else{
            table.putItem.(new Item().withString(currColumnName),elements[i])); //don't even know whether there is a withString method
            }

            System.out.println("PutItem succeeded: " + elements[i]);

        } catch (Exception e) {
            System.err.println("Unable to add user: " + elements);
            System.err.println(e.getMessage());
            break;
        }
        }

    }

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

此示例假定您的第一行包含存储在数据库中的列名。您不必在任何地方编写它们,无论它们是int还是String都可以,因为程序中有检查(当然,这不是执行此操作的最有效方法,您可以编写更好的东西,也许是Molok的建议)

This example assumes that your first row contains the column names as stored in the DB. You don't have to write anywhere whether they an int or a String because there is a check in the program (granted this is not the most efficient way to do this and you may write something better, perhaps what Molok has suggested)

这篇关于解析CSV文件以填充数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆