關於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
}
]
]
}
]
}
]
}
以上可以看到我們已經實現了自定義數據類型。比較複雜的數據類型就交給大家去研究了,但是具體步驟就是這樣的,謝謝大家。高手勿噴!
"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
}
]
]
}
]
}
]
}