-
Notifications
You must be signed in to change notification settings - Fork 280
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
added HBase timestamp support #297
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -56,7 +56,7 @@ private[hbase] class HBaseTableScanRDD( | |
requiredColumns: Array[String], | ||
filters: Array[Filter]) extends RDD[Row](relation.sqlContext.sparkContext, Nil) { | ||
val outputs = StructType(requiredColumns.map(relation.schema(_))).toAttributes | ||
val columnFields = relation.splitRowKeyColumns(requiredColumns)._2 | ||
val columnFields = relation.splitReservedColumns(requiredColumns)._2 | ||
private def sparkConf = SparkEnv.get.conf | ||
val serializedToken = relation.serializedToken | ||
|
||
|
@@ -125,6 +125,15 @@ private[hbase] class HBaseTableScanRDD( | |
} | ||
} | ||
|
||
val tsSeq = { | ||
if (relation.catalog.ts.isPresent) { | ||
val f = relation.catalog.getTimestamp.head | ||
Seq((f, result.rawCells()(0).getTimestamp)).toMap | ||
} else { | ||
Seq.empty | ||
} | ||
} | ||
|
||
import scala.collection.JavaConverters.mapAsScalaMapConverter | ||
val scalaMap = result.getMap.asScala | ||
|
||
|
@@ -165,7 +174,7 @@ private[hbase] class HBaseTableScanRDD( | |
}.toMap | ||
} | ||
|
||
val unioned = keySeq ++ valuesSeq | ||
val unioned = keySeq ++ tsSeq ++ valuesSeq | ||
|
||
// Return the row ordered by the requested order | ||
val ordered = fields.map(unioned.getOrElse(_, null)) | ||
|
@@ -187,6 +196,15 @@ private[hbase] class HBaseTableScanRDD( | |
} | ||
} | ||
|
||
val tsSeq = { | ||
if (relation.catalog.ts.isPresent) { | ||
val f = relation.catalog.getTimestamp.head | ||
Seq((f, result.rawCells()(0).getTimestamp)).toMap | ||
} else { | ||
Seq.empty | ||
} | ||
} | ||
|
||
import scala.collection.JavaConverters.mapAsScalaMapConverter | ||
val scalaMap = result.getNoVersionMap.asScala | ||
|
||
|
@@ -206,7 +224,7 @@ private[hbase] class HBaseTableScanRDD( | |
}).toMap | ||
} | ||
|
||
val unioned = keySeq ++ valuesSeq | ||
val unioned = keySeq ++ tsSeq ++ valuesSeq | ||
|
||
// Return the row ordered by the requested order | ||
val ordered = fields.map(unioned.getOrElse(_, null)) | ||
|
@@ -228,7 +246,16 @@ private[hbase] class HBaseTableScanRDD( | |
} | ||
} | ||
|
||
val valueSeq: Seq[Map[Long, (Field, Any)]] = fields.filter(!_.isRowKey).map { x => | ||
val tsSeq = { | ||
if (relation.catalog.ts.isPresent) { | ||
val f = relation.catalog.getTimestamp.head | ||
Seq((f, result.rawCells()(0).getTimestamp)).toMap | ||
} else { | ||
Seq.empty | ||
} | ||
} | ||
|
||
val valueSeq: Seq[Map[Long, (Field, Any)]] = fields.filter(c => HBaseTableCatalog.isNotReserved(c.cf)).map { x => | ||
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 this? val tsField = relation.catalog.getTimestamp.head // ADDED
val valueSeq: Seq[Map[Long, (Field, Any)]] = fields.filter(c => HBaseTableCatalog.isNotReserved(c.cf)).map { x =>
import scala.collection.JavaConverters.asScalaBufferConverter
val dataType = SHCDataTypeFactory.create(x)
val kvs = result.getColumnCells(
relation.catalog.shcTableCoder.toBytes(x.cf),
relation.catalog.shcTableCoder.toBytes(x.col)).asScala
kvs.map(kv => {
val v = CellUtil.cloneValue(kv)
(kv.getTimestamp, x -> dataType.fromBytes(v))
}).toMap.withDefaultValue(x -> null)
}
val ts = valueSeq.foldLeft(Set.empty[Long])((acc, map) => acc ++ map.keySet)
//we are loosing duplicate here, because we didn't support passing version (timestamp) to the row
ts.map(version => {
val tsSeq = Seq((tsField, version)).toMap // ADDED
keySeq ++ tsSeq ++ valueSeq.map(_.apply(version)).toMap // MODIFIED
}).map { unioned =>
// Return the row ordered by the requested order
Row.fromSeq(fields.map(unioned.get(_).getOrElse(null)))
} The only extra change that is required is to specify timestamp column when 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. My bad.In my use case, i have a single version, and i forget this point. |
||
import scala.collection.JavaConverters.asScalaBufferConverter | ||
val dataType = SHCDataTypeFactory.create(x) | ||
val kvs = result.getColumnCells( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql | ||
|
||
import org.apache.spark.sql.execution.datasources.hbase.Logging | ||
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FunSuite} | ||
import org.apache.spark.sql.execution.datasources.hbase.HBaseTableCatalog | ||
|
||
class CatalogWithTimestampSuite extends FunSuite with BeforeAndAfterEach with BeforeAndAfterAll with Logging{ | ||
def catalog = s"""{ | ||
|"table":{"namespace":"default", "name":"table1", "tableCoder":"PrimitiveType"}, | ||
|"rowkey":"key1:key2", | ||
|"timestamp":"ts", | ||
|"columns":{ | ||
|"col00":{"cf":"rowkey", "col":"key1", "type":"string", "length":"6"}, | ||
|"col01":{"cf":"rowkey", "col":"key2", "type":"int"}, | ||
|"colT":{"cf":"timestamp", "col":"ts", "type":"long"}, | ||
|"col1":{"cf":"cf1", "col":"col1", "type":"boolean"}, | ||
|"col2":{"cf":"cf2", "col":"col2", "type":"double"}, | ||
|"col3":{"cf":"cf3", "col":"col3", "type":"float"}, | ||
|"col4":{"cf":"cf4", "col":"col4", "type":"int"}, | ||
|"col5":{"cf":"cf5", "col":"col5", "type":"bigint"}, | ||
|"col6":{"cf":"cf6", "col":"col6", "type":"smallint"}, | ||
|"col8":{"cf":"cf8", "col":"col8", "type":"tinyint"}, | ||
|"col7":{"cf":"cf7", "col":"col7", "type":"string"} | ||
|} | ||
|}""".stripMargin | ||
|
||
test("Catalog with timestamp field meta data check") { | ||
val m = HBaseTableCatalog(Map(HBaseTableCatalog.tableCatalog->catalog)) | ||
assert(!m.row.fields.exists(_.length == -1)) | ||
assert(m.row.length == 10) | ||
assert(!m.ts.fields.exists(_.length == -1)) | ||
assert(m.ts.length == 8) | ||
assert(m.getColumnFamilies.intersect(HBaseTableCatalog.reserved).isEmpty) | ||
} | ||
|
||
test("Catalog with timestamp field should preserve the columns order") { | ||
val m = HBaseTableCatalog(Map(HBaseTableCatalog.tableCatalog->catalog)) | ||
assert(m.toDataType.fields.map(_.name).sameElements( | ||
Array("col00", "col01", "colT", "col1", "col2", "col3", "col4", "col5", "col6", "col8", "col7"))) | ||
} | ||
} |
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.
@sbarnoud when there are multiple cells in a single column, each cell will have different timestamp. However, this code reads the timestamp only from the latest cell. Is this intended behavior?
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.
plus
tsSeq
is not used insidebuildRows