閱讀860 返回首頁    go 阿裏雲 go 技術社區[雲棲]


用Java代碼調用MaxCompute

MaxCompute的客戶端的工具odpscmd是好東西,什麼都能幹。但是它不方便在自己的代碼做很好的集成,畢竟它是個Shell腳本。那有什麼辦法把MaxCompute的作業、設置和自己的代碼做無縫集成呢,MaxComput SDK就能幹這個。本文就實際的工作中最常見的幾個場景,做一些示例。詳細的使用可以在Maven上下到SDK的文檔說明
其實這裏麵的很多寫法都是文檔裏有的,或者在幫助文檔裏有寫過類似的例子。這裏就做算是做個整理吧。

對象操作

其實官方的SDK文檔裏,對這方麵的介紹是最多的了,可以參考這裏。這裏我再針對實際場景裏的比較多的創建表和分區做個例子,相信看完這些後對這方麵就沒有疑惑了

        String access_id = "your access id";
        String access_key = "your access key";
        String endpoint = "https://service.odps.aliyun.com/api";
        String project = "testproject";
        String tableName = "testCreate";
        String partition = "ds='20170614'";

        Account account = new AliyunAccount(access_id, access_key);
        Odps odps = new Odps(account);
        odps.setEndpoint(endpoint);
        odps.setDefaultProject(project);


        TableSchema schema = new TableSchema();
        schema.addColumn(new Column("col_str",OdpsType.STRING,"column string"));
        schema.addColumn(new Column("col_datetime",OdpsType.DATETIME,"column datetime"));
        schema.addColumn(new Column("col_int",OdpsType.BIGINT,"column bigint"));
        schema.addColumn(new Column("col_double",OdpsType.DOUBLE,"column double"));
        schema.addColumn(new Column("col_boolean",OdpsType.BOOLEAN,"column boolean"));
        schema.addPartitionColumn(new Column("ds",OdpsType.STRING,"partition column"));


        PartitionSpec ps= new PartitionSpec(partition);

        try {
            odps.tables().create(tableName, schema);
            odps.tables().get(tableName).createPartition(ps);
        } catch (OdpsException e) {
            e.printStackTrace();
        }


odps@ testproject>desc testCreate;

+------------------------------------------------------------------------------------+
| Owner: ALIYUN$cloudtecengr@aliyun.com | Project: testproject                                  |
| TableComment:                                                                      |
+------------------------------------------------------------------------------------+
| CreateTime:               2017-06-14 20:00:48                                      |
| LastDDLTime:              2017-06-14 20:00:48                                      |
| LastModifiedTime:         2017-06-14 20:00:48                                      |
+------------------------------------------------------------------------------------+
| InternalTable: YES      | Size: 0                                                  |
+------------------------------------------------------------------------------------+
| Native Columns:                                                                    |
+------------------------------------------------------------------------------------+
| Field           | Type       | Label | Comment                                     |
+------------------------------------------------------------------------------------+
| col_str         | string     |       | column string                               |
| col_datetime    | datetime   |       | column datetime                             |
| col_int         | bigint     |       | column bigint                               |
| col_double      | double     |       | column double                               |
| col_boolean     | boolean    |       | column boolean                              |
+------------------------------------------------------------------------------------+
| Partition Columns:                                                                 |
+------------------------------------------------------------------------------------+
| ds              | string     | partition column                                    |
+------------------------------------------------------------------------------------+

OK
odps@ testproject>show partitions  testCreate;

ds=20170614

OK

SQL

上文的提到的SDK文檔裏有個例子,可以用來提交SQL。

另外前麵提到的創建表的操作,也可以用這裏的SQLTask跑Create Table的SQL來實現。

說到SQL就少不了要說JDBC,目前有基於SQLTask開源做了個JDBC的實現,可以參考這裏

授權

授權不能直接用SQLTask來做,不過有類似的方法

    com.aliyun.odps.security.SecurityManager securityManager = odps.projects().get().getSecurityManager();
    String res = securityManager.runQuery("grant all on table wc_in to user aliyun$xxxxxxxxx@aliyun.com;", false);

MapReduce

針對這個問題,我寫了一篇文檔可以參考下

Graph

圖計算我們把這個功能寫進了產品文檔,可以參考這裏。這裏的ss.setLocalRun(false);如果是Ture就是本地調試,如果是false就是在雲上跑作業。

Tunnel

Tunnel可以參考Tunnel SDK對應章節,本文不再展開說明。

最後更新:2017-06-14 23:04:20

  上一篇:go  VPC SLB ECS RDS KvStore快速入門
  下一篇:go  linux DNS解析異常的排查