-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-5090][examples] The improvement of python converter for hbase #3920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3253b61
ceb31c5
5cbbcfc
15b1fe3
21de653
62df7f0
4802481
d2153df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| package org.apache.spark.examples | ||
|
|
||
| import org.apache.hadoop.hbase.client.HBaseAdmin | ||
| import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor} | ||
| import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor, TableName} | ||
| import org.apache.hadoop.hbase.mapreduce.TableInputFormat | ||
|
|
||
| import org.apache.spark._ | ||
|
|
@@ -36,7 +36,7 @@ object HBaseTest { | |
| // Initialize hBase table if necessary | ||
| val admin = new HBaseAdmin(conf) | ||
| if (!admin.isTableAvailable(args(0))) { | ||
| val tableDesc = new HTableDescriptor(args(0)) | ||
| val tableDesc = new HTableDescriptor(TableName.valueOf(args(0))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this an API change in HBase?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. The old one is deprecated |
||
| admin.createTable(tableDesc) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Output is a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That make sense. JSON will escape the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! In fact, HBase itself will escape |
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're expanding the example to illustrate dealing with
Resultthat contains multiple records, it would probably be a good idea to expand the sample data illustrated in the comments below to make use of that feature (i.e. have the sample data have multiple records (column families?)).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea. I will modify the sample data to show multiple records in a columnFamily and columnFamily:qualifier