Github user mridulm commented on a diff in the pull request:

    https://github.com/apache/spark/pull/194#discussion_r10862647
  
    --- Diff: 
external/hbase/src/main/scala/org/apache/spark/nosql/hbase/SparkHBaseWriter.scala
 ---
    @@ -0,0 +1,139 @@
    +/*
    + * 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.nosql.hbase
    +
    +import org.apache.hadoop.hbase.client.{Put, HTable}
    +import org.apache.hadoop.io.Text
    +import org.apache.hadoop.hbase.util.Bytes
    +import org.apache.commons.codec.binary.Hex
    +import org.apache.hadoop.hbase.HConstants
    +import org.apache.hadoop.conf.Configuration
    +import java.io.IOException
    +
    +/**
    + * Internal helper class that saves an RDD using a HBase OutputFormat. 
This is only public
    + * because we need to access this class from the `spark` package to use 
some package-private HBase
    + * functions, but this class should not be used directly by users.
    + */
    +private[apache]
    +class SparkHBaseWriter(conf: HBaseConf) {
    +
    +  private var htable: HTable = null
    +
    +  val zkHost = conf.zkHost
    +  val zkPort = conf.zkPort
    +  val zkNode = conf.zkNode
    +  val table = conf.table
    +  val rowkeyType = conf.rowkeyType
    +  val columns = conf.columns
    +  val delimiter = conf.delimiter
    +
    +  def init() {
    +    val conf = new Configuration()
    +    conf.set(HConstants.ZOOKEEPER_QUORUM, zkHost)
    +    conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, zkPort)
    +    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, zkNode)
    +    htable = new HTable(conf, table)
    +    // Use default writebuffersize to submit batch puts
    +    htable.setAutoFlush(false)
    +  }
    +
    +  /**
    +   * Convert field to bytes
    +   * @param field split by delimiter from record
    +   * @param kind the type of field
    +   * @return
    +   */
    +  def toByteArr(field: String, kind: String) = kind match {
    +    case HBaseType.Boolean => Bytes.toBytes(field.toBoolean)
    +    case HBaseType.Short => Bytes.toBytes(field.toShort)
    +    case HBaseType.Int => Bytes.toBytes(field.toInt)
    +    case HBaseType.Long => Bytes.toBytes(field.toLong)
    +    case HBaseType.Float => Bytes.toBytes(field.toFloat)
    +    case HBaseType.Double => Bytes.toBytes(field.toDouble)
    +    case HBaseType.String => Bytes.toBytes(field)
    +    case HBaseType.Bytes => Hex.decodeHex(field.toCharArray)
    +    case _ => throw new IOException("Unsupported data type.")
    +  }
    +
    +  /**
    +   * Convert a string record to [[org.apache.hadoop.hbase.client.Put]]
    +   * @param record
    +   * @return
    +   */
    +  def parseRecord(record: String) = {
    +    val fields = record.split(delimiter)
    +    val put = new Put(toByteArr(fields(0), rowkeyType))
    +
    +    List.range(1, fields.size) foreach {
    +      i => put.add(columns(i - 1).family, columns(i - 1).qualifier,
    +        toByteArr(fields(i), columns(i - 1).typ))
    +    }
    +
    +    put
    --- End diff --
    
    I dont know how hbase is normally used by users - so I cant comment 
unfortunately.
    It was not clear if this was the assumption, but I inferred it based on the 
"- 1" in rest of the code, etc.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to