You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. package schema
  2. import (
  3. "fmt"
  4. "github.com/parquet-go/parquet-go"
  5. "github.com/parquet-go/parquet-go/compress/zstd"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  7. "io"
  8. "os"
  9. "testing"
  10. )
  11. func TestWriteParquet(t *testing.T) {
  12. // create a schema_pb.RecordType
  13. recordType := NewRecordTypeBuilder().
  14. AddLongField("ID").
  15. AddLongField("CreatedAt").
  16. AddRecordField("Person", NewRecordTypeBuilder().
  17. AddStringField("zName").
  18. AddListField("emails", TypeString)).
  19. AddStringField("Company").Build()
  20. fmt.Printf("RecordType: %v\n", recordType)
  21. // create a parquet schema
  22. parquetSchema, err := ToParquetSchema("example", recordType)
  23. if err != nil {
  24. t.Fatalf("ToParquetSchema failed: %v", err)
  25. }
  26. fmt.Printf("ParquetSchema: %v\n", parquetSchema)
  27. parquet.PrintSchema(os.Stdout, "example", parquetSchema)
  28. filename := "example.parquet"
  29. testWritingParquetFile(t, filename, parquetSchema, recordType)
  30. total := testReadingParquetFile(t, filename, parquetSchema, recordType)
  31. if total != 128*1024 {
  32. t.Fatalf("total != 128*1024: %v", total)
  33. }
  34. }
  35. func testWritingParquetFile(t *testing.T, filename string, parquetSchema *parquet.Schema, recordType *schema_pb.RecordType) {
  36. // create a parquet file
  37. file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664)
  38. if err != nil {
  39. t.Fatalf("os.Open failed: %v", err)
  40. }
  41. defer file.Close()
  42. writer := parquet.NewWriter(file, parquetSchema, parquet.Compression(&zstd.Codec{Level: zstd.SpeedDefault}))
  43. rowBuilder := parquet.NewRowBuilder(parquetSchema)
  44. for i := 0; i < 128; i++ {
  45. rowBuilder.Reset()
  46. // generate random data
  47. AddRecordValue(rowBuilder, recordType, NewRecordValueBuilder().
  48. AddLongValue("ID", int64(1+i)).
  49. AddLongValue("CreatedAt", 2*int64(i)).
  50. AddRecordValue("Person", NewRecordValueBuilder().
  51. AddStringValue("zName", fmt.Sprintf("john_%d", i)).
  52. AddStringListValue("emails",
  53. fmt.Sprintf("john_%d@y.com", i),
  54. fmt.Sprintf("john_%d@g.com", i),
  55. fmt.Sprintf("john_%d@t.com", i))).
  56. AddStringValue("Company", fmt.Sprintf("company_%d", i)).Build())
  57. row := rowBuilder.Row()
  58. if err != nil {
  59. t.Fatalf("rowBuilder.Build failed: %v", err)
  60. }
  61. if _, err = writer.WriteRows([]parquet.Row{row}); err != nil {
  62. t.Fatalf("writer.Write failed: %v", err)
  63. }
  64. }
  65. if err = writer.Close(); err != nil {
  66. t.Fatalf("writer.WriteStop failed: %v", err)
  67. }
  68. }
  69. func testReadingParquetFile(t *testing.T, filename string, parquetSchema *parquet.Schema, recordType *schema_pb.RecordType) (total int) {
  70. // read the parquet file
  71. file, err := os.Open(filename)
  72. if err != nil {
  73. t.Fatalf("os.Open failed: %v", err)
  74. }
  75. defer file.Close()
  76. reader := parquet.NewReader(file, parquetSchema)
  77. rows := make([]parquet.Row, 128)
  78. for {
  79. rowCount, err := reader.ReadRows(rows)
  80. if err != nil {
  81. if err == io.EOF {
  82. break
  83. }
  84. t.Fatalf("reader.Read failed: %v", err)
  85. }
  86. for i := 0; i < rowCount; i++ {
  87. row := rows[i]
  88. // convert parquet row to schema_pb.RecordValue
  89. recordValue, err := ToRecordValue(recordType, row)
  90. if err != nil {
  91. t.Fatalf("ToRecordValue failed: %v", err)
  92. }
  93. fmt.Printf("RecordValue: %v\n", recordValue)
  94. }
  95. total += rowCount
  96. }
  97. fmt.Printf("total: %v\n", total)
  98. return
  99. }