diff --git a/README.md b/README.md index 128e0e99..52579ccd 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ schema: - name: age type: int index: 2 + default: 1 - name: name type: string index: 1 @@ -223,6 +224,7 @@ Each tag contains the following two properties: * `name`: **Required**. The property name, must be the same with the tag property in Nebula Graph. * `type`: **Optional**. The property type, currently `bool`, `int`, `float`, `double`, `string`, `time`, `timestamp`, `date`, `datetime`, `geography`, `geography(point)`, `geography(linestring)` and `geography(polygon)` are supported. * `index`: **Optional**. The column number in the CSV file. + * `default`: **Optional**. use this default value if the column is missing in CSV file > **NOTE**: The properties in the preceding `prop` parameter must be sorted in the **same** way as in the CSV data file. diff --git a/README_zh-CN.md b/README_zh-CN.md index 29d10ed9..41b76c36 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -170,6 +170,7 @@ schema: - name: age type: int index: 2 + default: 18 - name: name type: string index: 1 @@ -196,6 +197,7 @@ schema: - `name`:**必填**。属性名称,同 Nebula Graph 中创建的 TAG 的属性名称一致。 - `type`:**必填**。属性类型,目前支持 `bool`、`int`、`float`、`double`、`timestamp`、`string`、`geography`、`geography(point)`、`geography(linestring)`和`geography(polygon)` 几种类型。 - `index`:**可选**。在 CSV 文件中的列标。 + - `default`: **可选**。如果在csv文件中该列缺失的时候,使用该默认值 > **注意**:上述 `props` 中的属性描述**顺序**必须同数据文件中的对应数据排列顺序一致。 diff --git a/pkg/config/config.go b/pkg/config/config.go index 9db55334..3194e21e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -43,9 +43,10 @@ type NebulaClientSettings struct { } type Prop struct { - Name *string `json:"name" yaml:"name"` - Type *string `json:"type" yaml:"type"` - Index *int `json:"index" yaml:"index"` + Name *string `json:"name" yaml:"name"` + Type *string `json:"type" yaml:"type"` + Index *int `json:"index" yaml:"index"` + DefaultVal *string `json:"default" yaml:"default"` } type VID struct { @@ -815,6 +816,9 @@ func (p *Prop) FormatValue(record base.Record) (string, error) { return "", fmt.Errorf("Prop index %d out range %d of record(%v)", *p.Index, len(record), record) } r := record[*p.Index] + if r == "" && *p.DefaultVal != "" { + r = *p.DefaultVal + } if p.IsStringType() { return fmt.Sprintf("%q", r), nil } @@ -830,7 +834,7 @@ func (p *Prop) FormatValue(record base.Record) (string, error) { } func (p *Prop) String(prefix string) string { - return fmt.Sprintf("%s.%s:%s", prefix, *p.Name, *p.Type) + return fmt.Sprintf("%s.%s:%s:%s", prefix, *p.Name, *p.Type, *p.DefaultVal) } func (p *Prop) validateAndReset(prefix string, val int) error { @@ -845,6 +849,11 @@ func (p *Prop) validateAndReset(prefix string, val int) error { return fmt.Errorf("Invalid prop index: %d, name: %s, type: %s", *p.Index, *p.Name, *p.Type) } } + + if p.DefaultVal == nil { + v := "" + p.DefaultVal = &v + } return nil } diff --git a/pkg/reader/batchmgr.go b/pkg/reader/batchmgr.go index a8e63633..acd22232 100644 --- a/pkg/reader/batchmgr.go +++ b/pkg/reader/batchmgr.go @@ -107,31 +107,33 @@ func (bm *BatchMgr) InitSchema(header base.Record, runnerLogger *logger.RunnerLo } func (bm *BatchMgr) addVertexTags(r string, i int) { - columnName, columnType := bm.parseProperty(r) + columnName, columnType, defaultVal := bm.parseProperty(r) tagName, prop := bm.parseTag(columnName) if tagName == "" { return } tag := bm.getOrCreateVertexTagByName(tagName) p := config.Prop{ - Name: &prop, - Type: &columnType, - Index: &i, + Name: &prop, + Type: &columnType, + Index: &i, + DefaultVal: &defaultVal, } tag.Props = append(tag.Props, &p) } func (bm *BatchMgr) addEdgeProps(r string, i int) { - columnName, columnType := bm.parseProperty(r) + columnName, columnType, defaultVal := bm.parseProperty(r) res := strings.SplitN(columnName, ".", 2) prop := res[0] if len(res) > 1 { prop = res[1] } p := config.Prop{ - Name: &prop, - Type: &columnType, - Index: &i, + Name: &prop, + Type: &columnType, + Index: &i, + DefaultVal: &defaultVal, } bm.Schema.Edge.Props = append(bm.Schema.Edge.Props, &p) } @@ -191,14 +193,25 @@ func (bm *BatchMgr) parseTag(s string) (tag, field string) { return res[0], res[1] } -func (bm *BatchMgr) parseProperty(r string) (columnName, columnType string) { - res := strings.SplitN(r, ":", 2) +func (bm *BatchMgr) parseProperty(r string) (columnName, columnType, defaultVal string) { + res := strings.SplitN(r, ":", 3) - if len(res) == 1 || res[1] == "" || !base.IsValidType(res[1]) { - return res[0], "string" + if len(res) == 1 { + columnType = "string" } else { - return res[0], res[1] + columnType = res[1] } + if !base.IsValidType(columnType) { + columnType = "string" + } + + if len(res) > 2 { + defaultVal = res[2] + } else { + defaultVal = "" + } + + return res[0], columnType, defaultVal } func (bm *BatchMgr) Add(data base.Data, runnerLogger *logger.RunnerLogger) error {