Java对接飞书开放平台-查询多维表格数据案例

Java对接飞书开放平台

查询多维表格所有列数据

pom.xml
有问题的可以关注发消息问问题.

    <dependencies>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.1.23version>
        dependency>
        <dependency>
            <groupId>com.squareup.okhttpgroupId>
            <artifactId>okhttpartifactId>
            <version>2.7.5version>
        dependency>
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-commonartifactId>
            <version>3.1.0version>
            <exclusions>
                <exclusion>
                    <groupId>log4jgroupId>
                    <artifactId>log4jartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>commons-httpclientgroupId>
            <artifactId>commons-httpclientartifactId>
            <version>3.1version>
        dependency>
        <dependency>
            <groupId>org.apache.httpcomponents.core5groupId>
            <artifactId>httpcore5artifactId>
            <version>5.2version>
        dependency>
    dependencies>
public class DoPost {

    /**
     * 发送post请求的
     * @param url
     * @param param
     * @return
     * 获取多维表格操作权限的token
     */
    public static String doPost(String url,String param,String token){
        //创建httpClients对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(url);
            if (token != null && !"".equals(token))
                httpPost.addHeader("Authorization","Bearer "+token);
            httpPost.addHeader("Content-Type","application/json");
            StringEntity entity = new StringEntity(param);
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpClient!=null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
	//查询多维表格数据GET请求
	public static String get(String token ){
        OkHttpClient client = new OkHttpClient();
        String dataLine = "";
        MediaType mediaType = MediaType.parse("");
        Request request = new Request.Builder()
        //https://open.feishu.cn/open-apis/bitable/v1/apps/:appid(填写创建多维表格的appid)/tables/:table_id(多维表格的table_id)/records?page_size=1000&sort=%5B%22%E7%99%BB%E8%AE%B0%E6%97%A5%E6%9C%9F+DESC%22%5D
        //此接口查询的是按照登记日期降序排序,查询前面的1000条数据
        //这里解释一下,因为这个接口是GET请求,这些参数有一个转码的操作
                .url(PostDataEnum.POST_TO_QUERY_FEISHU_LIST_URL2.getValue())
                .method("GET", null)
                .addHeader("Authorization", "Bearer "+token)
                .build();
        try {
            Response response = client.newCall(request).execute();
            dataLine = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataLine;
    }
}
/*
*	此方法调用GET请求拿到多维表格数据,将多维表格中的数据解析出快递编号跟订单编号
*	将订单编号跟快递编号封装到Map中,保证唯一不重复
*/
 public class QueryTableData {
	 public static Map<String,String> queryTable(){
        //获取飞书token
        String url1 = PostDataEnum.POST_TO_EARN_TOKEN_URL.getValue();
        String param1 = PostDataEnum.POST_TO_EARN_TOKEN_PARAM.getValue();
        String line = DoPost.doPost(url1, param1,"");
        Object tenant_access_token = JSONObject.parseObject(line).getOrDefault("tenant_access_token", "");

//        String dataLine = DoPost.httpGet(PostDataEnum.POST_TO_QUERY_FEISHU_LIST_URL.getValue() , "" , tenant_access_token.toString());
//        System.out.println(dataLine);
//
//        JSONObject jsonObject = JSONObject.parseObject(dataLine);
//        Object data = jsonObject.getOrDefault("data", "0");

        String dataLine = DoPost.get(tenant_access_token.toString());
        JSONObject jsonObject = JSONObject.parseObject(dataLine);
        String dataString = jsonObject.getOrDefault("data", "null").toString();
        if (dataString.equals("null"))
            return null;
        JSONObject dataObject = JSONObject.parseObject(dataString);
        String items = dataObject.getOrDefault("items", "[]").toString();
        JSONArray array = JSONArray.parseArray(items);
        Map<String , Integer> courierMap = new HashMap<>();
        Map<String , Integer> orderNoMap = new HashMap<>();
        Map<String , String> exceptionMap = new HashMap<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = JSONObject.parseObject(array.getString(i));
            JSONObject fieldsObject = JSONObject.parseObject(object.getOrDefault("fields", "").toString());
            String courier_number = fieldsObject.getOrDefault("快递单号", "null").toString();
            String orderno = fieldsObject.getOrDefault("订单编号", "null").toString();
            if (!courierMap.containsKey(courier_number)){
                courierMap.put(courier_number , 1);
            }else {
                exceptionMap.put(courier_number , orderno);
            }

            if (!orderNoMap.containsKey(orderno)){
                orderNoMap.put(orderno , 1);
            }else{
                exceptionMap.put(courier_number , orderno);
            }
        }
        return exceptionMap;
    }
}

在main函数中调用此方法即可

public static void main(String[] args) {
	Map<String, String> exceptionMap = QueryTableData.queryTable();
	}

你可能感兴趣的:(bigdata,java,big,data,etl工程师,数据库开发,cloudera)