type ParquetMeta struct {
StandardHeader parquetMetaHeader
}
type parquetMetaHeader struct {
Sn string
Time string
ColName []string
ColType []string
}
type AccParquetRowData struct {
A float64 `parquet:"name=a, type=FLOAT"`
B float64 `parquet:"name=b, type=FLOAT"`
C float64 `parquet:"name=c, type=FLOAT"`
D float64 `parquet:"name=d, type=FLOAT"`
E float64 `parquet:"name=e, type=FLOAT"`
F float64 `parquet:"name=f, type=FLOAT"`
G float64 `parquet:"name=g, type=FLOAT"`
H float64 `parquet:"name=h, type=FLOAT"`
I float64 `parquet:"name=i, type=FLOAT"`
}
func WriteMetaDataByStruct() (string, error) {
fileTime := time.Now().Format("2006-01-02_15-04-05")
path := "./"
metaDataFileName := fileTime + ".toml"
buf := new(bytes.Buffer)
standardMeta := new(ParquetMeta)
standardMeta.StandardHeader.Sn = "12311"
standardMeta.StandardHeader.Time = "20210425T163100Z"
typeOfAps := reflect.TypeOf(AccParquetRowData{})
var fieldName, fieldType []string
for i := 0; i < typeOfAps.NumField(); i++ {
field := typeOfAps.Field(i)
fmt.Println(field.Name, "======", field.Type.Name())
fieldName = append(fieldName, field.Name)
fieldType = append(fieldType, field.Type.Name())
}
standardMeta.StandardHeader.ColName = fieldName
standardMeta.StandardHeader.ColType = fieldType
if err := toml.NewEncoder(buf).Encode(standardMeta); err != nil {
fmt.Println("写入TOML文件标准部分错误", err)
return "", err
}
tomlFilePath := utils.CreateFileFormByte(buf.Bytes(), path, metaDataFileName)
fmt.Println(tomlFilePath)
return tomlFilePath, nil
}
func CreateDateDir(basePath string) error {
if _, err := os.Stat(basePath); os.IsNotExist(err) {
err = os.MkdirAll(basePath, 0777)
if err != nil {
return err
}
}
return nil
}
func CreateFileFormByte(data []byte, path, fileName string) string {
err := CreateDateDir(path)
if err != nil {
fmt.Printf("创建文件夹错误,%v", err)
}
path = strings.TrimSuffix(path, "/")
fileFullPath := path + "/" + fileName
writeFileErr := ioutil.WriteFile(fileFullPath, data, 0666)
if writeFileErr != nil {
fmt.Printf("创建文件错误,%v", writeFileErr)
}
return fileFullPath
}
golang--操作toml文件
于 2022-05-19 11:07:30 首次发布