Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17.  
  18. package org.apache.spark.examples.pythonconverters
  19.  
  20. import scala.collection.JavaConversions._
  21.  
  22. import org.apache.spark.api.python.Converter
  23. import org.apache.hadoop.hbase.client.{Put, Result}
  24. import org.apache.hadoop.hbase.io.ImmutableBytesWritable
  25. import org.apache.hadoop.hbase.util.Bytes
  26.  
  27. /**
  28. * Implementation of [[org.apache.spark.api.python.Converter]] that converts an
  29. * HBase Result to a String
  30. */
  31. class HBaseResultToStringConverter extends Converter[Any, String] {
  32. override def convert(obj: Any): String = {
  33. val result = obj.asInstanceOf[Result]
  34. Bytes.toStringBinary(result.value())
  35. }
  36. }
  37.  
  38. /**
  39. * Implementation of [[org.apache.spark.api.python.Converter]] that converts an
  40. * ImmutableBytesWritable to a String
  41. */
  42. class ImmutableBytesWritableToStringConverter extends Converter[Any, String] {
  43. override def convert(obj: Any): String = {
  44. val key = obj.asInstanceOf[ImmutableBytesWritable]
  45. Bytes.toStringBinary(key.get())
  46. }
  47. }
  48.  
  49. /**
  50. * Implementation of [[org.apache.spark.api.python.Converter]] that converts a
  51. * String to an ImmutableBytesWritable
  52. */
  53. class StringToImmutableBytesWritableConverter extends Converter[Any, ImmutableBytesWritable] {
  54. override def convert(obj: Any): ImmutableBytesWritable = {
  55. val bytes = Bytes.toBytes(obj.asInstanceOf[String])
  56. new ImmutableBytesWritable(bytes)
  57. }
  58. }
  59.  
  60. /**
  61. * Implementation of [[org.apache.spark.api.python.Converter]] that converts a
  62. * list of Strings to HBase Put
  63. */
  64. class StringListToPutConverter extends Converter[Any, Put] {
  65. override def convert(obj: Any): Put = {
  66. val output = obj.asInstanceOf[java.util.ArrayList[String]].map(Bytes.toBytes(_)).toArray
  67. val put = new Put(output(0))
  68. put.add(output(1), output(2), output(3))
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement