forked from raystack/meteor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactored maxcompute client with interface and added tests
- Loading branch information
1 parent
c1206de
commit 03a843b
Showing
7 changed files
with
780 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package maxcompute | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aliyun/aliyun-odps-go-sdk/odps" | ||
"github.com/aliyun/aliyun-odps-go-sdk/odps/account" | ||
) | ||
|
||
type MaxComputeClient struct { | ||
client *odps.Odps | ||
project *odps.Project | ||
} | ||
|
||
func NewMaxComputeClient(config Config) *MaxComputeClient { | ||
aliAccount := account.NewAliyunAccount(config.AccessKey.ID, config.AccessKey.Secret) | ||
client := odps.NewOdps(aliAccount, config.EndpointProject) | ||
client.SetDefaultProjectName(config.ProjectName) | ||
|
||
project := client.Project(config.ProjectName) | ||
|
||
return &MaxComputeClient{ | ||
client: client, | ||
project: project, | ||
} | ||
} | ||
|
||
func (c *MaxComputeClient) ListSchema(ctx context.Context) (schemas []*odps.Schema, err error) { | ||
err = c.project.Schemas().List(func(schema *odps.Schema, err2 error) { | ||
if err2 != nil { | ||
err = err2 | ||
return | ||
} | ||
schemas = append(schemas, schema) | ||
}) | ||
|
||
return | ||
} | ||
|
||
func (c *MaxComputeClient) ListTable(ctx context.Context, schemaName string) (tables []*odps.Table, err error) { | ||
t := odps.NewTables(c.client, c.project.Name(), schemaName) | ||
t.List( | ||
func(table *odps.Table, err2 error) { | ||
if err2 != nil { | ||
err = err2 | ||
return | ||
} | ||
tables = append(tables, table) | ||
}, | ||
) | ||
return | ||
} | ||
|
||
func (c *MaxComputeClient) GetTable(ctx context.Context, table *odps.Table) (*odps.Table, error) { | ||
err := table.Load() | ||
if err != nil { | ||
isView := table.Schema().IsVirtualView || table.Schema().IsMaterializedView | ||
isLoaded := table.IsLoaded() | ||
if !isView || (isView && !isLoaded) { | ||
return nil, err | ||
} | ||
} | ||
return table, nil | ||
} |
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
Oops, something went wrong.