Add new config for csv column explicit type conversion (#4781)
This commit is contained in:
@@ -27,6 +27,11 @@ values.
|
||||
## If `csv_header_row_count` is set to 0, this config must be used
|
||||
csv_column_names = []
|
||||
|
||||
## For assigning explicit data types to columns.
|
||||
## Supported types: "int", "float", "bool", "string".
|
||||
## If this is not specified, type conversion will be done on the types above.
|
||||
csv_column_types = []
|
||||
|
||||
## Indicates the number of rows to skip before looking for header information.
|
||||
csv_skip_rows = 0
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ type Parser struct {
|
||||
Comment string
|
||||
TrimSpace bool
|
||||
ColumnNames []string
|
||||
ColumnTypes []string
|
||||
TagColumns []string
|
||||
MeasurementColumn string
|
||||
TimestampColumn string
|
||||
@@ -148,6 +149,40 @@ outer:
|
||||
}
|
||||
}
|
||||
|
||||
// Try explicit conversion only when column types is defined.
|
||||
if len(p.ColumnTypes) > 0 {
|
||||
// Throw error if current column count exceeds defined types.
|
||||
if i >= len(p.ColumnTypes) {
|
||||
return nil, fmt.Errorf("column type: column count exceeded")
|
||||
}
|
||||
|
||||
var val interface{}
|
||||
var err error
|
||||
|
||||
switch p.ColumnTypes[i] {
|
||||
case "int":
|
||||
val, err = strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("column type: parse int error %s", err)
|
||||
}
|
||||
case "float":
|
||||
val, err = strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("column type: parse float error %s", err)
|
||||
}
|
||||
case "bool":
|
||||
val, err = strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("column type: parse bool error %s", err)
|
||||
}
|
||||
default:
|
||||
val = value
|
||||
}
|
||||
|
||||
recordFields[fieldName] = val
|
||||
continue
|
||||
}
|
||||
|
||||
// attempt type conversions
|
||||
if iValue, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
recordFields[fieldName] = iValue
|
||||
|
||||
@@ -147,6 +147,18 @@ func TestValueConversion(t *testing.T) {
|
||||
|
||||
//deep equal fields
|
||||
require.Equal(t, expectedMetric.Fields(), returnedMetric.Fields())
|
||||
|
||||
// Test explicit type conversion.
|
||||
p.ColumnTypes = []string{"float", "int", "bool", "string"}
|
||||
|
||||
metrics, err = p.Parse([]byte(testCSV))
|
||||
require.NoError(t, err)
|
||||
|
||||
returnedMetric, err2 = metric.New(metrics[0].Name(), metrics[0].Tags(), metrics[0].Fields(), time.Unix(0, 0))
|
||||
require.NoError(t, err2)
|
||||
|
||||
//deep equal fields
|
||||
require.Equal(t, expectedMetric.Fields(), returnedMetric.Fields())
|
||||
}
|
||||
|
||||
func TestSkipComment(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user