Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support default value while import #125

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ schema:
- name: age
type: int
index: 2
default: 1
- name: name
type: string
index: 1
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ schema:
- name: age
type: int
index: 2
default: 18
- name: name
type: string
index: 1
Expand All @@ -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` 中的属性描述**顺序**必须同数据文件中的对应数据排列顺序一致。

Expand Down
17 changes: 13 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
39 changes: 26 additions & 13 deletions pkg/reader/batchmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down