Skip to content
代码片段 群组 项目
未验证 提交 04f5e12b 编辑于 作者: Damon Douglas's avatar Damon Douglas 提交者: GitHub
浏览文件

[BEAM-9681] Add textio.Read lesson to Go SDK katas (#12941)

上级 443cf83b
No related branches found
No related tags found
无相关合并请求
显示
355 个添加2 个删除
...@@ -95,7 +95,7 @@ rat { ...@@ -95,7 +95,7 @@ rat {
"learning/katas/**/section-remote-info.yaml", "learning/katas/**/section-remote-info.yaml",
"learning/katas/**/lesson-remote-info.yaml", "learning/katas/**/lesson-remote-info.yaml",
"learning/katas/**/task-remote-info.yaml", "learning/katas/**/task-remote-info.yaml",
"learning/katas/*/IO/**/*.txt", "learning/katas/**/*.txt",
// test p8 file for SnowflakeIO // test p8 file for SnowflakeIO
"sdks/java/io/snowflake/src/test/resources/invalid_test_rsa_key.p8", "sdks/java/io/snowflake/src/test/resources/invalid_test_rsa_key.p8",
......
...@@ -26,3 +26,4 @@ content: ...@@ -26,3 +26,4 @@ content:
- introduction - introduction
- core_transforms - core_transforms
- common_transforms - common_transforms
- io
id: 70387 id: 70387
update_date: Fri, 28 Aug 2020 23:34:45 UTC update_date: Wed, 30 Sep 2020 15:51:15 UTC
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
content:
- textio
id: 150333
update_date: Wed, 30 Sep 2020 15:54:59 UTC
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
content:
- read
id: 413047
update_date: Wed, 30 Sep 2020 15:55:01 UTC
unit: 402524
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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.
package main
import (
"beam.apache.org/learning/katas/io/textio/read/pkg/task"
"beam.apache.org/learning/katas/io/textio/read/testdata"
"context"
"github.com/apache/beam/sdks/go/pkg/beam"
"github.com/apache/beam/sdks/go/pkg/beam/log"
"github.com/apache/beam/sdks/go/pkg/beam/x/beamx"
"github.com/apache/beam/sdks/go/pkg/beam/x/debug"
)
var filePath = testdata.Path("countries.txt")
func main() {
ctx := context.Background()
p, s := beam.NewPipelineWithRoot()
countries := task.Read(s, filePath)
output := task.ApplyTransform(s, countries)
debug.Print(s, output)
err := beamx.Run(ctx, p)
if err != nil {
log.Exitf(ctx, "Failed to execute job: %v", err)
}
}
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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.
package task
import (
"github.com/apache/beam/sdks/go/pkg/beam"
"github.com/apache/beam/sdks/go/pkg/beam/io/textio"
"strings"
)
// Read reads from filename(s) specified by a glob string and a returns a PCollection<string>.
func Read(s beam.Scope, glob string) beam.PCollection {
return textio.Read(s, glob)
}
// ApplyTransform converts to uppercase all elements in a PCollection<string>.
func ApplyTransform(s beam.Scope, input beam.PCollection) beam.PCollection {
return beam.ParDo(s, strings.ToUpper, input)
}
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
type: edu
files:
- name: test/task_test.go
visible: false
- name: pkg/task/task.go
visible: true
placeholders:
- offset: 1087
length: 20
placeholder_text: TODO()
- offset: 1275
length: 37
placeholder_text: TODO()
- name: cmd/main.go
visible: true
- name: testdata/countries.txt
visible: true
- name: testdata/path.go
visible: true
id: 1598733
update_date: Wed, 30 Sep 2020 15:55:05 UTC
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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.
-->
# I/O Read
When you create a pipeline, you often need to read data from some external source, such as a file
or a database. Likewise, you may want your pipeline to output its result data to an external
storage system. Beam provides read and write transforms for a number of common data storage types.
Package [textio](https://godoc.org/github.com/apache/beam/sdks/go/pkg/beam/io/textio)
contains transforms for reading and writing text files.
[textio.Read](https://godoc.org/github.com/apache/beam/sdks/go/pkg/beam/io/textio#Read) reads a set of files and
returns the lines as a PCollection<string>.
**Kata:** Read the 'countries.txt' file and convert each country name into uppercase.
<div class="hint">
Use the <a href="https://godoc.org/github.com/apache/beam/sdks/go/pkg/beam/io/textio#Read">
textio.Read</a> method.
</div>
<div class="hint">
Refer to the Beam Programming Guide
<a href="https://beam.apache.org/documentation/programming-guide/#pipeline-io-reading-data">
"Reading input data"</a> section for more information.
</div>
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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.
package test
import (
"beam.apache.org/learning/katas/io/textio/read/pkg/task"
"beam.apache.org/learning/katas/io/textio/read/testdata"
"github.com/apache/beam/sdks/go/pkg/beam"
_ "github.com/apache/beam/sdks/go/pkg/beam/io/filesystem/local"
"github.com/apache/beam/sdks/go/pkg/beam/io/textio"
"github.com/apache/beam/sdks/go/pkg/beam/testing/passert"
"github.com/apache/beam/sdks/go/pkg/beam/testing/ptest"
"testing"
)
var filePath = testdata.Path("countries.txt")
func TestRead(t *testing.T) {
p, s := beam.NewPipelineWithRoot()
tests := []struct {
input string
want []interface{}
}{
{
input: filePath,
want: []interface{}{
"Singapore",
"United States",
"Australia",
"England",
"France",
"China",
"Indonesia",
"Mexico",
"Germany",
"Japan",
},
},
}
for _, tt := range tests {
got := task.Read(s, tt.input)
passert.Equals(s, got, tt.want...)
if err := ptest.Run(p); err != nil {
t.Error(err)
}
}
}
func TestApplyTransform(t *testing.T) {
p, s := beam.NewPipelineWithRoot()
tests := []struct {
input beam.PCollection
want []interface{}
} {
{
input: textio.Read(s, filePath),
want: []interface{}{
"SINGAPORE",
"UNITED STATES",
"AUSTRALIA",
"ENGLAND",
"FRANCE",
"CHINA",
"INDONESIA",
"MEXICO",
"GERMANY",
"JAPAN",
},
},
}
for _, tt := range tests {
got := task.ApplyTransform(s, tt.input)
passert.Equals(s, got, tt.want...)
if err := ptest.Run(p); err != nil {
t.Error(err)
}
}
}
\ No newline at end of file
Singapore
United States
Australia
England
France
China
Indonesia
Mexico
Germany
Japan
\ No newline at end of file
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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.
package testdata
import (
"path/filepath"
"runtime"
)
var (
// dataDir is the absolute path to the directory of this package
dataDir string
)
func init() {
_, runtimeFilePath, _, _ := runtime.Caller(0)
dataDir = filepath.Dir(runtimeFilePath)
}
// Path returns the absolute path to name relative to
// github.com/apache/beam/learning/katas/go/io/textio/read/testdata
func Path(name string) string {
if filepath.IsAbs(name) {
return name
}
return filepath.Join(dataDir, name)
}
\ No newline at end of file
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册