Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在playframework中使用Scala连接HBase_Scala_Playframework 2.0_Hbase_Apache Zookeeper - Fatal编程技术网

在playframework中使用Scala连接HBase

在playframework中使用Scala连接HBase,scala,playframework-2.0,hbase,apache-zookeeper,Scala,Playframework 2.0,Hbase,Apache Zookeeper,您好,我正在尝试从Play framework中的Scala应用程序连接到HBase。我正在跟踪以建立连接。我的应用程序运行不正常。我正在通过putty远程访问Hbase。我在本地windows计算机上安装了此播放应用程序。在应用程序中的何处以及如何提及HBase服务器连接详细信息 conf/application.conf: e# This is the main configuration file for the application. # ~~~~~ # Secret key # ~

您好,我正在尝试从Play framework中的Scala应用程序连接到HBase。我正在跟踪以建立连接。我的应用程序运行不正常。我正在通过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
错误:
给我一个表单和添加按钮的网页。当我点击“添加”按钮时,它会将我重定向到url,并在网页本身上给出以下错误

Bad request
For request 'POST /bars' [Expecting xml body]
控制台上没有错误日志

我的\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))
  }

}
# 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)
我的\play hbase\conf\routes文件如下所示:

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))
  }

}
# 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>
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
// 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")
#Activator-generated Properties
#Sat Oct 22 14:55:10 UTC 2016
template.uuid=148fc4a0-928a-42a0-81c8-98d83d1a656d
sbt.version=0.13.0
我的\play hbase\project\plugins.sbt如下所示:

@(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>
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
// 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")
#Activator-generated Properties
#Sat Oct 22 14:55:10 UTC 2016
template.uuid=148fc4a0-928a-42a0-81c8-98d83d1a656d
sbt.version=0.13.0
我的\play hbase\project\build.properties如下所示:

@(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>
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
// 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")
#Activator-generated Properties
#Sat Oct 22 14:55:10 UTC 2016
template.uuid=148fc4a0-928a-42a0-81c8-98d83d1a656d
sbt.version=0.13.0

谢谢

怎么了?配置文件?@pedroct92我用application.conf文件详细信息和错误更新了我的帖子。我想你应该看看你提到的教程。如果您有错误的请求,这意味着您的路由和如何管理应用程序上的请求存在问题。此外,请记住检查您正在使用的play版本。根据教程,访问HBase的配置在此@pedroct92上。我更新了我的问题,提供了更多详细信息。我还使用
Application.scala
来点击HBase。当我更新我的问题时,我使用println()检查流程。它根本就没有碰到我的HBase配置代码,只是在两者之间停下来。