aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/scala
diff options
context:
space:
mode:
authorGenTang <gen.tang86@gmail.com>2015-05-22 23:37:03 -0700
committerDavies Liu <davies@databricks.com>2015-05-22 23:37:03 -0700
commit4583cf4be17155c68178155acf6866d7cc8f7df0 (patch)
treeda036545e4fd42dc265a316801d900addb526ecd /examples/src/main/scala
parent368b8c2b5ed8b06b00ac87059f75915b13ba3b8d (diff)
downloadspark-4583cf4be17155c68178155acf6866d7cc8f7df0.tar.gz
spark-4583cf4be17155c68178155acf6866d7cc8f7df0.tar.bz2
spark-4583cf4be17155c68178155acf6866d7cc8f7df0.zip
[SPARK-5090] [EXAMPLES] The improvement of python converter for hbase
Hi, Following the discussion in http://apache-spark-developers-list.1001551.n3.nabble.com/python-converter-in-HBaseConverter-scala-spark-examples-td10001.html. I made some modification in three files in package examples: 1. HBaseConverters.scala: the new converter will converts all the records in an hbase results into a single string 2. hbase_input.py: as the value string may contain several records, we can use ast package to convert the string into dict 3. HBaseTest.scala: as the package examples use hbase 0.98.7 the original constructor HTableDescriptor is deprecated. The updation to new constructor is made Author: GenTang <gen.tang86@gmail.com> Closes #3920 from GenTang/master and squashes the following commits: d2153df [GenTang] import JSONObject precisely 4802481 [GenTang] dump the result into a singl String 62df7f0 [GenTang] remove the comment 21de653 [GenTang] return the string in json format 15b1fe3 [GenTang] the modification of comments 5cbbcfc [GenTang] the improvement of pythonconverter ceb31c5 [GenTang] the modification for adapting updation of hbase 3253b61 [GenTang] the modification accompanying the improvement of pythonconverter
Diffstat (limited to 'examples/src/main/scala')
-rw-r--r--examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala20
1 files changed, 17 insertions, 3 deletions
diff --git a/examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala b/examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala
index 273bee0a8b..90d48a6410 100644
--- a/examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala
+++ b/examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala
@@ -18,20 +18,34 @@
package org.apache.spark.examples.pythonconverters
import scala.collection.JavaConversions._
+import scala.util.parsing.json.JSONObject
import org.apache.spark.api.python.Converter
import org.apache.hadoop.hbase.client.{Put, Result}
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.util.Bytes
+import org.apache.hadoop.hbase.KeyValue.Type
+import org.apache.hadoop.hbase.CellUtil
/**
- * Implementation of [[org.apache.spark.api.python.Converter]] that converts an
- * HBase Result to a String
+ * Implementation of [[org.apache.spark.api.python.Converter]] that converts all
+ * the records in an HBase Result to a String
*/
class HBaseResultToStringConverter extends Converter[Any, String] {
override def convert(obj: Any): String = {
+ import collection.JavaConverters._
val result = obj.asInstanceOf[Result]
- Bytes.toStringBinary(result.value())
+ val output = result.listCells.asScala.map(cell =>
+ Map(
+ "row" -> Bytes.toStringBinary(CellUtil.cloneRow(cell)),
+ "columnFamily" -> Bytes.toStringBinary(CellUtil.cloneFamily(cell)),
+ "qualifier" -> Bytes.toStringBinary(CellUtil.cloneQualifier(cell)),
+ "timestamp" -> cell.getTimestamp.toString,
+ "type" -> Type.codeToType(cell.getTypeByte).toString,
+ "value" -> Bytes.toStringBinary(CellUtil.cloneValue(cell))
+ )
+ )
+ output.map(JSONObject(_).toString()).mkString("\n")
}
}