本文整理汇总了Scala中org.apache.hadoop.hbase.mapreduce.TableInputFormat类的典型用法代码示例。如果您正苦于以下问题:Scala TableInputFormat类的具体用法?Scala TableInputFormat怎么用?Scala TableInputFormat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TableInputFormat类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: HBaseHelloWorld
//设置package包名称以及导入依赖的类
import org.apache.spark._
import org.apache.hadoop._
import org.apache.hadoop.hbase.client.{HBaseAdmin, Result}
import org.apache.hadoop.hbase.{ HBaseConfiguration, HTableDescriptor }
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.client.{HBaseAdmin,HTable,Put,Get}
import org.apache.hadoop.hbase.util.Bytes
object HBaseHelloWorld {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("HBaseRead")
val sc = new SparkContext(sparkConf)
val conf = HBaseConfiguration.create()
val tableName = "enterprises"
conf.set("hbase.master", "localhost:60000")
conf.setInt("timeout", 120000)
conf.set(TableInputFormat.INPUT_TABLE, tableName)
val admin = new HBaseAdmin(conf)
if (!admin.isTableAvailable(tableName)) {
val tableDesc = new HTableDescriptor(tableName)
admin.createTable(tableDesc)
}
val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat], classOf[ImmutableBytesWritable], classOf[Result])
println("Number of Records found : " + hBaseRDD.count())
val pairs = hBaseRDD.map(s => (s, 1))
val counts = pairs.reduceByKey((a, b) => a + b)
sc.stop()
}
}
示例2: HBaseReader
//设置package包名称以及导入依赖的类
import org.apache.hadoop.hbase.client.{ HBaseAdmin, Result }
import org.apache.hadoop.hbase.HTableDescriptor
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.util.Bytes
object HBaseReader {
def main(args: Array[String]) {
if(args.size != 1){
println("Usage: HBaseReader <table name>")
System.exit(-1)
}
val tableName = args(0)
val master = ""
val sc = Connection.createSparkConf()
val conf = Connection.createHBaseConf()
val admin = new HBaseAdmin(conf)
if (!admin.isTableAvailable(tableName)) {
val tableDesc = new HTableDescriptor(tableName)
admin.createTable(tableDesc)
}
conf.set(TableInputFormat.INPUT_TABLE, tableName)
val hBaseRDD = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat],
classOf[ImmutableBytesWritable],
classOf[Result])
println("Number of Records found : " + hBaseRDD.count())
val stocksRdd = hBaseRDD.map {
case (key: ImmutableBytesWritable, value: Result) =>
Stock(
new String(key.get),
new String(value.getValue(Bytes.toBytes("prices"), Bytes.toBytes("Open"))),
new String(value.getValue(Bytes.toBytes("prices"), Bytes.toBytes("Close"))),
new String(value.getValue(Bytes.toBytes("prices"), Bytes.toBytes("High"))),
new String(value.getValue(Bytes.toBytes("prices"), Bytes.toBytes("Low"))),
new String(value.getValue(Bytes.toBytes("prices"), Bytes.toBytes("AdjClose"))),
new String(value.getValue(Bytes.toBytes("volume"), Bytes.toBytes("vol"))))
}
stocksRdd.take(10).foreach(println)
}
}
示例3: HbaseExample
//设置package包名称以及导入依赖的类
package com.hortonworks.examples
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.{Get, HBaseAdmin, HTable}
import org.apache.hadoop.hbase.mapreduce.{TableInputFormat, TableOutputFormat}
import org.apache.hadoop.hbase.util.Bytes
import org.apache.spark.{SparkConf, SparkContext}
object HbaseExample {
def main(arg: Array[String]) {
if (arg.length < 2) {
System.err.println("Usage: HbaseExample Hbase-Table RowKey")
System.exit(1)
}
val jobName = "SparkHBaseTest"
val conf = new SparkConf().setAppName(jobName)
val sc = new SparkContext(conf)
val hbaseTable = arg(0)
val rowKey = arg(1)
println("hbase table: " + hbaseTable)
println("row key to get: " + rowKey)
runTest(hbaseTable, rowKey, sc)
}
def runTest(hbaseTable: String, rowKey: String, sc: SparkContext): Null = {
val hconf = HBaseConfiguration.create()
hconf.set(TableOutputFormat.OUTPUT_TABLE, hbaseTable)
hconf.set(TableInputFormat.INPUT_TABLE, hbaseTable)
HBaseAdmin.checkHBaseAvailable(hconf)
val htable = new HTable(hconf, hbaseTable)
println("Hbase table connection established")
println(htable.getTableDescriptor)
println(htable.get(new Get(Bytes.toBytes(rowKey))).getMap)
println("Getting count of records in table now...")
val hBaseRDD = sc.newAPIHadoopRDD(hconf, classOf[TableInputFormat],
classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
classOf[org.apache.hadoop.hbase.client.Result])
println(hBaseRDD.count())
println("Done!")
null
}
}