我试图将API(Google股票/财务API)中的一些数据写入我的AWS Firehose流。我已经在Eclipse上下载并安装了AWS插件,在AWS上设置了我的Firehose流,一切似乎都设置正确。但是我遇到了一些问题。以下行似乎已被弃用......我尝试了亚马逊SDK的不同版本,但我似乎无法获得正确的代码。
AmazonKinesisFirehoseClient firehoseClient = new AmazonKinesisFirehoseClient(凭证);
接下来,我发现以下错误。具体错误是" 方法setRecord(Record)未定义类型PutRecordRequest," 即使我直接从Amazon的API参考中获取它。
request.setRecord(记录);
firehoseClient.putRecord(请求);
在第二行上面也出现错误:" AmazonKinesisFirehoseClient类型中的方法putRecord(com.amazonaws.services.kinesisfirehose.model.PutRecordRequest)不适用于参数(com.amazonaws) .services.kinesis.model.PutRecordRequest)"
package com.amazonaws.samples;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import org.apache.http.client.CredentialsProvider;
import com.amazonaws.*;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.kinesis.AmazonKinesis;
import com.amazonaws.services.kinesis.AmazonKinesisClient;
import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorFactory;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.InitialPositionInStream;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker;
import com.amazonaws.services.kinesis.model.PutRecordRequest;
import com.amazonaws.services.kinesis.model.ResourceNotFoundException;
import com.amazonaws.services.kinesisfirehose.AmazonKinesisFirehoseClient;
import com.amazonaws.services.kinesisfirehose.model.PutRecordBatchRequest;
import com.amazonaws.services.kinesisfirehose.model.Record;
public class FirehoseExample {
public static void main(String[] args) {
AWSCredentials credentials = null;
try {
credentials = new ProfileCredentialsProvider().getCredentials();
}
catch (Exception e) {
throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
+ "Please make sure that your credentials file is at the correct "
+ "location (/Users/elybenari/.aws/credentials), and is in valid format.", e);
}
AmazonKinesisFirehoseClient firehoseClient = new AmazonKinesisFirehoseClient(credentials);
PutRecordRequest request = new PutRecordRequest();
request.setStreamName("project-stream");
Record record = new Record();
for (int i = 0; i < 20*60; i++){
try {
URL url = new URL("https://www.google.com/finance/info?q=NASDAQ:AMZN");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println(response.toString().replace("\n", "").replaceAll(" ", ""));
System.out.println("****\n");
ByteBuffer buffer = ByteBuffer.wrap(response.toString().replace("\n", "").replaceAll(" ", "").getBytes());
record.setData(buff);
request.setRecord(record);
firehoseClient.putRecord(request);
Thread.sleep(2000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
问题是你已经包含了Kinesis的一些类,不是 Kinesis Firehose,Java包。例如,您已经使用过:
import com.amazonaws.services.kinesis.model.PutRecordRequest;
然而,您应该使用:
import com.amazonaws.services.kinesisfirehose.model.PutRecordRequest;
Kinesis,Kinesis Firehose和Kinesis Analytics是不同的服务,即使它们属于AWS的流媒体服务。因此,它们在Java SDK中具有不同的包命名空间。如果您从官方文档here开始,那么您将获得正确的Java SDK参考here。
编辑:要回答您的其他问题:是的,以下内容已被弃用:
AmazonKinesisFirehoseClient firehoseClient = new AmazonKinesisFirehoseClient(credentials);
您应该使用以下内容:
AmazonKinesisFirehoseClient firehoseClient = AmazonKinesisFirehoseClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();
有关如何正确初始化AmazonKinesisFirehoseClient的信息,请参阅官方文档here。