在MonetDB中加载具有空值的CSV

时间:2017-04-14 18:28:19

标签: database csv monetdb

我正在尝试加载一个CSV文件,其中应为NULL的值只是空白。例如,请考虑以下架构:

1.0,1.2,1.3
1.4,,1.5
1.6,,,
1.9,2.0,1.2

我正在尝试加载一个看起来像这样的文件(某些行可能没有NULL值,其他行可能有几个)。

 COPY INTO table from '/path/to/file.csv' USING DELIMITERS ',','\n','"' NULL AS '';

我希望数据库中的空字段(例如第二行中的第二个字段)为NULL。

我使用以下命令加载csv文件:

Failed to import table line 2 field b 'double' expected in ''

我收到以下错误:

impl<'de> Deserialize<'de> for EventKind {
    fn deserialize<D>(deserializer: D) -> result::Result<EventKind, D::Error>
        where D: Deserializer<'de>
    {
        struct FieldVisitor {
            min: usize,
        };

        impl<'de> Visitor<'de> for FieldVisitor {
            type Value = EventKind;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                write!(formatter, "a string containing at least {} bytes", self.min)
            }

            fn visit_str<E>(self, value: &str) -> result::Result<EventKind, E>
                where E: serde::de::Error
            {
                let kind = match value {
                    "request" => EventKind::Request,
                    "ready" => EventKind::Ready,
                    "next" => EventKind::Next,
                    "reject" => EventKind::Reject,
                    "fail" => EventKind::Fail,
                    "done" => EventKind::Done,
                    "cancel" => EventKind::Cancel,
                    "suspended" => EventKind::Suspended,
                    s => {
                        return Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(s),
                                                                   &self));
                    }
                };
                Ok(kind)
            }
        }
        deserializer.deserialize_str(FieldVisitor { min: 4 })
    }
}

enum EventKind {
    Request,
    Ready,
    Next,
    Reject,
    Fail,
    Done,
    Cancel,
    Suspended,
}

impl Serialize for EventKind {
    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
        where S: Serializer
    {
        let kind = match *self {
            EventKind::Request => "request",
            EventKind::Ready => "ready",
            EventKind::Next => "next",
            EventKind::Reject => "reject",
            EventKind::Fail => "fail",
            EventKind::Done => "done",
            EventKind::Cancel => "cancel",
            EventKind::Suspended => "suspended",
        };
        serializer.serialize_str(kind)
    }
}

我是否应该将任何其他内容传递给load命令(NULL AS&#39;&#39;除外)?

1 个答案:

答案 0 :(得分:0)

不,你似乎正确地这样做了。我试图复制你的测试,它对我有用。如果您忽略NULL AS&#39;&#39;那么您的错误似乎只会发生。你答案的一部分。

您提供的代码中存在一些小错误,正确的代码是:

CREATE TABLE test (
    a float,
    b float,
    c float
);

COPY INTO test from '/path/to/file.csv' USING DELIMITERS ',','\n','"' NULL AS '';

在您的档案中,您应该删除额外的&#39;,&#39;字符:

1.0,1.2,1.3
1.4,,1.5
1.6,,
1.9,2.0,1.2

如果失败,请尝试更新您的MonetDB版本。