我有json4s跟随Scala控制台会话,我试图从解析的json中提取String值:
scala> import org.json4s._
import org.json4s._
scala> import org.json4s.native.JsonMethods._
import org.json4s.native.JsonMethods._
scala> val s = """ {"a": "hello"} """
s: String = " {"a": "hello"} "
scala> val json = parse(s)
json: org.json4s.JValue = JObject(List((a,JString(hello))))
scala> json \ "a"
res0: org.json4s.JValue = JString(hello)
scala> res0.extract[String]
<console>:17: error: No org.json4s.Formats found. Try to bring an instance of org.json4s.Formats in scope or use the org.json4s.DefaultFormats.
res0.extract[String]
^
scala> import org.json4s.Formats._
import org.json4s.Formats._
scala> res0.extract[String]
<console>:20: error: No org.json4s.Formats found. Try to bring …Run Code Online (Sandbox Code Playgroud) 我看到了一个问题:从数组中创建ArrayList
但是,当我使用以下代码尝试该解决方案时,它并不适用于所有情况:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class ToArrayList {
public static void main(String[] args) {
// this works
String[] elements = new String[] { "Ryan", "Julie", "Bob" };
List<String> list = new ArrayList<String>(Arrays.asList(elements));
System.out.println(list);
// this works
List<Integer> intList = null;
intList = Arrays.asList(3, 5);
System.out.println(intList);
int[] intArray = new int[] { 0, 1 };
// this doesn't work!
intList = new ArrayList<Integer>(Arrays.asList(intArray));
System.out.println(intList);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?代码不应该intList = new ArrayList<Integer>(Arrays.asList(intArray));编译得很好吗?
我可以像这样创建一个sbt项目:
$ mkdir project1
$ cd project1
$ sbt
Loading /usr/share/sbt/bin/sbt-launch-lib.bash
> set name := "project1"
[info] Defining *:name
...
> set scalaVersion :="2.10.2"
[info] Defining *:scalaVersion
...
> set version :="1.0"
[info] Defining *:version
...
> session save
[info] Reapplying settings...
> exit
Run Code Online (Sandbox Code Playgroud)
这build.sbt将为项目创建文件.
$ cat build.sbt
name := "project1"
scalaVersion :="2.10.2"
version :="1.0"
Run Code Online (Sandbox Code Playgroud)
现在是否有任何命令行来做同样的事情?就像是:
sbt new_project "name" version scala_version
Run Code Online (Sandbox Code Playgroud)
EDIT1
我想出了另一种方式,即创建项目文件夹:
$ mkdir project1
$ cd project1/
Run Code Online (Sandbox Code Playgroud)
更新项目详情:
$ sbt 'set name := "project1"' 'set scalaVersion …Run Code Online (Sandbox Code Playgroud) 我有这两个错误:
Error:(39, 20) Cannot find an implicit ExecutionContext. You might pass
an (implicit ec: ExecutionContext) parameter to your method
or import scala.concurrent.ExecutionContext.Implicits.global.
val pipeline = sendReceive
^
Error:(39, 20) not enough arguments for method sendReceive: (implicit refFactory: akka.actor.ActorRefFactory, implicit executionContext: scala.concurrent.ExecutionContext, implicit futureTimeout: akka.util.Timeout)spray.client.pipelining.SendReceive.
Unspecified value parameter executionContext.
val pipeline = sendReceive
^
Run Code Online (Sandbox Code Playgroud)
我的代码是:
import scala.util.{Success, Failure}
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.can.Http
import spray.client.pipelining._
import spray.util._
import argonaut._, Argonaut._
object test {
case …Run Code Online (Sandbox Code Playgroud) 我在python中有一个使用tqdm输出进度条的程序,如下所示:
0%| | 1/782 [00:02<31:00, 2.38s/it, loss=0.763 ]
17%|?? | 134/782 [00:19<01:21, 7.98it/s, loss=0.375 ]
100%|??????????| 782/782 [03:50<00:00, 2.73it/s, loss=0.0193]
100%|??????????| 779/782 [03:47<00:00, 4.33it/s, loss=0.0175]
100%|??????????| 780/782 [03:48<00:00, 4.08it/s, loss=0.0172]
100%|??????????| 781/782 [03:48<00:00, 3.83it/s, loss=0.0195]
Run Code Online (Sandbox Code Playgroud)
让我们进入第二行:
17%|?? | 134/782 [00:19<01:21, 7.98it/s, loss=0.375 ]
Run Code Online (Sandbox Code Playgroud)
字段顺序为:
|?? | : 进度条134/782:迭代的项目数超过了项目总数。[00:19<01:21, 7.98it/s, loss=0.375 ]:让我们分别在下面进行分解。
00:19<01:21:<<HERE>>无法解决这个问题。7.98it/s:每秒迭代loss=0.375:正如标签上所说,这是损失。我知道它显示进度和统计信息,例如每秒迭代,获得的损失等。但是,我无法准确地说出这种时间格式(00:19<01:21例如)在每一行中代表什么?该<标志表示什么?
我正在努力在OSX 10.8上编译PyGLPK.我已经通过自制软件安装了glpk和gmp.我已经验证/ usr/local/include中存在以下文件
gmp.h
gmpxx.h
glpk.h
Run Code Online (Sandbox Code Playgroud)
但我仍然得到以下错误.
python setup.py build
running build
running build_ext
building 'glpk' extension
clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -Isrc -I/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/glpk.c -o build/temp.macosx-10.9-x86_64-2.7/src/glpk.o -m32
clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -Isrc -I/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/lp.c -o build/temp.macosx-10.9-x86_64-2.7/src/lp.o -m32
In file included from src/lp.c:24:
src/kkt.h:30:3: error: unknown type name 'LPXKKT'
LPXKKT kkt;
^
src/lp.c:182:18: warning: implicit declaration of function 'lpx_read_model' is invalid …Run Code Online (Sandbox Code Playgroud) 在Ruby中我们可以这样做:
$ irb
>> ("aa".."bb").map { |x| x }
=> ["aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax", "ay", "az", "ba", "bb"]
Run Code Online (Sandbox Code Playgroud)
在Scala,如果我尝试相同的我得到错误:
$ scala
Welcome to Scala version 2.9.1 (OpenJDK 64-Bit Server VM, Java 1.7.0_51).
scala> ("aa" to "bb").map(x => x)
<console>:8: error: value to is not a member of java.lang.String
("aa" to "bb").map(x => x)
^
Run Code Online (Sandbox Code Playgroud)
如何在Scala中获得一系列字符串?
是否有可用于Scala的OAuth2提供程序,我可以使用Scalatra或Play2 Web框架?
我已经看到了这个答案:Scala/Lift的OAuth 2.0提供程序实现
我正在寻找提供程序库而不是OAuth2.0客户端库.
编辑:
我正在寻找Scala OAuth2.0 Provider:http://tuxdna.in/blog/2014/07/09/oauth2-dot-0-server-using-play-2-dot-0-framework-in-斯卡拉/
我有Apache Mesos 0.22.1集群(3个主服务器和5个从服务器),在HA配置和Spark 1.5.1框架中运行Cloudera HDFS(2.5.0-cdh5.3.1).
当我尝试spark-submit编译的HdfsTest.scala示例应用程序(来自Spark 1.5.1源代码)时 - 它java.lang.IllegalArgumentException: java.net.UnknownHostException: hdfs在执行程序日志中失败并出错.只有在我传递HDFS HA Path作为参数hdfs://hdfs/<file>时才会出现此错误,当我通过时hdfs://namenode1.hdfs.mesos:50071/tesfile- 一切正常.
我在启用TRACE日志记录后发现的是Spark驱动程序实际上hdfs://hdfs正确读取了URL,但Spark执行器却没有.
我的Scala应用代码:
import org.apache.spark._
object HdfsTest {
def main(args: Array[String]) {
val sparkConf = new SparkConf().setAppName("HdfsTest")
val sc = new SparkContext(sparkConf)
val file = sc.textFile(args(0))
val mapped = file.map(s => s.length).cache()
for (iter <- 1 to 10) {
val start = System.currentTimeMillis()
for (x <- mapped) { x + 2 }
val end = System.currentTimeMillis()
println("Iteration " + …Run Code Online (Sandbox Code Playgroud) 我使用scala版本: Scala code runner version 2.9.2-unknown-unknown -- Copyright 2002-2011, LAMP/EPFL
我在这里尝试深度大小写匹配构造:http://ofps.oreilly.com/titles/9780596155957/RoundingOutTheEssentials.html,代码如下match-deep.scala:
class Role
case object Manager extends Role
case object Developer extends Role
case class Person(name:String, age: Int, role: Role)
val alice = new Person("Alice", 25, Developer)
val bob = new Person("Bob", 32, Manager)
val charlie = new Person("Charlie", 32, Developer)
for( person <- List(alice, bob, charlie) ) {
person match {
case (id, p @ Person(_, _, Manager)) => println("%s is overpaid".format(p))
case …Run Code Online (Sandbox Code Playgroud) scala ×7
python ×2
apache-spark ×1
argonaut ×1
arrays ×1
collections ×1
generics ×1
glpk ×1
hdfs ×1
java ×1
json4s ×1
macos ×1
mesos ×1
mesosphere ×1
oauth-2.0 ×1
sbt ×1
scala-2.9 ×1
scalatra ×1
spray-client ×1
spray-json ×1
tqdm ×1