ODPS的Java连接(2)

maven

<dependency>
   	<groupId>com.aliyun.odps</groupId>
    <artifactId>odps-jdbc</artifactId>
    <version>3.0.1</version>
</dependency>

正文

package org.apache.dolphinscheduler.common.utils;


import com.aliyun.odps.*;
import com.aliyun.odps.account.Account;
import com.aliyun.odps.account.AliyunAccount;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.task.SQLTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OdpsUtil {

    /**
     * odps连接
     * @param endPoint 地址
     * @param accessId 用户名
     * @param accessKey 密码
     * @param project 数据库名称
     * @return
     */
    public static Odps getConn(String endPoint, String accessId, String accessKey, String project) {
        Account account = new AliyunAccount(accessId, accessKey);
        Odps odps = new Odps(account);
        odps.setEndpoint(endPoint);
        odps.setDefaultProject(project);
        return odps;
    }

    /**
     * 获取OPDS的tables
     * @param stmt opds已连接对象
     * @return
     */
    public ArrayList<Map<String, String>> showTables(Odps stmt) {
        ArrayList<Map<String, String>> tablelist = new ArrayList<>();
        Tables tables = stmt.tables();
        for (Table table : tables) {
            Map<String, String> tableInfo = new HashMap<>();
            tableInfo.put("tableName", table.getName());
            tableInfo.put("remarks", table.getComment());
            tablelist.add(tableInfo);
        }
        return tablelist;
    }


    /**
     * 获取OPDS的字段colunmn的信息
     * @param stmt opds 已连接对象
     * @param tablename 表的名称
     * @return
     * @throws OdpsException
     */
    public ArrayList<Map<String, String>> showTableFields(Odps stmt, String tablename) throws OdpsException {
        ArrayList<Map<String, String>> tableFieldlist = new ArrayList<>();
        Table t = stmt.tables().get(tablename);
        t.reload();
        TableSchema schema = t.getSchema();
        List<Column> columns = schema.getColumns();
        for (Column column : columns) {
            Map<String, String> fieldInfo = new HashMap<>();
            fieldInfo.put("columnName", column.getName());
            fieldInfo.put("remarks", column.getComment());
            tableFieldlist.add(fieldInfo);
        }
        return tableFieldlist;
    }

    /**
     * 获取表的数据预览
     * @param stmt
     * @param tablename
     * @throws OdpsException
     */
    public void getTableData(Odps stmt, String tablename) throws OdpsException {
        String sql = "select * from " + tablename + " limit 5;";
        Instance i = SQLTask.run(stmt, sql);
        i.waitForSuccess();
        List<Record> records = SQLTask.getResult(i);
        for (Record r : records) {
            String fielddata1 = r.getString(1);
            String fielddata = r.getString("name");
        }
    }

}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值