用sparkscala读取hbase表的方法

gv8xihay  于 2021-06-09  发布在  Hbase
关注(0)|答案(1)|浏览(393)

我正在使用catalog方法从hbase读取数据并将其存储到dataframe中,方法如下:使用spark读取hbase table with where子句,但我想知道是否还有其他有效的方法来解决此问题:
扫描hbase表
扫描hbase表\u b(Map表)
检查表\u b中是否存在列\u 1值,如果是,则从Map表中获取父\u id
如果不是,则检查表\u b中的列\u 2,如果是,则从Map表中获取父\u id
将结果保存到文件中。
我可以使用上面的方法来实现这一点,但是因为我使用的是像下面这样的连接,所以从连接b中选择*,其中(如果a.duns为null,那么a.ig else a.duns end)=b.rowkey
这需要永远
请帮忙

9fkzdhlc

9fkzdhlc1#

import org.apache.hadoop.hbase.{HBaseConfiguration, 

HTableDescriptor,HColumnDescriptor,HConstants,TableName,CellUtil}

import org.apache.hadoop.hbase.client.{HBaseAdmin, 

Result,Put,HTable,ConnectionFactory,Connection,Get,Scan}

import org.apache.hadoop.hbase.io.ImmutableBytesWritable

import org.apache.hadoop.hbase.mapreduce.TableInputFormat

import org.apache.hadoop.hbase.util.Bytes

      val hconf = HBaseConfiguration.create()

      hconf.set("hbase.zookee per.quorum","localhost")

      hconf.set("hbase.zookeeper.property.clientPort","2181") 

      val admin = new HBaseAdmin(hconf)

      val hconn=ConnectionFactory.createConnection(hconf)

      var tabName_string= admin.getTableNames("student")(0)   // enter table name

      val table = new HTable(hconf,tabName_string)  // create table connection

      var data= table.get(new Get(Bytes.toBytes("row-id97")))   // row ID

      def getHBaseRowData (x: org.apache.hadoop.hbase.Cell, hint: Int )=  { 

      if(hint == 1){
                       ((Bytes.toString(x.getRow())), Bytes.toString(CellUtil.cloneQualifier(x)))

                    } else if(hint == 2) { 
                        ((Bytes.toString(x.getRow())),Bytes.toString(CellUtil.cloneValue(x))) 

                    } else if(hint == 3) { 
                        ((Bytes.toString(x.getRow())),Bytes.toString(CellUtil.cloneFamily(x))) 

                    } else if(hint == 4) { 
                    ((Bytes.toString(x.getRow())),(Bytes.toString(CellUtil.cloneQualifier(x))), (Bytes.toString(CellUtil.cloneFamily(x))), (Bytes.toString(CellUtil.cloneValue(x)))) 

                    } else 

                      ("Wrong Hint")

            }

       data.rawCells().foreach(x=> println(getHBaseRowData(x,4)))

相关问题