问题
I use jmeter to create HTTP request for my webservice REST. Now i want to send this request to Amazon kinesis using PutRecords method but i don't know hot to create the request, in particular how to set Headers fields for Signing and Authenticating in kinesis. Has someone used Rest request? thanks
回答1:
As per PutRecords API reference sample request should look as
POST / HTTP/1.1
Host: kinesis.<region>.<domain>
x-amz-Date: <Date>
Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x- amzn-requestid, Signature=<Signature>
User-Agent: <UserAgentString>
Content-Type: application/x-amz-json-1.1
Content-Length: <PayloadSizeBytes>
Connection: Keep-Alive
X-Amz-Target: Kinesis_20131202.PutRecords
So at least the following are required:
- Content-Type
- X-Amz-Target
- Authorization
- x-amz-date
You can add a HTTP Header Manager to add them to your requests.
JMeter should populate Content-Length, Connection and Host on his own.
回答2:
This question is old and I don't remember all but this is the code if someone need it: BeanShell Sampler
import org.MyKinesisClient;
//Create a controller object every time Jmeter starts
MyKinesisClient controller=new MyKinesisClient(vars.get("accessKey"),vars.get("secretKey"),vars.get("endpoint"),vars.get("serviceName"),vars.get("regionId"));
bsh.shared.controller=controller;
This is the last code:
import com.amazonaws.util.json.JSONArray;
import com.amazonaws.util.json.JSONObject;
import org.MyKinesisClient;
//Variables
int timestampValue=(${i});
String idValue=${__threadNum}+"_"+"1";
JSONObject part = new JSONObject();
//Inserimento campi Json
part.put("updated",timestampValue);
part.put("parent","${__threadNum}");
part.put("id",idValue);
part.put("thingClass","CosyInverter");
part.put("mac_address_w","${mac_address_w_1}");
//Other put
....
//Send Json to kinesis
MyKinesisClient controller=bsh.shared.controller;
controller.sendJson(part, ${__Random(0,${__threadNum})},vars.get("streamName"));
My code is more complicated than that above (database query etc...) but I hope this is what you need.
This is the java code about MyKinesisClient
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.kinesis.AmazonKinesisClient;
import com.amazonaws.services.kinesis.model.PutRecordRequest;
import com.amazonaws.services.kinesis.model.PutRecordsRequest;
import com.amazonaws.services.kinesis.model.PutRecordsRequestEntry;
import com.amazonaws.util.json.JSONArray;
import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
/**
* Class useful to send Json to Amazon Kinesis.
* @author l.calicchio
*
*/
public class MyKinesisClient {
private AmazonKinesisClient kinesisClient;
/**
* Class constructor. Allow all parameter setting
* @param accessKey: access key for kinesis connection
* @param secretKey: secret key for kinesis connection
* @param endpoint: Kinesis endpoint
* @param serviceName: Amazon service name
* @param regionId: region id of kinesis endpoint
*/
public MyKinesisClient(String accessKey, String secretKey, String endpoint, String serviceName, String regionId ) {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
kinesisClient = new AmazonKinesisClient(credentials);
kinesisClient.setEndpoint(endpoint,serviceName,regionId);
}
/**
* Method useful to send the json to Kinesis
* @param json: com.amazonaws.util.json.JSONObject to be sent
* @param partitionKey: partition key for Kinesis stream
* @param streamName: name of Kinesis stream
* @throws UnsupportedEncodingException
* @throws JSONException
*/
public void sendJson(JSONObject json, int partitionKey, String streamName) throws UnsupportedEncodingException, JSONException {
try{
PutRecordRequest putRecordRequest = new PutRecordRequest();
putRecordRequest.setStreamName(streamName);
putRecordRequest.setData(ByteBuffer.wrap(json.toString().getBytes("utf-8")));
putRecordRequest.setPartitionKey(String.format("partitionKey-%d", partitionKey));
kinesisClient.putRecord(putRecordRequest);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
/**
* Method useful to send the json Array to Kinesis
* @param json: com.amazonaws.util.json.JSONObject Array to be sent
* @param partitionKey: partition key for Kinesis stream
* @param streamName: Kinesis stream name
*/
public void sendJsonArray(JSONArray json,int partitionKey, String streamName) {
try{
PutRecordsRequest putRecordsRequest = new PutRecordsRequest();
putRecordsRequest.setStreamName(streamName);
List <PutRecordsRequestEntry> putRecordsRequestEntryList = new ArrayList<PutRecordsRequestEntry>();
for(int i=0;i<json.length();i++){
PutRecordsRequestEntry putRecordsRequestEntry = new PutRecordsRequestEntry();
putRecordsRequestEntry.setData(ByteBuffer.wrap(json.getJSONObject(i).toString().getBytes("utf-8")));
putRecordsRequestEntry.setPartitionKey(String.format("partitionKey-%d", partitionKey));
putRecordsRequestEntryList.add(putRecordsRequestEntry);
}
putRecordsRequest.setRecords(putRecordsRequestEntryList);
kinesisClient.putRecords(putRecordsRequest);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
来源:https://stackoverflow.com/questions/30031477/using-http-request-of-jmeter-to-put-records-into-amazon-kinesis