kairosdb中的custom *
关于kairosdb新的kairosdb-1.1.1-1版本中支持的默认数据类型只有long,double和string。如果我们需要比较负载一点的数据点类型,就需要我们自己去实现,下面我们来实现一个自定义的类型。
注册一个新的数据类型需要以下步骤:
Create a plugin.
Create a DataPoint implementation.
Create a DataPointFactory implementation.
Bind your DataPointFactory in your plugin module.
Register custom type in the properties file.
Replace the new jar package to kairosdb-1.1.1-1.jar.
Make sure.
目标:我们的自定义类型的目的是value值包含两个double类型的值real 和imaginary
以下就是主要的步骤:
一、首先我们需要下载kairosdb的源代码,下载地址为https://github.com/kairosdb/kairosdb/releases,在eclipse中加载好源码。
二、为我们的类型实现DataPoint接口,在 org.kairosdb.core.datapoints包中实现类如下
ComplexDataPoint
package org.kairosdb.core.datapoints;
import org.json.JSONException;
import org.json.JSONWriter;
import java.io.DataOutput;
import java.io.IOException;
/**
Used to show how to create a custom data type
Created by bhawkins on 6/27/14.
*/
public class ComplexDataPoint extends DataPointHelper
{
private static final String API_TYPE = "complex";
private double m_real;
private double m_imaginary;
public ComplexDataPoint(long timestamp, double real, double imaginary)
{
super(timestamp);
m_real = real;
m_imaginary = imaginary;
}
@Override
public void writeValueToBuffer(DataOutput buffer) throws IOException
{
buffer.writeDouble(m_real);
buffer.writeDouble(m_imaginary);
}
@Override
public void writeValueToJson(JSONWriter writer) throws JSONException
{
writer.object();
writer.key("real").value(m_real);
writer.key("imaginary").value(m_imaginary);
writer.endObject();
}
@Override
public String getApiDataType()
{
return API_TYPE;
}
@Override
public String getDataStoreDataType()
{
return ComplexDataPointFactory.DST_COMPLEX;
}
@Override
public boolean isLong()
{
return false;
}
@Override
public long getLongValue()
{
return 0;
}
@Override
public boolean isDouble()
{
return false;
}
@Override
public double getDoubleValue()
{
return 0;
}
}
三、为我们的类型实现一个DataPointFactory接口,在org.kairosdb.core.datapoints包中实现类如下:
ComplexDataPointFactory
package org.kairosdb.core.datapoints;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.kairosdb.core.DataPoint;
import java.io.DataInput;
import java.io.IOException;
/**
Used to show how to create a custom data type
Created by bhawkins on 6/30/14.
*/
public class ComplexDataPointFactory implements DataPointFactory
{
public static final String DST_COMPLEX = "kairos_complex";
public static final String GROUP_TYPE = "complex";
@Override
public String getDataStoreType()
{
return DST_COMPLEX;
}
@Override
public String getGroupType()
{
return GROUP_TYPE;
}
@Override
public DataPoint getDataPoint(long timestamp, JsonElement json) throws IOException
{
if (json.isJsonObject())
{
JsonObject object = json.getAsJsonObject();
double real = object.get("real").getAsDouble();
double imaginary = object.get("imaginary").getAsDouble();
return new ComplexDataPoint(timestamp, real, imaginary);
}
else
throw new IOException("JSON object is not a valid complex data point");
}
@Override
public DataPoint getDataPoint(long timestamp, DataInput buffer) throws IOException
{
double real = buffer.readDouble();
double imaginary = buffer.readDouble();
return new ComplexDataPoint(timestamp, real, imaginary);
}
}
四、绑定ComplexDataPointFactory 在我们的方法中,绑定的类在org.kairosdb.core包中的CoreModule类中,添加了代码如下:
bind(ComplexDataPointFactory.class).in(Singleton.class);
五、在配置文件中注册我们的新类型,我们在安装kairosdb的位置找到配置文件kairosdb.properties,添加内容如下:
kairosdb.datapoints.factory.complex=org.kairosdb.core.datapoints.ComplexDataPointFactory
六、生成新的jar包,替换原来的jar包(kairosdb-1.1.1-1.jar)。
1、生成jar包。
保证源码是修改后的代码,并将其导出成jar,如下图:
2、在kairosdb安装的位置,在lib中的kairosdb-1.1.1-1.jar重新命名为kairosdb-1.1.1-1.jar.bak,将新的jar包(kairosdb-1.1.1-1.jar)加入到lib文件中。
以上就完成了包的更新工作。
七、验证:
重启kairosdb,然后我们通过restclient工具使用新的数据类型提交我们的数据。
post url:http://192.168.31.25:8080/api/v1/datapoints
json格式为:
[
{
"name": "afterTest_Metric_complex",
"type": "complex",
"datapoints": [
[
1466474588000,
{
"real": 2.3,
"imaginary": 3.4
}
],
[
1466474589000,
{
"real": 1.1,
"imaginary": 5
}
]
],
"tags": {
"host": "afterPC",
"data_center": "test_DC1"
}
}
]
成功之后我们可以通过查询来获取我们之前提交的数据:
post url:http://192.168.31.25:8080/api/v1/datapoints/query
json格式为:
{
"metrics": [
{
"tags": {
"data_center": [
"test_DC1"
],
"host": [
"afterPC"
]
},
"name": "afterTest_Metric_complex"
}
],
"cache_time": 0,
"start_absolute": 1466352000000,
"end_absolute": 1466524800000
}
查询的结果为:
{
"queries": [
{
"sample_size": 2,
"results": [
{
"name": "afterTest_Metric_complex",
"group_by": [
{
"name": "type",
"type": "complex"
}
],
"tags": {
"data_center": [
"test_DC1"
],
"host": [
"afterPC"
]
},
"values": [
[
1466474588000,
{
"real": 2.3,
"imaginary": 3.4
}
],
[
1466474589000,
{
"real": 1.1,
"imaginary": 5
}
]
]
}
]
}
]
}
以上可以看到我们已经实现了自定义数据类型。比较复杂的数据类型就交给大家去研究了,但是具体步骤就是这样的,谢谢大家。高手勿喷!
相关文章