JAVA-SDK-调用dataworks-API接口案例


新增一个maven项目

mvn archetype:generate \
-DgroupId=org.darebeat \
-DartifactId=dataworks \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeCatalog=local \
-DinteractiveMode=false

配置pop.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.darebeat</groupId>
  <artifactId>dataworks</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dataworks</name>
  <url>http://maven.apache.org</url>

  <dependencies>
      <dependency>
          <groupId>io.opentracing</groupId>
          <artifactId>opentracing-api</artifactId>
          <version>0.31.0</version>
      </dependency>
      <dependency>
          <groupId>io.opentracing</groupId>
          <artifactId>opentracing-noop</artifactId>
          <version>0.31.0</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-core</artifactId>
          <version>4.5.20</version>
      </dependency>

      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-dataworks-public</artifactId>
          <version>3.3.10</version>
      </dependency>

      <dependency>
          <groupId>com.aliyun.odps</groupId>
          <artifactId>odps-sdk-core</artifactId>
          <version>0.37.9-public</version>
      </dependency>

      <!--<dependency>-->
          <!--<groupId>com.aliyun.odps</groupId>-->
          <!--<artifactId>odps-sdk-commons</artifactId>-->
          <!--<version>0.37.9-public</version>-->
      <!--</dependency>-->

      <!--<dependency>-->
          <!--<groupId>com.aliyun.odps</groupId>-->
          <!--<artifactId>odps-sdk-udf</artifactId>-->
          <!--<version>0.37.9-public</version>-->
      <!--</dependency>-->

      <!--<dependency>-->
          <!--<groupId>com.aliyun.odps</groupId>-->
          <!--<artifactId>odps-sdk-mapred</artifactId>-->
          <!--<version>0.37.9-public</version>-->
      <!--</dependency>-->

      <!--<dependency>-->
          <!--<groupId>com.aliyun.odps</groupId>-->
          <!--<artifactId>odps-sdk-graph</artifactId>-->
          <!--<version>0.37.9-public</version>-->
      <!--</dependency>-->

      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
      </dependency>
  </dependencies>
    <repositories>
        <repository>
            <id>sonatype-nexus-staging</id>
            <name>Sonatype Nexus Staging</name>
            <!--<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>-->
            <url>https://repo1.maven.org/maven2/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

JAVA代码实现

import org.apache.commons.lang3.StringUtils; 
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest; 
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.URLDecoder;
import java.security.MessageDigest; 
import java.util.Arrays;

public class SearchNodes {
    public static void main(String[] args) throws Exception {
        HttpClient httpClient = HttpClientBuilder.create().build();
        String uri = "http://baseapi.***.com/v1.0/node/prod?exe cuteMethod=SEARCH&searchText=nodeTest";
        HttpUriRequest request = createHttpRequest(
            "base key",
            "base token", uri
        );
        request.setHeader("tenant_id", "租户 id"); 
        request.setHeader("base_id", "用户 id");

        HttpResponse response = httpClient.execute(request); 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder stringBuilder = new StringBuilder(); 
        while(bufferedReader.ready()) {
            String line = bufferedReader.readLine(); if (line == null) {
                break;
            }
            stringBuilder.append(line);
        }
        System.out.println(stringBuilder.toString());
    }

    private static String byteArrayToHexString(byte[] byteArray) {
        final char[] HEX_DIGITS = {'0', '1', '2', '3', '4',  '5', '6', '7', '8', '9', 'a', 'b','c', 'd', 'e', 'f'};
        String result = null;
        int l = byteArray.length; char[] str = new char[l << 1]; int k = 0;
        for (int i = 0; i < l; i++) {
            byte byte0 = byteArray[i];
            str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
            str[k++] = HEX_DIGITS[byte0 & 0xf];
        }
        result = new String(str); return result;
    }

    public static String tokenSign(String param, String salt) t hrows Exception {
        if (StringUtils.isEmpty(param) || StringUtils.isEmpty(salt)) {
            return null;
        }
        String query = URLDecoder.decode(param, "UTF-8");
        String[] pStr = query.trim().split("&");
        Arrays.sort(pStr);
        StringBuilder buf = new StringBuilder(); for (int i = 0; i < pStr.length; ++i) {
            buf.append(pStr[i]);
            buf.append("&");
        }
        String paramStr = buf.substring(0, buf.length() - 1);
        String sourceStr = salt + ":" + paramStr + ":" + salt;
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] byteArray = md.digest(sourceStr.getBytes("UTF-8"));
        return byteArrayToHexString(byteArray);
    }

    protected static HttpUriRequest createHttpRequest(String ba seKey, String baseToken, String uri) {
        StringBuilder buf;
        int index = uri.indexOf('?');
        if (index == -1) {
            index = uri.length();
            buf = new StringBuilder(uri.length() + baseKey.length() + 13);
            buf.append(uri).append('?');
        } else {
            buf = new StringBuilder(uri.length() + baseKey.length() + 13);
            buf.append(uri).append('&');
        }
        buf.append("baseKey=").append(baseKey);
        buf.append("&timestamp=").append(System.currentTimeMillis());
        HttpUriRequest request = new HttpGet(buf.toString()); String signature;
        try {
            signature = tokenSign(buf.substring(index + 1), baseToken);
        } catch (Exception ex) {
            throw new RuntimeException("error on sign for uri: " + uri, ex);
        }
        request.addHeader("signature", signature);
        return request;
    }
}

文章作者: darebeat
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 darebeat !
 上一篇
MySQL中SQL语句的执行过程 MySQL中SQL语句的执行过程
在使用MySQL过程中,要想避免一些复杂和慢查询,那么,了解MySQL中SQL语句从查询开始到返回结果,中间的执行过程是有必要了解一下的。
2022-05-18
下一篇 
Windows下解决端口占用问题 Windows下解决端口占用问题
在我们的日常生活和工作过程中,尝尝会面临这个多个服务同时开启的状态;而多个服务的访问时对应的端口,可能不会进行有规划的统一管理,这会导致多个服务共用一个端口的情况出现。为了解决这个问题,本文就windows系统端口占用,记录一下解决方案,留作备用参考。
2022-02-19
  目录