admin管理员组文章数量:1400080
I am currently reading a json file from s3 location using the below code and then printing the data.
Json File
[
{
"name": "A",
"lastName" : "B"
},
{
"name": "C",
"lastName" : "D"
}
]
Reading data from s3
AwsBasicCredentials awsSourceBasicCredentials = AwsBasicCredentials.create(accessKey, secretKey);
URI sourceUrl = new URI(ep);
S3Client s3Client= S3Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(awsSourceBasicCredentials))
.region(Region.AWS_GLOBAL)
.endpointOverride(sourceUrl)
.build();
ResponseInputStream object = s3Client.getObject(request -> request
.bucket(bucketName)
.key(key));
This ResponseInputStram I am priniting.
Say suppose I have a Name Object. Name.class
class Name
{
String name;
String lastName;
}
Is there a way that instead of printing I can create a List<Name>
?
I am currently reading a json file from s3 location using the below code and then printing the data.
Json File
[
{
"name": "A",
"lastName" : "B"
},
{
"name": "C",
"lastName" : "D"
}
]
Reading data from s3
AwsBasicCredentials awsSourceBasicCredentials = AwsBasicCredentials.create(accessKey, secretKey);
URI sourceUrl = new URI(ep);
S3Client s3Client= S3Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(awsSourceBasicCredentials))
.region(Region.AWS_GLOBAL)
.endpointOverride(sourceUrl)
.build();
ResponseInputStream object = s3Client.getObject(request -> request
.bucket(bucketName)
.key(key));
This ResponseInputStram I am priniting.
Say suppose I have a Name Object. Name.class
class Name
{
String name;
String lastName;
}
Is there a way that instead of printing I can create a List<Name>
?
2 Answers
Reset to default 0So if you just want to read the JSON file and bind that into a Java object you can use Jackson for that:
try( var res = s3Client.getObject(request -> request
.bucket(bucketName)
.key(key)) ) {
Reader reader = new InputStreamReader( res, res.response().contentType() );
List<Name> names = objectMapper.readValue(reader, new TypeReference<List<Name>>(){});
}
And remember to add the dependency to your build environment:
<!-- https://mvnrepository/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.18.3</version>
</dependency>
Or
// https://mvnrepository/artifact/com.fasterxml.jackson.core/jackson-core
implementation("com.fasterxml.jackson.core:jackson-core:2.18.3")
I implemented something like this:
ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
String jsonString = objectBytes.asUtf8String();
List<Name> fileObjects = mapper.readValue(jsonString, new TypeReference<List<Name>>(){});
本文标签: javaReading a json file from ECS location and transforming it into list of ObjectStack Overflow
版权声明:本文标题:java - Reading a json file from ECS location and transforming it into list of Object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744255898a2597480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
amazon-ecs
. This question appears to be entirely about S3. – Mark B Commented Mar 24 at 12:10