Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Apache Phoenix Setting Trace On and Off

DZone's Guide to

Apache Phoenix Setting Trace On and Off

Here is a brief how-to of setting trace on and trace off in Apache Phoenix.

· Database Zone
Free Resource

Read why times series is the fastest growing database category.

Apache Phoenix is a SQL layer on top of HBase. Writing tracing information to tracing table "SYSTEM.TRACING_STATS" is optional and can be manipulated through the query statements "trace on" and "trace off."

The following code sample sets "trace on", executes few SQL query statements, and sets "trace off."

try {

    Connection con = DriverManager.getConnection("jdbc:phoenix:"+PHOENIX_HOST+":"+PHOENIX_PORT);
    Statement statement = con.createStatement();
    ResultSet rs = statement.executeQuery("TRACE ON");
    statement.executeUpdate("INSERT into books values (1, Little Women)";
    statement.executeUpdate("DELETE * from books where index = 3");
    statement.executeUpdate("DROP table books");
    rs = statement.executeQuery("TRACE OFF"); 
    con.close();

    } 
catch (Exception ex) {
    System.out.println(ex.getMessage());
    }


When "trace on" and "trace off" are executed the relevant trace_id for the entire trace is returned. It can be retrieved from the ResultSet.

ResultSet rs =statement.executeQuery("TRACE ON");
while (rs.next()) {
    traceId = rs.getString("trace_id");
    }


 Also, the particular trace_id for the trace can be retrieved from the following code snippet.

PhoenixConnection pconn = (PhoenixConnection) con;
long traceId = pconn.getTraceScope().getSpan().getTraceId();


The trace_id we get from all these three methods is written to the tracing table with a small delay. Therefore we need to query the tracing table for the particular trace_id appropriately.

The query statements,

statement.executeUpdate("INSERT into books values (1, Little Women)";
statement.executeUpdate("DELETE * from books where index = 3");
statement.executeUpdate("DROP table books");

can be replaced by any other query statements as preferred by the user to be executed between "trace on" and "trace off". All these queries belong to a single trace identified by a unique trace_id. But these queires may fork into multiple spans.

Learn how to get 20x more performance than Elastic by moving to a Time Series database.

Topics:
apache phoenix ,sql query ,trace

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}