Advertisement
Guest User

hbase98test

a guest
Feb 18th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.69 KB | None | 0 0
  1. package com.example
  2.  
  3. import java.io.IOException
  4. import org.apache.hadoop.conf.Configuration
  5. import org.apache.hadoop.io._
  6. import scala.collection.JavaConversions._
  7. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat
  8. import org.apache.hadoop.mapreduce.Job
  9. import org.apache.hadoop.mapreduce.Reducer
  10. import org.apache.hadoop.mapreduce.Mapper
  11. import org.apache.hadoop.hbase.HBaseConfiguration
  12. import org.apache.hadoop.hbase.client.HBaseAdmin
  13. import org.apache.hadoop.hbase.client.HConnectionManager
  14. import org.apache.hadoop.hbase.client.HConnection
  15. import org.apache.hadoop.hbase.client.HTableInterface
  16. import org.apache.hadoop.hbase.client.HTable
  17. import org.apache.hadoop.hbase.HTableDescriptor
  18. import org.apache.hadoop.hbase.util.Bytes
  19. import org.apache.hadoop.hbase.HColumnDescriptor
  20. import org.apache.hadoop.hbase.client.Put
  21. import java.util.logging.Logger
  22.  
  23. case class HString(name: String) {
  24.     lazy val bytes = name.getBytes
  25.     override def toString = name
  26. }
  27. object HString {
  28.     import scala.language.implicitConversions
  29.     implicit def hstring2String(src: HString): String = src.name
  30.     implicit def hstring2Bytes(src: HString): Array[Byte] = src.bytes
  31. }
  32.  
  33. object Families {
  34.     val stream = HString("stream")
  35.     val identity = HString("identity")
  36.     val cf = HString("cf")
  37. }
  38. object Qualifiers {
  39.     val title = HString("title")
  40.     val url = HString("url")
  41.     val media = HString("media")
  42.     val media_source = HString("media_source")
  43.     val content = HString("content")
  44.     val nolimitid_timestamp = HString("nolimitid.timestamp")
  45.     val original_id = HString("original_id")
  46.     val timestamp = HString("timestamp")
  47.     val date_created = HString("date_created")
  48.     val count = HString("count")
  49. }
  50. object Tables {
  51.     val rawstream100 = HString("raw_stream_1.0.0")
  52.     val rawstream = HString("rawCopy")
  53. }
  54.  
  55. object Hello {
  56.   val hbaseConfig = {
  57.         val conf = HBaseConfiguration.create()
  58.         conf.set("hbase.master", "127.0.0.1:60000")
  59.         conf.set("hbase.zookeeper.quorum", "127.0.0.1")
  60.         conf.set("hbase.zookeeper.property.clientPort", "2181")
  61.         conf
  62.       }
  63.      
  64.   val hbaseAdmin = new HBaseAdmin(hbaseConfig)
  65.   val connection = HConnectionManager.createConnection(hbaseConfig)
  66.  
  67.   def createInterface(connection: HConnection, tableName: String, tableFamily: List[String]): HTableInterface = {
  68.     checkTable(tableName, tableFamily)
  69.     val table = connection.getTable(tableName)
  70.     return table
  71.   }
  72.  
  73.   def createTable(tableName: String, tableFamily: List[String]): HTable = {
  74.     checkTable(tableName, tableFamily)
  75.     val table = new HTable(hbaseConfig, tableName)
  76.     return table
  77.   }
  78.  
  79.   def checkTable(tableName: String, tableFamily: List[String]) {
  80. //    if (!hbaseAdmin.tableExists(tableName)) {
  81.       val desc = new HTableDescriptor(tableName)
  82.       tableFamily.foreach(n => desc.addFamily(new HColumnDescriptor(Bytes.toBytes(n))))
  83.       hbaseAdmin.createTable(desc)
  84.   //  }
  85.   }
  86.   def main(args: Array[String]): Unit = {
  87.     //
  88.    
  89.     val conf = HBaseConfiguration.create()
  90.     conf.set("hbase.master", "127.0.0.1:60000")
  91.     conf.set("hbase.zookeeper.quorum", "127.0.0.1")
  92.     //conf.set("hbase.zookeeper.property.clientPort", "2181")
  93.     val table = new HTable(conf, Bytes.toBytes("table1"))
  94.     val p = new Put(Bytes.toBytes("row1"))
  95.     p.add(Bytes.toBytes("fam1"), Bytes.toBytes("qul1"),
  96.       Bytes.toBytes("asolole"))
  97.     table.put(p)
  98.     table.flushCommits()
  99.     table.close()
  100.    
  101.     var putObj      = new Put(Bytes.toBytes("tes1"))
  102.     putObj.add(
  103.         Bytes.toBytes("data"),
  104.         Bytes.toBytes("id"),
  105.         Bytes.toBytes(212)
  106.     )
  107.     //htablei.put(putObj)        
  108.     //htablei.flushCommits()
  109.     //htablei.close()
  110.     println("endcode")
  111.     //
  112.   }
  113. }
  114.  
  115. class Hello {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement