Skip to content

Commit

Permalink
chore: add example of create external table (#39)
Browse files Browse the repository at this point in the history
* chore: add example of create external table

* chore: remove ci on pull request

* chore: remove adding jars in example
  • Loading branch information
dingxin-tech authored Oct 30, 2024
1 parent acf10f7 commit 3446122
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ on:
workflow_dispatch: # 支持手动触发
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

Expand Down
97 changes: 97 additions & 0 deletions examples/sdk/table/create_external_table/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 3446122

Please sign in to comment.