在playframework中使用scala连接hbase

uqdfh47h  于 2021-06-10  发布在  Hbase
关注(0)|答案(0)|浏览(260)

嗨,我正在尝试连接到hbase从scala应用程序在播放框架。我跟随这个链接建立连接。我的应用程序运行不正常。我通过putty远程访问hbase。我在本地的windows机器上有这个播放应用程序。在应用程序中的何处和如何提及hbase服务器连接详细信息?
conf/application.conf文件:

e# This is the main configuration file for the application.

# ~~~~~

# Secret key

# ~~~~~

# The secret key is used to secure cryptographics functions.

# If you deploy your application to several instances be sure to use the same key!

application.secret="?@3Y^s/S>oCNuO7If3Mq8]U285PqOG[bh/;^WVjZ@p5=`KljrbDrg4tBG6clCPuN"

# The application languages

# ~~~~~

application.langs="en"

# Global object class

# ~~~~~

# Define the Global object class for this application.

# Default to Global in the root package.

# application.global=Global

# Router

# ~~~~~

# Define the Router object to use for this application.

# This router will be looked up first when the application is starting up,

# so make sure this is the entry point.

# Furthermore, it's assumed your route file is named properly.

# So for an application router like `conf/my.application.Router`,

# you may need to define a router file `my.application.routes`.

# Default to Routes in the root package (and `conf/routes`)

# application.router=my.application.Routes

# Database configuration

# ~~~~~

# You can declare as many datasources as you want.

# By convention, the default datasource is named `default`

# 

# db.default.driver=org.h2.Driver

# db.default.url="jdbc:h2:mem:play"

# db.default.user=sa

# db.default.password=""

# 

# You can expose this datasource via JNDI if needed (Useful for JPA)

# db.default.jndiName=DefaultDS

# Evolutions

# ~~~~~

# You can disable evolutions if needed

# evolutionplugin=disabled

# Ebean configuration

# ~~~~~

# You can declare as many Ebean servers as you want.

# By convention, the default server is named `default`

# 

# ebean.default="models.*"

# Logger

# ~~~~~

# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .

# Root logger:

logger.root=ERROR

# Logger used by the framework:

logger.play=INFO

# Logger provided to your application:

logger.application=DEBUG

错误:
http://localhost:9000为我提供了带有一个窗体和“添加”按钮的网页。当我点击“添加”按钮时,它会将我重定向到http://localhost:9000/栏url并在网页本身上给出以下错误

Bad request
For request 'POST /bars' [Expecting xml body]

控制台上没有错误日志。
my\play hbase\app\controllers\application.scala如下所示:

package controllers

import play.api.mvc.{Action, Controller}
import org.apache.hadoop.hbase.{HColumnDescriptor, HTableDescriptor, HBaseConfiguration}
import org.apache.hadoop.hbase.client._
import org.apache.hadoop.hbase.util.Bytes
import play.api.Logger
import play.api.libs.json.Json
import java.util.UUID
import scala.collection.JavaConversions._

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.client.{ConnectionFactory,HTable,Put,HBaseAdmin}

object Application extends Controller {

  val barsTableName = "bars"
  val family = Bytes.toBytes("all")
  val qualifier = Bytes.toBytes("json")

  lazy val hbaseConfig = {
    println("Hi .... hbaseConfig ... START")
    val conf:Configuration = HBaseConfiguration.create()
    conf.set("hbase.zookeeper.quorum", "xxx.xxx.xxx.xxx") // xxx.xxx.xxx.xxx IP address of my Cloudera virtual machine.
    conf.set("hbase.zookeeper.property.clientPort", "2181")

    val hbaseAdmin = new HBaseAdmin(conf)

    // create a table in HBase if it doesn't exist
    if (!hbaseAdmin.tableExists(barsTableName)) {
      val desc = new HTableDescriptor(barsTableName)
      desc.addFamily(new HColumnDescriptor(family))
      hbaseAdmin.createTable(desc)
      Logger.info("bars table created")
    }

    // return the HBase config
    println("Hi .... hbaseConfig ... END")
    conf    
  }

  def index = Action {
    // return the server-side generated webpage from app/views/index.scala.html
    println("Hi .... index ... START")
    Ok(views.html.index("Play Framework + HBase"))
  }

  def addBar() = Action(parse.json) { request =>
    // create a new row in the table that contains the JSON sent from the client
    println("Hi .... addBar ... START")
    val table = new HTable(hbaseConfig, barsTableName)
    val put1 = new Put(Bytes.toBytes(UUID.randomUUID().toString))
    put1.add(family, qualifier, Bytes.toBytes(request.body.toString()))
    table.put(put1)
    table.close()
    println("Hi .... addBar ... END")
    Ok
  }

  def getBars = Action {
    // query the table and return a JSON list of the bars in the table
    val table = new HTable(hbaseConfig, barsTableName)
    val scan = new Scan()
    scan.addColumn(family, qualifier)
    val scanner = table.getScanner(scan)
    val results = try {
      scanner.toList.map {result =>
        Json.parse(result.getValue(family, qualifier))
      }
    } finally {
      scanner.close()
      table.close()
    }
    Ok(Json.toJson(results))
  }

}

我的\play hbase\conf\routes文件如下所示:


# Routes

# This file defines all application routes (Higher priority routes first)

# ~~~~

GET        /                    controllers.Application.index
GET        /bars                controllers.Application.getBars
POST       /bars                controllers.Application.addBar

# Map static resources from the /public folder to the /assets URL path

GET        /assets/*file        controllers.Assets.at(path="/public", file)
GET        /webjars/*file       controllers.WebJarAssets.at(file)

我在application.scala文件中添加了println()语句来检查流。只是打印:

Hi .... index ... START
Hi .... index ... START

我的\play hbase\app\views\index.scala文件如下所示:

@(title: String)
<!DOCTYPE html>

<html>
<head>
    <title>@title</title>
    <link rel='shortcut icon' type='image/png' href='@routes.Assets.at("images/favicon.png")'>
    <link rel='stylesheet' href='@routes.WebJarAssets.at(WebJarAssets.locate("bootstrap.min.css"))'>
    <link rel='stylesheet' href='@routes.Assets.at("stylesheets/index.css")'>
    <script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>
    <script type='text/javascript' src='@routes.Assets.at("javascripts/index.min.js")'></script>
</head>
<body>
    <div class="navbar navbar-fixed-top">
        <div class="navbar-inner">
            <div class="container-fluid">
                <a id="titleLink" class="brand" href="/">@title</a>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="well">
            <h3>Bars</h3>
            <ul id="bars"></ul>
            <hr>
            <h3>Add a Bar</h3>
            <form id="addBar" action="@routes.Application.addBar()" method="post">
                <input id="barName" placeholder="Name">
                <button>Add Bar</button>
            </form>
        </div>
    </div>
</body>
</html>

my\play hbase\build.sbt如下所示:

name := "play-hbase"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  // Select Play modules
  //jdbc,      // The JDBC connection pool and the play.api.db API
  //anorm,     // Scala RDBMS Library
  //javaJdbc,  // Java database API
  //javaEbean, // Java Ebean plugin
  //javaJpa,   // Java JPA plugin
  //filters,   // A set of built-in filters
  //javaCore,  // The core Java API
  // WebJars pull in client-side web libraries
  "org.webjars" %% "webjars-play" % "2.2.0-RC1-1",
  "org.webjars" % "bootstrap" % "2.3.1",
  // HBase
  //"org.apache.hadoop" % "hadoop-core" % "1.2.1",
  //"org.apache.hbase" % "hbase" % "0.94.11",
  "org.apache.hadoop" % "hadoop-common" % "2.6.0",
  "org.apache.hadoop" % "hadoop-client" % "2.6.0",
  "org.apache.hbase" % "hbase" % "1.2.0",
  "org.apache.hbase" % "hbase-client" % "1.2.0",
  "org.apache.hbase" % "hbase-common" % "1.2.0",
  "org.apache.hbase" % "hbase-server" % "1.2.0",
  "org.slf4j" % "slf4j-log4j12" % "1.7.5"
  // Add your own project dependencies in the form:
  // "group" % "artifact" % "version"
)

play.Project.playScalaSettings

my\play hbase\project\plugins.sbt如下所示:

// Comment to get more information during initialization
logLevel := Level.Warn

// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0-RC1")

我的\play hbase\project\build.properties如下所示:


# Activator-generated Properties

# Sat Oct 22 14:55:10 UTC 2016

template.uuid=148fc4a0-928a-42a0-81c8-98d83d1a656d
sbt.version=0.13.0

谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题