详情页标题前

阿里云对象存储OSS使用Java SDK的SelectObject查询CSV和JSON文件-云淘科技

详情页1

本文介绍如何使用Java SDK的SelectObject查询CSV和JSON文件。

说明

本文示例由阿里云用户bin提供,仅供参考。

以下代码用于查询CSV文件和JSON文件:

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SelectObject {

    private static final String csvKey = "test.csv";
    // Endpoint 以杭州为例,其它 Region 请按实际情况填写。
    private static final String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    private static final String accessKeyId = System.getenv("OSS_ACCESS_KEY_ID");
    private static final String accessKeySecret = System.getenv("OSS_ACCESS_KEY_SECRET");
    private static final String bucketName = "examplebucket";

    /**
     * CreateSelectObjectMeta API 用于获取目标文件总的行数,总的列数(对于 CSV 文件),以及 Splits 个数。
     */
    public static void createCsvSelectObjectMetadata () {
        // 创建 OSSClient 实例。
        OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String content = "name,school,company,age\r
" +
                "Lora Francis,School A,Staples Inc,27\r
" +
                "Eleanor Little,School B,\"Conectiv, Inc\",43\r
" +
                "Rosie Hughes,School C,Western Gas Resources Inc,44\r
" +
                "Lawrence Ross,School D,MetLife Inc.,24";

        client.putObject(bucketName, csvKey, new ByteArrayInputStream(content.getBytes()));

        SelectObjectMetadata selectObjectMetadata = client.createSelectObjectMetadata(
                new CreateSelectObjectMetadataRequest(bucketName, csvKey)
                        .withInputSerialization(
                                new InputSerialization().withCsvInputFormat(
                                        new CSVFormat().withHeaderInfo(CSVFormat.Header.Use)
                                                .withRecordDelimiter("\r
"))));
        //获取 csv 的总列数。
        System.out.println(selectObjectMetadata.getCsvObjectMetadata().getTotalLines());
        //获取 csv 的 splits 个数。
        System.out.println(selectObjectMetadata.getCsvObjectMetadata().getSplits());
        client.shutdown();
    }

    /**
     * 查询 csv。
     */
    public static void selectCsv () {
        // 创建 OSSClient 实例。
        OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            //保存 Select 请求的容器。
            SelectObjectRequest selectObjectRequest =
                    new SelectObjectRequest(bucketName, csvKey)
                            .withInputSerialization(
                                    new InputSerialization().withCsvInputFormat(
                                            new CSVFormat().withHeaderInfo(CSVFormat.Header.Use)
                                                    .withRecordDelimiter("\r
")))
                            .withOutputSerialization(new OutputSerialization().withCsvOutputFormat(new CSVFormat()));

            // ossobject 不可修改,查询第 4 列值大于 40 的数据,会直接进行隐式转换。
            selectObjectRequest.setExpression("select * from ossobject where _4 > 40");
            OSSObject ossObject = client.selectObject(selectObjectRequest);

            writeToFile(ossObject.getObjectContent(), "result.csv");
        } catch (OSSException ex) {
            System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
        } catch (ClientException ex) {
            System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
        } finally {
            client.shutdown();
        }
    }

    /**
     * 查询简单 json。
     */
    public static void selectSimpleJson () {
        String key = "simple.json";
        // 创建 OSSClient 实例。
        OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        final String content = "{
" +
                "	\"name\": \"Lora Francis\",
" +
                "	\"age\": 27,
" +
                "	\"company\": \"Staples Inc\"
" +
                "}
" +
                "{
" +
                "	\"name\": \"Eleanor Little\",
" +
                "	\"age\": 43,
" +
                "	\"company\": \"Conectiv, Inc\"
" +
                "}
" +
                "{
" +
                "	\"name\": \"Rosie Hughes\",
" +
                "	\"age\": 44,
" +
                "	\"company\": \"Western Gas Resources Inc\"
" +
                "}
" +
                "{
" +
                "	\"name\": \"Lawrence Ross\",
" +
                "	\"age\": 24,
" +
                "	\"company\": \"MetLife Inc.\"
" +
                "}";

        try {
            client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
            SelectObjectRequest selectObjectRequest =
                    new SelectObjectRequest(bucketName, key)
                            .withInputSerialization(new InputSerialization()
                                    .withCompressionType(CompressionType.NONE)
                                    .withJsonInputFormat(new JsonFormat().withJsonType(JsonType.LINES)))
                            .withOutputSerialization(new OutputSerialization()
                                    .withCrcEnabled(true)
                                    .withJsonOutputFormat(new JsonFormat()))
                            .withExpression("select * from ossobject as s where s.age > 40");

            OSSObject ossObject = client.selectObject(selectObjectRequest);
            writeToFile(ossObject.getObjectContent(), "result.simple.json");
        } catch (OSSException ex) {
            System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
        } catch (ClientException ex) {
            System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
        } finally {
            client.shutdown();
        }
    }

    /**
     * 查询复杂 json。
     */
    public static void selectComplexJson () {
        String key = "complex.json";
        // 创建 OSSClient 实例。
        OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String content = "{
" +
                "  \"contacts\":[
" +
                "{
" +
                "  \"firstName\": \"John\",
" +
                "  \"lastName\": \"Smith\",
" +
                "  \"isAlive\": true,
" +
                "  \"age\": 27,
" +
                "  \"address\": {
" +
                "    \"streetAddress\": \"21 2nd Street\",
" +
                "    \"city\": \"New York\",
" +
                "    \"state\": \"NY\",
" +
                "    \"postalCode\": \"10021-3100\"
" +
                "  },
" +
                "  \"phoneNumbers\": [
" +
                "    {
" +
                "      \"type\": \"home\",
" +
                "      \"number\": \"212 555-1234\"
" +
                "    },
" +
                "    {
" +
                "      \"type\": \"office\",
" +
                "      \"number\": \"646 555-4567\"
" +
                "    },
" +
                "    {
" +
                "      \"type\": \"mobile\",
" +
                "      \"number\": \"123 456-7890\"
" +
                "    }
" +
                "  ],
" +
                "  \"children\": [],
" +
                "  \"spouse\": null
" +
                "}
" +
                "]}";

        try {
            client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
            SelectObjectRequest selectObjectRequest =
                    new SelectObjectRequest(bucketName, key)
                            .withInputSerialization(new InputSerialization()
                                    .withCompressionType(CompressionType.NONE)
                                    .withJsonInputFormat(new JsonFormat().withJsonType(JsonType.LINES)))
                            .withOutputSerialization(new OutputSerialization()
                                    .withCrcEnabled(true)
                                    .withJsonOutputFormat(new JsonFormat()))
                            //返回所有 age 是 27 的记录,表达式可以根据 json 对象结构进行嵌套调用。
                            .withExpression("select * from ossobject.contacts[*] s where s.age = 27");

            OSSObject ossObject = client.selectObject(selectObjectRequest);
            writeToFile(ossObject.getObjectContent(), "result.complex.json");
        } catch (OSSException ex) {
            System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
        } catch (ClientException ex) {
            System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
        } finally {
            client.shutdown();
        }
    }

    /**
     * 写入文件。
     *
     * @param in
     * @param file
     */
    private static void writeToFile (InputStream in, String file){
        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            createCsvSelectObjectMetadata();
            selectCsv();
            selectComplexJson();
        } catch (ClientException ex) {
            System.out.println("ClientException: " + ex.getMessage());
        }
    }

}

SelectObject的更多详情,请参考SelectObject。

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家

阿里云企业补贴进行中: 马上申请

腾讯云限时活动1折起,即将结束: 马上收藏

同尘科技为腾讯云授权服务中心。

购买腾讯云产品享受折上折,更有现金返利:同意关联,立享优惠

转转请注明出处:https://www.yunxiaoer.com/159523.html

(0)
上一篇 2023年12月10日
下一篇 2023年12月10日
详情页2

相关推荐

  • 阿里云RDS数据库RDS SQL Server CPU使用率高问题-云淘科技

    CPU使用率较高时,容易影响查询性能。本文介绍如何查看CPU使用情况以及排查CPU问题。 查看CPU使用情况 RDS管理控制台提供多种查看CPU使用情况的方法: 监控与报警 在控制台的监控与报警页面,单击旧版监控页签,在资源监控内,可以查看CPU使用率信息。 自治服务 实例不能是RDS SQL Server 2008 R2云盘版。 在控制台的自治服务 &gt…

    2023年12月9日
  • 阿里云负载均衡SetBackendServers – 设置后端服务器权重-云淘科技

    设置后端服务器权重。 调试 您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。 调试调试授权信息下表是API对应的授权信息,可以在RAM权限策略语句的Action元素中使用,用来给RAM用户或RAM角色授予调用此API的权限。具体说明如下: 操作:是指具体的…

    阿里云负载均衡 2023年12月10日
  • 阿里云大数据开发治理平台 DataWorksUpdateWorkbenchEventResult-云淘科技

    所属扩展程序的运维中心回调API,当运维中心的流程被扩展程序卡住后,调用UpdateWorkbenchEventResult返回扩展程序的处理结果。 调试 您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。 请求参数 名称 类型 是否必选 示例值 描述 Ac…

  • 阿里云ECS云服务器DescribeDedicatedHostTypes-云淘科技

    调用DescribeDedicatedHostTypes查询指定地域下支持的专有宿主机规格详细参数,或者查询专有宿主机支持的ECS实例规格族。 调试 您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。 请求参数 名称 类型 是否必选 示例值 描述 Actio…

    阿里云服务器 2023年12月9日
  • 信息流广告,信息流部分建议宽度830px,只针对默认列表样式,顺序随机
  • 阿里云负载均衡DescribeAccessControlLists – 查询已创建的访问控制策略组-云淘科技

    查询已创建的访问控制策略组。 调试 您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。 调试调试授权信息下表是API对应的授权信息,可以在RAM权限策略语句的Action元素中使用,用来给RAM用户或RAM角色授予调用此API的权限。具体说明如下: 操作:是…

    阿里云负载均衡 2023年12月10日

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
本站为广大会员提供阿里云、腾讯云、华为云、百度云等一线大厂的购买,续费优惠,保证底价,买贵退差。