From 3446122872fe9f9ba9fe29e9ef99b709cc96905d Mon Sep 17 00:00:00 2001 From: Jason Zhang <50667759+dingxin-tech@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:57:55 +0800 Subject: [PATCH] chore: add example of create external table (#39) * chore: add example of create external table * chore: remove ci on pull request * chore: remove adding jars in example --- .github/workflows/ci.yml | 2 - .../sdk/table/create_external_table/main.go | 97 +++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 examples/sdk/table/create_external_table/main.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e3c93d4..5392960 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,8 +7,6 @@ on: workflow_dispatch: # 支持手动触发 push: branches: [ "master" ] - pull_request: - branches: [ "master" ] jobs: diff --git a/examples/sdk/table/create_external_table/main.go b/examples/sdk/table/create_external_table/main.go new file mode 100644 index 0000000..135b7e2 --- /dev/null +++ b/examples/sdk/table/create_external_table/main.go @@ -0,0 +1,97 @@ +package main + +import ( + "github.com/aliyun/aliyun-odps-go-sdk/odps" + "github.com/aliyun/aliyun-odps-go-sdk/odps/account" + "github.com/aliyun/aliyun-odps-go-sdk/odps/datatype" + "github.com/aliyun/aliyun-odps-go-sdk/odps/tableschema" + "log" +) + +func main() { + // Specify the ini file path + configPath := "./config.ini" + conf, err := odps.NewConfigFromIni(configPath) + + if err != nil { + log.Fatalf("%+v", err) + } + + aliAccount := account.NewAliyunAccount(conf.AccessId, conf.AccessKey) + odpsIns := odps.NewOdps(aliAccount, conf.Endpoint) + // Set the Default Maxcompute project used By Odps instance + odpsIns.SetDefaultProjectName(conf.ProjectName) + + // create external table if not exists go_sdk_regression_testing.`testCreateExternalTableWithUserDefinedStorageHandler` ( + // `a` STRING , + // `b` STRING , + // `c` BIGINT + //) + // comment 'External table using user defined TextStorageHandler' + // partitioned by (`dt` STRING) + // stored by 'com.aliyun.odps.udf.example.text.TextStorageHandler' + // with serdeproperties('odps.text.option.delimiter'='|', 'my.own.option'='value') + // location 'MOCKoss://full/uri/path/to/oss/directory/' + // lifecycle 10; + + tableName := "testCreateExternalTableWithUserDefinedStorageHandler" + + c1 := tableschema.Column{ + Name: "a", + Type: datatype.StringType, + } + + c2 := tableschema.Column{ + Name: "b", + Type: datatype.StringType, + } + + c3 := tableschema.Column{ + Name: "c", + Type: datatype.BigIntType, + } + + // partition column + pc := tableschema.Column{ + Name: "dt", + Type: datatype.StringType, + } + + sb := tableschema.NewSchemaBuilder() + + sb.Name(tableName). // table name + Columns(c1, c2, c3). // columns + PartitionColumns(pc). // partition columns + Location("MOCKoss://full/uri/path/to/oss/directory/"). + StorageHandler("com.aliyun.odps.udf.example.text.TextStorageHandler"). + Comment("External table using user defined TextStorageHandler"). + Lifecycle(10) + + tablesIns := odpsIns.Tables() + + schema := sb.Build() + + // 定义 properties 映射 + serDeProperties := map[string]string{ + "odps.text.option.delimiter": "|", + "my.own.option": "value", + } + + // 定义 hints 映射 + hints := map[string]string{ + "odps.sql.preparse.odps2": "lot", + "odps.sql.planner.mode": "lot", + "odps.sql.planner.parser.odps2": "true", + "odps.sql.ddl.odps2": "true", + "odps.compiler.output.format": "lot,pot", + } + + sql, err := schema.ToExternalSQLString(odpsIns.DefaultProjectName(), "", true, serDeProperties, nil) + print(sql) + + err = tablesIns.CreateExternal(schema, true, serDeProperties, nil, hints, nil) + + if err != nil { + log.Fatalf("%+v", err) + } +}