本文介绍如何使用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