-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: coldWater <254244460@qq.com> Signed-off-by: Jiyong Huang <huangjy@emqx.io> Co-authored-by: coldWater <254244460@qq.com>
- Loading branch information
1 parent
716d004
commit c11b697
Showing
7 changed files
with
205 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2021-2024 EMQ Technologies Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build parquet || full | ||
|
||
package reader | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"os" | ||
|
||
"github.com/parquet-go/parquet-go" | ||
|
||
"github.com/lf-edge/ekuiper/contract/v2/api" | ||
"github.com/lf-edge/ekuiper/v2/pkg/modules" | ||
) | ||
|
||
func init() { | ||
modules.RegisterFileStreamReader("parquet", func(ctx api.StreamContext) modules.FileStreamReader { | ||
return &ParquetReader{} | ||
}) | ||
} | ||
|
||
type ParquetReader struct { | ||
pf *parquet.File | ||
groups []parquet.RowGroup | ||
curGroup int | ||
rowsReader parquet.Rows | ||
} | ||
|
||
func (pr *ParquetReader) Provision(ctx api.StreamContext, props map[string]any) error { | ||
return nil | ||
} | ||
|
||
func (pr *ParquetReader) Bind(ctx api.StreamContext, fr io.Reader, _ int) (err error) { | ||
f, ok := fr.(*os.File) | ||
if !ok { | ||
return errors.New("parquet reader needs a file") | ||
} | ||
info, err := f.Stat() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pr.pf, err = parquet.OpenFile(f, info.Size()) | ||
if err != nil { | ||
return err | ||
} | ||
pr.groups = pr.pf.RowGroups() | ||
return nil | ||
} | ||
|
||
func (pr *ParquetReader) Read(_ api.StreamContext) (any, error) { | ||
var row [1]parquet.Row | ||
for pr.curGroup < len(pr.groups) { | ||
group := pr.groups[pr.curGroup] | ||
if pr.rowsReader == nil { | ||
pr.rowsReader = group.Rows() | ||
} | ||
|
||
_, err := pr.rowsReader.ReadRows(row[:]) | ||
switch { | ||
case errors.Is(err, io.EOF): | ||
pr.curGroup++ | ||
err = pr.rowsReader.Close() | ||
pr.rowsReader = nil | ||
if err != nil { | ||
return nil, err | ||
} | ||
continue | ||
case err != nil: | ||
return nil, err | ||
} | ||
|
||
m := make(map[string]any) | ||
err = pr.pf.Schema().Reconstruct(&m, row[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} | ||
return nil, io.EOF | ||
} | ||
|
||
func (pr *ParquetReader) IsBytesReader() bool { | ||
return false | ||
} | ||
|
||
func (pr *ParquetReader) Close(_ api.StreamContext) error { | ||
return nil | ||
} | ||
|
||
var _ modules.FileStreamReader = &ParquetReader{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 EMQ Technologies Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build parquet || full | ||
|
||
package file | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
_ "github.com/lf-edge/ekuiper/v2/internal/io/file/reader" | ||
|
||
"github.com/lf-edge/ekuiper/contract/v2/api" | ||
"github.com/lf-edge/ekuiper/v2/pkg/mock" | ||
"github.com/lf-edge/ekuiper/v2/pkg/model" | ||
"github.com/lf-edge/ekuiper/v2/pkg/timex" | ||
) | ||
|
||
func TestParquet(t *testing.T) { | ||
path, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
path = filepath.Join(path, "test") | ||
meta := map[string]interface{}{ | ||
"file": filepath.Join(path, "parquet", "simple.parq"), | ||
} | ||
mc := timex.Clock | ||
exp := []api.MessageTuple{ | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(1), "name": "user1"}, meta, mc.Now()), | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(2), "name": "user2"}, meta, mc.Now()), | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(7), "name": "user7"}, meta, mc.Now()), | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(8), "name": "user8"}, meta, mc.Now()), | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(10), "name": "user10"}, meta, mc.Now()), | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(12), "name": "user12"}, meta, mc.Now()), | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(15), "name": "user15"}, meta, mc.Now()), | ||
model.NewDefaultSourceTuple(map[string]interface{}{"id": int64(16), "name": "user16"}, meta, mc.Now()), | ||
} | ||
r := GetSource() | ||
mock.TestSourceConnector(t, r, map[string]any{ | ||
"fileType": "parquet", | ||
"path": path, | ||
"datasource": "parquet/simple.parq", | ||
}, exp, func() { | ||
// do nothing | ||
}) | ||
} |
Binary file not shown.