使用APIG从华为数据中台取数,保存到自己的Oracle数据库中

APIG操作省略:

得到API的URL:

https://25.36.xx.xx:8343/getapi/web

maven依赖:

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.6</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.5</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>classes12</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
    </dependencies>

配置文件c3p0.properties:

c3p0.jdbcUrl=jdbc:oracle:thin:@//10.122.xx.xx:1521/orcl
c3p0.driverClass=oracle.jdbc.driver.OracleDriver
c3p0.user=xxx
c3p0.password=xxx

c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60
c3p0.maxPoolSize=20
c3p0.maxStatements=100
c3p0.minPoolSize=5

http类:

package com.zhbr.huawei;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.DefaultCookieSpecProvider;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URI;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StatefulHttpClient
 * @Description TODO
 * @Autor yanni
 * @Date 2020/4/30 15:04
 * @Version 1.0
 **/
public class StatefulHttpClient {

    private HttpClientContext context;
    private CloseableHttpClient httpclient;
    private int requestTimeOut = 15;
    private int sessionTimeOut = 60;

    public StatefulHttpClient(int sessionTimeOut, int requestTimeOut,
                              HttpHost proxy) {
        initCookieStore();
        this.sessionTimeOut = sessionTimeOut;
        this.requestTimeOut = requestTimeOut;
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
                .setConnectTimeout(this.requestTimeOut * 1000).setSocketTimeout(this.requestTimeOut * 1000);
        if (proxy != null) {
            requestConfigBuilder.setProxy(proxy);
        }
        httpclient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfigBuilder.build()).build();
    }

    public StatefulHttpClient(HttpHost proxy) {
        initCookieStore();
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
                .setConnectTimeout(10000).setSocketTimeout(this.requestTimeOut * 1000);
        if (proxy != null) {
            requestConfigBuilder.setProxy(proxy);
        }
        httpclient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfigBuilder.build()).build();
    }

    public int getRequestTimeOut() {
        return requestTimeOut;
    }

    public void setRequestTimeOut(int requestTimeOut) {
        this.requestTimeOut = requestTimeOut;
    }

    public int getSessionTimeOut() {
        return sessionTimeOut;
    }

    public void setSessionTimeOut(int sessionTimeOut) {
        this.sessionTimeOut = sessionTimeOut;
    }

    private void initCookieStore() {
        PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
        Registry<CookieSpecProvider> cookieSpecReg = RegistryBuilder.<CookieSpecProvider>create()
                .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider(publicSuffixMatcher))
                .register(CookieSpecs.STANDARD, new RFC6265CookieSpecProvider(publicSuffixMatcher)).build();
        CookieStore cookieStore = new BasicCookieStore();

        context = HttpClientContext.create();
        context.setCookieSpecRegistry(cookieSpecReg);
        context.setCookieStore(cookieStore);
    }


    /**
     * http get.
     *
     * @param url url
     * @param queryParam queryParam
     * @param headers headers
     * @return T
     * @throws IOException e
     */
    @SuppressWarnings("unchecked")
    public String getJson(String url,
                     Map<String, String> queryParam, Map<String, String> headers) throws IOException {
        HttpGet get = new HttpGet(url);
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                get.setHeader(key, headers.get(key));
            }
        }
        try {
            if (queryParam != null && queryParam.size() > 0) {
                URIBuilder builder = new URIBuilder(get.getURI());
                for (String key : queryParam.keySet()) {
                    builder.addParameter(key, queryParam.get(key));
                }
                get.setURI(builder.build());
            }

        } catch (Exception exception) {
            exception.printStackTrace();

        }
        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(get, context);
            int statusCode = response.getStatusLine().getStatusCode() / 100;
            entity = response.getEntity();
            String result = EntityUtils.toString(response.getEntity());
            if (statusCode == 4 || statusCode == 5) {
                throw new IOException(result);
            }
            if (result == null || result.length() == 0) {
                return null;
            }

            return result;
        } finally {
            if (entity != null) {
                EntityUtils.consume(entity);
            }
        }
    }
}

json类:

package com.zhbr.huawei;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName JsonUtil
 * @Description TODO
 * @Autor yanni
 * @Date 2020/5/5 11:31
 * @Version 1.0
 **/
public class JsonUtil {

    /**
     * 将json数组解析成java对象集合
     * @param jsonString
     * @param clazz
     * @return
     */
    public static List getDTOArray(String jsonString, Class clazz){
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        JSONArray jsonArray = (JSONArray)jsonObject.get("json");
        List bills = (List)JSONArray.parseArray(jsonArray.toString(), clazz);

        return bills;
    }
}

jdbc类:

package com.zhbr.huawei;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 *  1. 获取数据源
 *  2. 获取连接对象
 *  3. 返回dbutils的QueryRunner对象
 * @ClassName JDBCUtil
 * @Description TODO
 * @Autor yanni
 * @Date 2020/5/5 12:17
 * @Version 1.0
 */
public class JDBCUtil {
    private static ComboPooledDataSource dataSource = null;
    static {
        dataSource = new ComboPooledDataSource();
    }
    /**
     * 1.获取数据库连接对象 (事务),这个连接对象需要手动释放
     * @return
     */
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 2.获取dbutils的QueryRunner对象(非事务),资源会自动释放
     * @return
     */
    public static QueryRunner getQueryRunner() {
        return new QueryRunner(dataSource);
    }
}

DataTrans类

package com.zhbr.huawei;

import org.apache.commons.dbutils.QueryRunner;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName DataTrans
 * @Description TODO
 * @Autor yanni
 * @Date 2020/5/5 13:00
 * @Version 1.0
 **/
public class DataTrans {

    public static List trans(String ApiUrl,Class clazz){
        String namenodeUrl = String.format(ApiUrl);
        StatefulHttpClient client = new StatefulHttpClient(null);

        try {
            String json = client.getJson(namenodeUrl, null, null);
            List list = JsonUtil.getDTOArray(json, clazz);

            return list;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

获取到的数据的Java类

package com.zhbr;

/**
 * @ClassName User
 * @Description TODO
 * @Autor yanni
 * @Date 2020/5/5 11:55
 * @Version 1.0
 **/
public class User {

    private String name;
    private String modelerType;
    private int BlocksTotal;
    private int UnderReplicatedBlocks;
    private Long CapacityTotal;
    private int CapacityUsed;
    private Long CapacityRemaining;
    private int TotalLoad;
    private String SnapshotStats;
    private int NumEncryptionZones;
    private int FsLockQueueLength;
    private int MaxObjects;
    private int FilesTotal;
    private int PendingReplicationBlocks;
    private int ScheduledReplicationBlocks;
    private int PendingDeletionBlocks;
    private Long BlockDeletionStartTime;
    private String FSState;
    private int NumLiveDataNodes;
    private int NumDeadDataNodes;
    private int NumDecomLiveDataNodes;
    private int NumDecomDeadDataNodes;
    private int VolumeFailuresTotal;
    private int EstimatedCapacityLostTotal;
    private int NumDecommissioningDataNodes;
    private int NumStaleDataNodes;
    private int NumStaleStorages;
    private String TopUserOpCounts;
    private int NumInMaintenanceLiveDataNodes;
    private int NumInMaintenanceDeadDataNodes;
    private int NumEnteringMaintenanceDataNodes;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getModelerType() {
        return modelerType;
    }

    public void setModelerType(String modelerType) {
        this.modelerType = modelerType;
    }

    public int getBlocksTotal() {
        return BlocksTotal;
    }

    public void setBlocksTotal(int blocksTotal) {
        BlocksTotal = blocksTotal;
    }

    public int getUnderReplicatedBlocks() {
        return UnderReplicatedBlocks;
    }

    public void setUnderReplicatedBlocks(int underReplicatedBlocks) {
        UnderReplicatedBlocks = underReplicatedBlocks;
    }

    public Long getCapacityTotal() {
        return CapacityTotal;
    }

    public void setCapacityTotal(Long capacityTotal) {
        CapacityTotal = capacityTotal;
    }

    public int getCapacityUsed() {
        return CapacityUsed;
    }

    public void setCapacityUsed(int capacityUsed) {
        CapacityUsed = capacityUsed;
    }

    public Long getCapacityRemaining() {
        return CapacityRemaining;
    }

    public void setCapacityRemaining(Long capacityRemaining) {
        CapacityRemaining = capacityRemaining;
    }

    public int getTotalLoad() {
        return TotalLoad;
    }

    public void setTotalLoad(int totalLoad) {
        TotalLoad = totalLoad;
    }

    public int getNumEncryptionZones() {
        return NumEncryptionZones;
    }

    public void setNumEncryptionZones(int numEncryptionZones) {
        NumEncryptionZones = numEncryptionZones;
    }

    public int getFsLockQueueLength() {
        return FsLockQueueLength;
    }

    public void setFsLockQueueLength(int fsLockQueueLength) {
        FsLockQueueLength = fsLockQueueLength;
    }

    public int getMaxObjects() {
        return MaxObjects;
    }

    public void setMaxObjects(int maxObjects) {
        MaxObjects = maxObjects;
    }

    public int getFilesTotal() {
        return FilesTotal;
    }

    public void setFilesTotal(int filesTotal) {
        FilesTotal = filesTotal;
    }

    public int getPendingReplicationBlocks() {
        return PendingReplicationBlocks;
    }

    public void setPendingReplicationBlocks(int pendingReplicationBlocks) {
        PendingReplicationBlocks = pendingReplicationBlocks;
    }

    public int getScheduledReplicationBlocks() {
        return ScheduledReplicationBlocks;
    }

    public void setScheduledReplicationBlocks(int scheduledReplicationBlocks) {
        ScheduledReplicationBlocks = scheduledReplicationBlocks;
    }

    public int getPendingDeletionBlocks() {
        return PendingDeletionBlocks;
    }

    public void setPendingDeletionBlocks(int pendingDeletionBlocks) {
        PendingDeletionBlocks = pendingDeletionBlocks;
    }

    public Long getBlockDeletionStartTime() {
        return BlockDeletionStartTime;
    }

    public void setBlockDeletionStartTime(Long blockDeletionStartTime) {
        BlockDeletionStartTime = blockDeletionStartTime;
    }

    public String getFSState() {
        return FSState;
    }

    public void setFSState(String FSState) {
        this.FSState = FSState;
    }

    public int getNumLiveDataNodes() {
        return NumLiveDataNodes;
    }

    public void setNumLiveDataNodes(int numLiveDataNodes) {
        NumLiveDataNodes = numLiveDataNodes;
    }

    public int getNumDeadDataNodes() {
        return NumDeadDataNodes;
    }

    public void setNumDeadDataNodes(int numDeadDataNodes) {
        NumDeadDataNodes = numDeadDataNodes;
    }

    public int getNumDecomLiveDataNodes() {
        return NumDecomLiveDataNodes;
    }

    public void setNumDecomLiveDataNodes(int numDecomLiveDataNodes) {
        NumDecomLiveDataNodes = numDecomLiveDataNodes;
    }

    public int getNumDecomDeadDataNodes() {
        return NumDecomDeadDataNodes;
    }

    public void setNumDecomDeadDataNodes(int numDecomDeadDataNodes) {
        NumDecomDeadDataNodes = numDecomDeadDataNodes;
    }

    public int getVolumeFailuresTotal() {
        return VolumeFailuresTotal;
    }

    public void setVolumeFailuresTotal(int volumeFailuresTotal) {
        VolumeFailuresTotal = volumeFailuresTotal;
    }

    public int getEstimatedCapacityLostTotal() {
        return EstimatedCapacityLostTotal;
    }

    public void setEstimatedCapacityLostTotal(int estimatedCapacityLostTotal) {
        EstimatedCapacityLostTotal = estimatedCapacityLostTotal;
    }

    public int getNumDecommissioningDataNodes() {
        return NumDecommissioningDataNodes;
    }

    public void setNumDecommissioningDataNodes(int numDecommissioningDataNodes) {
        NumDecommissioningDataNodes = numDecommissioningDataNodes;
    }

    public int getNumStaleDataNodes() {
        return NumStaleDataNodes;
    }

    public void setNumStaleDataNodes(int numStaleDataNodes) {
        NumStaleDataNodes = numStaleDataNodes;
    }

    public int getNumStaleStorages() {
        return NumStaleStorages;
    }

    public void setNumStaleStorages(int numStaleStorages) {
        NumStaleStorages = numStaleStorages;
    }


    public int getNumInMaintenanceLiveDataNodes() {
        return NumInMaintenanceLiveDataNodes;
    }

    public void setNumInMaintenanceLiveDataNodes(int numInMaintenanceLiveDataNodes) {
        NumInMaintenanceLiveDataNodes = numInMaintenanceLiveDataNodes;
    }

    public int getNumInMaintenanceDeadDataNodes() {
        return NumInMaintenanceDeadDataNodes;
    }

    public void setNumInMaintenanceDeadDataNodes(int numInMaintenanceDeadDataNodes) {
        NumInMaintenanceDeadDataNodes = numInMaintenanceDeadDataNodes;
    }

    public int getNumEnteringMaintenanceDataNodes() {
        return NumEnteringMaintenanceDataNodes;
    }

    public void setNumEnteringMaintenanceDataNodes(int numEnteringMaintenanceDataNodes) {
        NumEnteringMaintenanceDataNodes = numEnteringMaintenanceDataNodes;
    }
}

测试主类:

package com.zhbr;

import com.zhbr.huawei.DataTrans;
import com.zhbr.huawei.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName test
 * @Description TODO
 * @Autor yanni
 * @Date 2020/5/5 13:35
 * @Version 1.0
 **/
public class test {

    public static void main(String[] args) throws SQLException {

        List list = DataTrans.trans("https://25.36.xx.xx:8343/getapi/web", User.class);

        List<User> newList = list;

        for(User user:newList){
            String sql = "insert into user_info values (?,?,?)";
            Object [] params = {user.getFilesTotal(),user.getName(),user.getBlocksTotal()};
            QueryRunner qr = JDBCUtil.getQueryRunner();
            qr.update(sql,params);
        }
    }
}

你可能感兴趣的:(国网大数据平台,杂项)