【问题标题】:Open multiple Hbase tables Spark打开多个 Hbase 表 Spark
【发布时间】:2015-09-01 06:36:32
【问题描述】:

我是 Spark 的新用户,我想将流数据保存到多个 Hbase 表中。当我想将我的数据保存在一个数据中时,我没有遇到任何问题,但我无法使用多个数据。

我尝试创建多个 HTable,但后来我注意到这个类仅用于与单个 HBase 表进行通信。

有什么办法吗?

这是我尝试创建多个 Htables 的地方(当然不起作用,但这是我的想法)

 //HBASE Tables
val tableFull = "table1"
val tableCategoricalFiltered = "table2"

// Add local HBase conf
val conf1 = HBaseConfiguration.create()
val conf2 = HBaseConfiguration.create()

conf1.set(TableInputFormat.INPUT_TABLE, tableFull)
conf2.set(TableInputFormat.INPUT_TABLE, tableCategoricalFiltered)

//Opening Tables
val tableInputFeatures = new HTable(conf1, tableFull)
val tableCategoricalFilteredFeatures = new HTable(conf2, tableCategoricalFiltered)

这是我尝试使用它们的地方(虽然只有一个 HTable 可以工作)

events.foreachRDD { event =>

    var j = 0
    event.foreach { feature =>

            if ( j <= 49 ) {
                    println("Feature " + j + " : " + featuresDic(j))
                    println(feature)

                    val p_full = new Put(new String("stream " + row_full).getBytes())
                    p_full.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
                    tableInputFeatures.put(p_full)

                    if ( j != 26 || j != 27 || j != 28 || j != 29 ) {

                            val p_cat = new Put(new String("stream " + row_categorical).getBytes())
                            p_cat.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
                            tableCategoricalFilteredFeatures.put(p_cat)
                    }else{
                            j = 0
                            row_full = row_full + 1

                            println("Feature " + j + " : " + featuresDic(j))
                            println(feature)

                            val p_full = new Put(new String("stream " + row_full).getBytes())
                            p_full.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
                            tableInputFeatures.put(p_full)

                            val p_cat = new Put(new String("stream " + row_categorical).getBytes())
                            p_cat.add(featuresDic(j).getBytes(), "1".getBytes(), new String(feature).getBytes())
                            tableCategoricalFilteredFeatures.put(p_cat)
                    }

                    j  = j + 1
            }
    }

【问题讨论】:

  • 如果您提供代码示例,您的问题可能会得到更多关注。
  • 感谢您的提示 Eric,我刚刚提供了我的代码 :)

标签: apache-spark hbase spark-streaming


【解决方案1】:

我确认有一种方法效果很好,那就是使用 hbase-rdd 库。 https://github.com/unicredit/hbase-rdd

它易于使用。使用情况请参考https://github.com/unicredit/hbase-rdd#writing-to-hbase。

您可以尝试 MultiTableOutputFormat,因为我确认它适用于传统的 mapreduce。我还没有从 Spark 中使用它。

【讨论】:

  • 这个库看起来很有趣。我一直在玩它从文件中读取数据,但我不知道如何从流数据创建 RDD[(String, Map[String, A])]。由于在 event.foreach 块中,我一一获取数据。也许你知道如何做到这一点?非常感谢:)
  • 我没有使用 Spark 流,但基本方法是'使用“转换”将 RDD 转换为 RDD',而不是使用 foreach。
猜你喜欢
  • 1970-01-01
  • 2017-12-20
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
相关资源
最近更新 更多