Python example update - 0.3.0

Updating the dstat example to work with the current
version of the kudu-python package (0.3.0). Works
in both python 2 and 3.


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/26b81875
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/26b81875
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/26b81875

Branch: refs/heads/master
Commit: 26b81875168b2ccfd87c965e873bc876f1ad16ef
Parents: 8bf1839
Author: Jordan Birdsell <[email protected]>
Authored: Mon Sep 26 22:28:23 2016 -0400
Committer: Jordan Birdsell <[email protected]>
Committed: Wed Sep 28 23:20:23 2016 -0400

----------------------------------------------------------------------
 python/dstat-kudu/kudu_dstat.py | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/26b81875/python/dstat-kudu/kudu_dstat.py
----------------------------------------------------------------------
diff --git a/python/dstat-kudu/kudu_dstat.py b/python/dstat-kudu/kudu_dstat.py
index 7da8932..f1f5bb2 100644
--- a/python/dstat-kudu/kudu_dstat.py
+++ b/python/dstat-kudu/kudu_dstat.py
@@ -4,16 +4,12 @@ import subprocess
 import sys
 import tempfile
 import time
+from kudu.client import Partitioning
 
 DSTAT_COL_NAMES = ["usr", "sys", "idl", "wai", "hiq", "siq", "read", "writ", 
"recv", "send",
                   "in","out","int","csw"]
 
 
-def connect_to(host, port=7051):
-  """Returns a kudu client object connecting to the specified kudu master"""
-  return kudu.Client("{0}:{1}".format(host, port))
-
-
 def open_or_create_table(client, table, drop=False):
   """Based on the default dstat column names create a new table indexed by a 
timstamp col"""
   exists = False
@@ -25,23 +21,27 @@ def open_or_create_table(client, table, drop=False):
 
   if not exists:
     # Create the schema for the table, basically all float cols
-    cols = [kudu.ColumnSchema.create("ts", kudu.INT64)]
-    cols += [kudu.ColumnSchema.create(x, kudu.FLOAT) for x in DSTAT_COL_NAMES]
+    builder = kudu.schema_builder()
+    builder.add_column("ts", kudu.int64, nullable=False, primary_key=True)
+    for col in DSTAT_COL_NAMES:
+      builder.add_column(col, kudu.float_)
+    schema = builder.build()
+
+    # Create hash partitioning buckets
+    partitioning = Partitioning().add_hash_partitions('ts', 2)
 
-    # Based on the column meta data create a new schema object, where the 
first column
-    # is the key column.
-    schema = kudu.schema_from_list(cols, 1)
-    client.create_table(table, schema)
+    client.create_table(table, schema, partitioning)
 
-  return client.open_table(table)
+  return client.table(table)
 
 def append_row(table, line):
   """The line is the raw string read from stdin, that is then splitted by , 
and prepended
   with the current timestamp."""
   data = [float(x.strip()) for x in line.split(",")]
 
-  op = table.insert()
-  op["ts"] = int(time.time())
+  op = table.new_insert()
+  # Convert to microseconds
+  op["ts"] = int(time.time() * 1000000)
   for c, v in zip(DSTAT_COL_NAMES, data):
     op[c] = v
   return op
@@ -62,7 +62,7 @@ if __name__ == "__main__":
     if operation in ["drop"]:
       drop = True
 
-  client = connect_to("127.0.0.1")
+  client = kudu.connect("127.0.0.1", 7051)
   table = open_or_create_table(client, "dstat", drop)
 
   # Start dstat

Reply via email to