0

I get this as a response to an API hit.

1735 Queries

Taking 1.001303 to 31.856310 seconds to complete

SET timestamp=XXX;   
SELECT * FROM ABC_EM WHERE last_modified >= 'XXX' AND last_modified < 'XXX';

38 Queries

Taking 1.007646 to 5.284330 seconds to complete

SET timestamp=XXX;  
show slave status;

6 Queries

Taking 1.021271 to 1.959838 seconds to complete

SET timestamp=XXX;   
SHOW SLAVE STATUS;

2 Queries

Taking 4.825584, 18.947725 seconds to complete

use marketing;   
SET timestamp=XXX;   
SELECT * FROM ABC WHERE last_modified >= 'XXX' AND last_modified < 'XXX';

I have extracted this out of the response html and have it as a string now.I need to retrieve values as concisely as possible such that I get a map of values of this format Map(Query -> T1 to T2 seconds) Basically what this is the status of all the slow queries running on MySQL slave server. I am building an alert system over it . So from this entire paragraph in the form of String I need to separate out the queries and save the corresponding time range with them. 1.001303 to 31.856310 is a time range . And against the time range the corresponding query is :

SET timestamp=XXX; SELECT * FROM ABC_EM WHERE last_modified >= 'XXX' AND last_modified < 'XXX'; 

This information I was hoping to save in a Map in scala. A Map of the form (query:String->timeRange:String)

Another example:

("use marketing; SET timestamp=XXX; SELECT * FROM ABC WHERE last_modified >= 'XXX' AND last_modified xyz ;"->"4.825584 to 18.947725 seconds")

"""###(.)###(.)\n\n(.*)###""".r.findAllIn(reqSlowQueryData).matchData foreach {m => println("group0"+m.group(1)+"next group"+m.group(2)+m.group(3)}

I am using the above statement to extract the the repeating cells to do my manipulations on it later. But it doesnt seem to be working;

THANKS IN ADvance! I know there are several ways to do this but all the ones striking me are inefficient and tedious. I need Scala to do the same! Maybe I can extract recursively using the subString method ?

2
  • Just like the near-duplicate you posted yesterday, it's still unclear what you want. Please edit your question and format it more clearly. Then show us what you have tried and where you are stuck, as this isn't a site to say "give me the code" Commented Sep 2, 2014 at 7:24
  • 3
    possible duplicate of Intelligent and quick way to parse string to get required data Commented Sep 2, 2014 at 7:24

2 Answers 2

1

If you want use scala try this:

    val regex =  """(\d+).(\d+).*(\d+).(\d+) seconds""".r // extract range

    val txt = """
                 |1735 Queries
                 |
                 |Taking 1.001303 to 31.856310 seconds to complete
                 |
                 |SET timestamp=XXX; SELECT * FROM ABC_EM WHERE last_modified >= 'XXX' AND last_modified < 'XXX';
                 |
                 |38 Queries
                 |
                 |Taking 1.007646 to 5.284330 seconds to complete
                 |
                 |SET timestamp=XXX; show slave status;
                 |
                 |6 Queries
                 |
                 |Taking 1.021271 to 1.959838 seconds to complete
                 |
                 |SET timestamp=XXX; SHOW SLAVE STATUS;
                 |
                 |2 Queries
                 |
                 |Taking 4.825584, 18.947725 seconds to complete
                 |
                 |use marketing; SET timestamp=XXX; SELECT * FROM ABC WHERE last_modified >= 'XXX' AND last_modified < 'XXX';
    """.stripMargin


def logToMap(txt:String) = {
    val (_,map) = txt.lines.foldLeft[(Option[String],Map[String,String])]((None,Map.empty)){
      (acc,el) =>
        val (taking,map) = acc // taking contains range
        taking match {
          case Some(range) if el.trim.nonEmpty => //Some contains range
            (None,map + ( el -> range)) // add to map
          case None =>
            regex.findFirstIn(el) match { //extract range
              case Some(range) => (Some(range),map) 
              case _ => (None,map)
            }
          case _ => (taking,map) // probably empty line
        }
    }
map
}
Sign up to request clarification or add additional context in comments.

Comments

0

Modified ajozwik's answer to work for SQL commands over multiple lines :

val regex =  """(\d+).(\d+).*(\d+).(\d+) seconds""".r // extract range
  def logToMap(txt:String) = {
   val (_,map) = txt.lines.foldLeft[(Option[String],Map[String,String])]((None,Map.empty)){
    (accumulator,element) =>
      val (taking,map) = accumulator
      taking match {
        case Some(range) if element.trim.nonEmpty=> {
          if (element.contains("Queries"))
          (None, map)
          else
          (Some(range),map+(range->(map.getOrElse(range,"")+element)))
        }
        case None =>
          regex.findFirstIn(element) match {
          case Some(range) => (Some(range),map)
          case _ => (None,map)
          }
        case _ => (taking,map)
      }
   }
   println(map)
   map
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.