Skip to content

Commit 5cbbcfc

Browse files
committed
the improvement of pythonconverter
1 parent ceb31c5 commit 5cbbcfc

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

examples/src/main/scala/org/apache/spark/examples/pythonconverters/HBaseConverters.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,27 @@ import org.apache.spark.api.python.Converter
2323
import org.apache.hadoop.hbase.client.{Put, Result}
2424
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
2525
import org.apache.hadoop.hbase.util.Bytes
26+
import org.apache.hadoop.hbase.KeyValue.Type
27+
import org.apache.hadoop.hbase.CellUtil
2628

2729
/**
28-
* Implementation of [[org.apache.spark.api.python.Converter]] that converts an
29-
* HBase Result to a String
30+
* Implementation of [[org.apache.spark.api.python.Converter]] that converts all
31+
* the records in an HBase Result to a String
3032
*/
3133
class HBaseResultToStringConverter extends Converter[Any, String] {
3234
override def convert(obj: Any): String = {
35+
import collection.JavaConverters._
3336
val result = obj.asInstanceOf[Result]
34-
Bytes.toStringBinary(result.value())
37+
val output = result.listCells.asScala.map(cell =>
38+
"{'columnFamliy':'%s','qualifier':'%s','timestamp':'%s','type':'%s','value':'%s'}".format(
39+
Bytes.toStringBinary(CellUtil.cloneFamily(cell)),
40+
Bytes.toStringBinary(CellUtil.cloneQualifier(cell)),
41+
cell.getTimestamp.toString,
42+
Type.codeToType(cell.getTypeByte),
43+
Bytes.toStringBinary(CellUtil.cloneValue(cell))
44+
)
45+
)
46+
output.mkString(" ")
3547
}
3648
}
3749

0 commit comments

Comments
 (0)