基于scala的OpenTSDB的查询(参考JAVA版的OpenTSDB API)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24084925/article/details/80366757

OpenTSDB提供三种方式的读写操作:telnet、http、post,但官方并没提供JAVA版的API。

      多亏有开源贡献者“shifeng258”,他用java编写了 opentsdb-client ,才使得我们能对openTSDB的读写操作进行封装,下面是JAVA版参考资料

https://my.oschina.net/HuQingmiao/blog/701145

而我因项目原因,用的scala,不过也是参考这篇博文写的,再次表示感谢。

一些参数设置可以参考opentsdb官网说明

http://opentsdb.net/docs/build/html/user_guide/query/filters.html

下面是部分我的基于scala的opentsdb查询代码,请多指教

override def compute(sparkSession: SparkSession): Dataset[Row] = {
    val build = QueryBuild.getInstance();

    val querie = new QueryDataQuerie();
    
    querie.setMetric(metrics)
    
    //设置聚合形式
      querie.setAggregator(aggr);

    //按tag查询的条件
    if(null != tags && tags.indexOf(":") != -1){
      val tag = tags.split(",");
      for(t <- tag){
        val kv = t.split(":")
        querie.addFilter(tagvFilterRule, kv(0), kv(1), true);
      }
    }

    //设置时间粒度
    if(!downsample.trim.isEmpty){
      querie.setDownsample(downsample+"-"+querie.aggregator);
    }
    //设置开始时间和结束时间
    build.setDataReq(start,end).addQuerie(querie);
    val clientImpl = new HttpClientImpl("http://"+url);
    val qsdr = clientImpl.get(build);
    val jsonparser:JsonParser = new JsonParser();
    val contentqsdr:JsonElement = jsonparser.parse(qsdr)
    val c:JsonElement = jsonparser.parse(contentqsdr.getAsJsonObject.get("content").getAsString)
    var list = new ArrayList[String]
    var metric=""
    var tagsjson=""
    if(c.getAsJsonArray.size()==0){
      return null;
    }else{
      for (i <- 0 to c.getAsJsonArray.size()-1){
        val dps:JsonElement = c.getAsJsonArray.get(i).getAsJsonObject.get("dps")
        metric = c.getAsJsonArray.get(i).getAsJsonObject.get("metric").getAsString
        tagsjson = c.getAsJsonArray.get(i).getAsJsonObject.get("tags").toString()
        val set = dps.getAsJsonObject.entrySet()
        val ite = set.iterator();

        while(ite.hasNext()){
          val en = ite.next();
          list.add(tagsjson+";"+en.getKey+";"+en.getValue.getAsString)
        }
      }
    }

    

    val rdd = sparkSession.sparkContext.parallelize(list.toArray(), 3)
    val rowRdd = rdd.map(x => {
      val kv = x.toString().split(";");
      RowFactory.create(metric,kv(0),kv(1),kv(2))
    });
    val schemaString = "metric tags timestamp value"
    val schema = StructType(schemaString.split(" ").map(a => StructField(a, StringType, true)))
    val df = sparkSession.createDataFrame(rowRdd, schema)
    df.show()
    return df
  }

传入该opentsdb查询算子的参数:

获取的结果展示


用csv导出构建的RDD的结果

metric,tags,timestamp,value
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546400,460497.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546430,441703.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546460,444268.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546490,455393.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546520,420697.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546550,431402.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546580,487284.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546610,474018.0
fj.metric.yali,"{\"host\":\"web10000\",\"dc\":\"lga\"}",1526546640,423753.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546400,439619.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546430,443548.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546460,452408.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546490,485137.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546520,442714.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546550,458163.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546580,426864.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546610,447950.0
fj.metric.yali,"{\"host\":\"web10001\",\"dc\":\"lga\"}",1526546640,443985.0

展开阅读全文

没有更多推荐了,返回首页