问:
Sparkling Water fails to create h2oContext in simple spark project
I am setting up for the first time Sparkling Water on a standalone cluster running spark 2.2. I have run Sparkling Water on such a cluster before via R (using rsparkling + sparklyr + h2o), but am having issues setting this up as a spark application (in scala).

The app is built with Maven, so I have added the latest sparkling water dependancy:

```<code>    <dependency>
        <groupId>ai.h2o</groupId>
        <artifactId>sparkling-water-core_2.11</artifactId>
        <version>2.2.2</version>
    </dependency>
</code>```

Then the app code is as follows:

```<code>package com.me.app

import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.h2o._
import water.Key
import water.fvec.Frame

object sparklingWaterH2o {

  def sparklingWaterH2o(): Unit = {

    val sparkSession = SparkSession
      .builder()
      .master("spark://clsuter.address:0077")
      .appName("sparklingWaterH2o")
      .config("spark.executor.memory", "32G")
      .config("spark.executor.cores", "5")
      .config("spark.cores.max", "40")
      .config("spark.ext.h2o.nthreads", "40")
      .config("spark.jars", "/path/to/fat/jar/app-1.0-SNAPSHOT-jar-with-dependencies.jar")
      .getOrCreate()

    val h2oContext = H2OContext.getOrCreate(sparkSession)

    import h2oContext._

    val df = Seq(
      (1, "2014/07/31 23:00:01"),
      (1, "2016/12/09 10:12:43")).toDF("id", "date")

    val h2oTrainFrame = h2oContext.asH2OFrame(df)

    println(s"h2oContext = ${h2oContext.toString()}")
</code>```

I then compile the fat jar to send to the cluster, however the h2oContext never gets created and the SparkContext gets shut down with exit code 255. The app exiting with no error codes before an h2o context is created - the only potentially useful message is IP address not found on this machine.

I've tried this with Sparkling Water version 2.2.0 and get the same issues, also tried adding dependencies for sparkling-water-ml and sparkling-water-repl, as well as adding all the h2o core dependencies (though assuming these are not needed as they are integrated into sparkling water?). See log file below.

```objc[39611]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x10ab4b4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10bb724e0). One of the two will be used. Which one is undefined.
Usinrg Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/username/.m2/repository/org/slf4j/slf4j-log4j12/1.7.16/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/username/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.6.2/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
17/11/17 10:16:01 INFO SparkContext: Running Spark version 2.2.0
17/11/17 10:16:02 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
17/11/17 10:16:02 INFO SparkContext: Submitted application: sparklingWaterH2o
17/11/17 10:16:02 INFO SecurityManager: Changing view acls to: username
17/11/17 10:16:02 INFO SecurityManager: Changing modify acls to: username
17/11/17 10:16:02 INFO SecurityManager: Changing view acls groups to:
17/11/17 10:16:02 INFO SecurityManager: Changing modify acls groups to:
17/11/17 10:16:02 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users  with view permissions: Set(username); groups with view permissions: Set(); users  with modify permissions: Set(username); groups with modify permissions: Set()
17/11/17 10:16:03 INFO Utils: Successfully started service 'sparkDriver' on port 53775.
17/11/17 10:16:03 INFO SparkEnv: Registering MapOutputTracker
17/11/17 10:16:03 INFO SparkEnv: Registering BlockManagerMaster
17/11/17 10:16:03 INFO BlockManagerMasterEndpoint: Using org.apache.spark.storage.DefaultTopologyMapper for getting topology information
17/11/17 10:16:03 INFO BlockManagerMasterEndpoint: BlockManagerMasterEndpoint up
17/11/17 10:16:03 INFO DiskBlockManager: Created local directory at /private/var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/blockmgr-d29de5c5-9116-4abf-812c-04ca680781fe
17/11/17 10:16:03 INFO MemoryStore: MemoryStore started with capacity 1002.3 MB
17/11/17 10:16:03 INFO SparkEnv: Registering OutputCommitCoordinator
17/11/17 10:16:03 INFO Utils: Successfully started service 'SparkUI' on port 4040.
17/11/17 10:16:03 INFO SparkUI: Bound SparkUI to 0.0.0.0, and started at http://192.168.103.46:4040
17/11/17 10:16:03 INFO SparkContext: Added JAR /path/to/app/target/app-1.0-SNAPSHOT-jar-with-dependencies.jar at spark://192.168.103.46:53775/jars/app-1.0-SNAPSHOT-jar-with-dependencies.jar with timestamp 1510913763424
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Connecting to master spark://rnd-centos7-ben-31.nominet.org.uk:7077...
17/11/17 10:16:03 INFO TransportClientFactory: Successfully created connection to rnd-centos7-ben-31.nominet.org.uk/XXX.XXX.211.31:7077 after 26 ms (0 ms spent in bootstraps)
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Connected to Spark cluster with app ID app-20171117101603-0031
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/0 on worker-20171013100055-XXX.XXX.211.30-33565 (XXX.XXX.211.30:33565) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/0 on hostPort XXX.XXX.211.30:33565 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/1 on worker-20171013100055-XXX.XXX.211.33-34424 (XXX.XXX.211.33:34424) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/1 on hostPort XXX.XXX.211.33:34424 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/2 on worker-20171013100055-XXX.XXX.211.31-37513 (XXX.XXX.211.31:37513) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/2 on hostPort XXX.XXX.211.31:37513 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/3 on worker-20171013100054-XXX.XXX.211.32-36797 (XXX.XXX.211.32:36797) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/3 on hostPort XXX.XXX.211.32:36797 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/2 is now RUNNING
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/1 is now RUNNING
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/3 is now RUNNING
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/0 is now RUNNING
17/11/17 10:16:03 INFO Utils: Successfully started service 'org.apache.spark.network.netty.NettyBlockTransferService' on port 53777.
17/11/17 10:16:03 INFO NettyBlockTransferService: Server created on 192.168.103.46:53777
17/11/17 10:16:03 INFO BlockManager: Using org.apache.spark.storage.RandomBlockReplicationPolicy for block replication policy
17/11/17 10:16:03 INFO BlockManagerMaster: Registering BlockManager BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:03 INFO BlockManagerMasterEndpoint: Registering block manager 192.168.103.46:53777 with 1002.3 MB RAM, BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:03 INFO BlockManagerMaster: Registered BlockManager BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:03 INFO BlockManager: Initialized BlockManager: BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.31:46906) with ID 2
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.30:54738) with ID 0
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.31:45376 with 8.4 GB RAM, BlockManagerId(2, XXX.XXX.211.31, 45376, None)
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.30:34172 with 8.4 GB RAM, BlockManagerId(0, XXX.XXX.211.30, 34172, None)
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.32:53076) with ID 3
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.33:47478) with ID 1
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.32:34360 with 8.4 GB RAM, BlockManagerId(3, XXX.XXX.211.32, 34360, None)
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.33:34342 with 8.4 GB RAM, BlockManagerId(1, XXX.XXX.211.33, 34342, None)
17/11/17 10:16:33 INFO StandaloneSchedulerBackend: SchedulerBackend is ready for scheduling beginning after waiting maxRegisteredResourcesWaitingTime: 30000(ms)
17/11/17 10:16:33 INFO SharedState: Setting hive.metastore.warehouse.dir ('null') to the value of spark.sql.warehouse.dir ('file:/path/to/app/spark-warehouse/').
17/11/17 10:16:33 INFO SharedState: Warehouse path is 'file:/path/to/app/spark-warehouse/'.
17/11/17 10:16:34 INFO StateStoreCoordinatorRef: Registered StateStoreCoordinator endpoint
17/11/17 10:16:34 WARN InternalH2OBackend: Increasing 'spark.locality.wait' to value 30000
17/11/17 10:16:34 WARN InternalH2OBackend: Due to non-deterministic behavior of Spark broadcast-based joins
We recommend to disable them by
configuring `spark.sql.autoBroadcastJoinThreshold` variable to value `-1`:
sqlContext.sql("SET spark.sql.autoBroadcastJoinThreshold=-1")
17/11/17 10:16:34 INFO InternalH2OBackend: Starting H2O services: Sparkling Water configuration:
  backend cluster mode : internal
  workers              : None
  cloudName            : sparkling-water-username_app-20171117101603-0031
  flatfile             : true
  clientBasePort       : 54321
  nodeBasePort         : 54321
  cloudTimeout         : 60000
  h2oNodeLog           : INFO
  h2oClientLog         : WARN
  nthreads             : 40
  drddMulFactor        : 10
17/11/17 10:16:34 INFO SparkContext: Starting job: collect at SpreadRDDBuilder.scala:105
17/11/17 10:16:34 INFO DAGScheduler: Got job 0 (collect at SpreadRDDBuilder.scala:105) with 41 output partitions
17/11/17 10:16:34 INFO DAGScheduler: Final stage: ResultStage 0 (collect at SpreadRDDBuilder.scala:105)
17/11/17 10:16:34 INFO DAGScheduler: Parents of final stage: List()
17/11/17 10:16:34 INFO DAGScheduler: Missing parents: List()
17/11/17 10:16:34 INFO DAGScheduler: Submitting ResultStage 0 (MapPartitionsRDD[1] at mapPartitionsWithIndex at SpreadRDDBuilder.scala:102), which has no missing parents
17/11/17 10:16:34 INFO MemoryStore: Block broadcast_0 stored as values in memory (estimated size 2.1 KB, free 1002.3 MB)
17/11/17 10:16:34 INFO MemoryStore: Block broadcast_0_piece0 stored as bytes in memory (estimated size 1379.0 B, free 1002.3 MB)
17/11/17 10:16:34 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on 192.168.103.46:53777 (size: 1379.0 B, free: 1002.3 MB)
17/11/17 10:16:34 INFO SparkContext: Created broadcast 0 from broadcast at DAGScheduler.scala:1006
17/11/17 10:16:34 INFO DAGScheduler: Submitting 41 missing tasks from ResultStage 0 (MapPartitionsRDD[1] at mapPartitionsWithIndex at SpreadRDDBuilder.scala:102) (first 15 tasks are for partitions Vector(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14))
17/11/17 10:16:34 INFO TaskSchedulerImpl: Adding task set 0.0 with 41 tasks
17/11/17 10:16:34 INFO TaskSetManager: Starting task 0.0 in stage 0.0 (TID 0, XXX.XXX.211.31, executor 2, partition 0, PROCESS_LOCAL, 4829 bytes)
17/11/17 10:16:34 INFO TaskSetManager: Starting task 1.0 in stage 0.0 (TID 1, XXX.XXX.211.30, executor 0, partition 1, PROCESS_LOCAL, 4829 bytes)
...
17/11/17 10:16:34 INFO TaskSetManager: Starting task 19.0 in stage 0.0 (TID 19, XXX.XXX.211.33, executor 1, partition 19, PROCESS_LOCAL, 4829 bytes)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.30:34172 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.32:34360 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.33:34342 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.31:45376 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added rdd_0_13 in memory on XXX.XXX.211.30:34172 (size: 32.0 B, free: 8.4 GB)
...
17/11/17 10:16:43 INFO TaskSetManager: Finished task 40.0 in stage 0.0 (TID 40) in 29 ms on XXX.XXX.211.33 (executor 1) (41/41)
17/11/17 10:16:43 INFO TaskSchedulerImpl: Removed TaskSet 0.0, whose tasks have all completed, from pool
17/11/17 10:16:43 INFO DAGScheduler: ResultStage 0 (collect at SpreadRDDBuilder.scala:105) finished in 8.913 s
17/11/17 10:16:43 INFO DAGScheduler: Job 0 finished: collect at SpreadRDDBuilder.scala:105, took 9.072610 s
17/11/17 10:16:43 INFO ParallelCollectionRDD: Removing RDD 0 from persistence list
17/11/17 10:16:43 INFO BlockManager: Removing RDD 0
17/11/17 10:16:43 INFO SpreadRDDBuilder: Detected 4 spark executors for 4 H2O workers!
17/11/17 10:16:43 INFO InternalH2OBackend: Launching H2O on following 4 nodes: (0,XXX.XXX.211.30,-1),(1,XXX.XXX.211.33,-1),(2,XXX.XXX.211.31,-1),(3,XXX.XXX.211.32,-1)
17/11/17 10:16:43 INFO SparkContext: Starting job: collect at InternalBackendUtils.scala:163
17/11/17 10:16:43 INFO DAGScheduler: Got job 1 (collect at InternalBackendUtils.scala:163) with 4 output partitions
17/11/17 10:16:43 INFO DAGScheduler: Final stage: ResultStage 1 (collect at InternalBackendUtils.scala:163)
17/11/17 10:16:43 INFO DAGScheduler: Parents of final stage: List()
17/11/17 10:16:43 INFO DAGScheduler: Missing parents: List()
17/11/17 10:16:43 INFO DAGScheduler: Submitting ResultStage 1 (MapPartitionsRDD[3] at map at InternalBackendUtils.scala:100), which has no missing parents
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_1 stored as values in memory (estimated size 3.1 KB, free 1002.3 MB)
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_1_piece0 stored as bytes in memory (estimated size 2029.0 B, free 1002.3 MB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on 192.168.103.46:53777 (size: 2029.0 B, free: 1002.3 MB)
17/11/17 10:16:43 INFO SparkContext: Created broadcast 1 from broadcast at DAGScheduler.scala:1006
17/11/17 10:16:43 INFO DAGScheduler: Submitting 4 missing tasks from ResultStage 1 (MapPartitionsRDD[3] at map at InternalBackendUtils.scala:100) (first 15 tasks are for partitions Vector(0, 1, 2, 3))
17/11/17 10:16:43 INFO TaskSchedulerImpl: Adding task set 1.0 with 4 tasks
17/11/17 10:16:43 INFO TaskSetManager: Starting task 2.0 in stage 1.0 (TID 41, XXX.XXX.211.31, executor 2, partition 2, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 0.0 in stage 1.0 (TID 42, XXX.XXX.211.30, executor 0, partition 0, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 3.0 in stage 1.0 (TID 43, XXX.XXX.211.32, executor 3, partition 3, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 1.0 in stage 1.0 (TID 44, XXX.XXX.211.33, executor 1, partition 1, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.30:34172 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.31:45376 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.33:34342 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.32:34360 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 0.0 in stage 1.0 (TID 42) in 349 ms on XXX.XXX.211.30 (executor 0) (1/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 2.0 in stage 1.0 (TID 41) in 358 ms on XXX.XXX.211.31 (executor 2) (2/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 3.0 in stage 1.0 (TID 43) in 394 ms on XXX.XXX.211.32 (executor 3) (3/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 1.0 in stage 1.0 (TID 44) in 408 ms on XXX.XXX.211.33 (executor 1) (4/4)
17/11/17 10:16:43 INFO TaskSchedulerImpl: Removed TaskSet 1.0, whose tasks have all completed, from pool
17/11/17 10:16:43 INFO DAGScheduler: ResultStage 1 (collect at InternalBackendUtils.scala:163) finished in 0.411 s
17/11/17 10:16:43 INFO DAGScheduler: Job 1 finished: collect at InternalBackendUtils.scala:163, took 0.428038 s
17/11/17 10:16:43 INFO SparkContext: Starting job: foreach at InternalBackendUtils.scala:175
17/11/17 10:16:43 INFO DAGScheduler: Got job 2 (foreach at InternalBackendUtils.scala:175) with 4 output partitions
17/11/17 10:16:43 INFO DAGScheduler: Final stage: ResultStage 2 (foreach at InternalBackendUtils.scala:175)
17/11/17 10:16:43 INFO DAGScheduler: Parents of final stage: List()
17/11/17 10:16:43 INFO DAGScheduler: Missing parents: List()
17/11/17 10:16:43 INFO DAGScheduler: Submitting ResultStage 2 (InvokeOnNodesRDD[2] at RDD at InvokeOnNodesRDD.scala:27), which has no missing parents
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_2 stored as values in memory (estimated size 1832.0 B, free 1002.3 MB)
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_2_piece0 stored as bytes in memory (estimated size 1209.0 B, free 1002.3 MB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on 192.168.103.46:53777 (size: 1209.0 B, free: 1002.3 MB)
17/11/17 10:16:43 INFO SparkContext: Created broadcast 2 from broadcast at DAGScheduler.scala:1006
17/11/17 10:16:43 INFO DAGScheduler: Submitting 4 missing tasks from ResultStage 2 (InvokeOnNodesRDD[2] at RDD at InvokeOnNodesRDD.scala:27) (first 15 tasks are for partitions Vector(0, 1, 2, 3))
17/11/17 10:16:43 INFO TaskSchedulerImpl: Adding task set 2.0 with 4 tasks
17/11/17 10:16:43 INFO TaskSetManager: Starting task 2.0 in stage 2.0 (TID 45, XXX.XXX.211.31, executor 2, partition 2, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 0.0 in stage 2.0 (TID 46, XXX.XXX.211.30, executor 0, partition 0, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 3.0 in stage 2.0 (TID 47, XXX.XXX.211.32, executor 3, partition 3, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 1.0 in stage 2.0 (TID 48, XXX.XXX.211.33, executor 1, partition 1, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.31:45376 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.33:34342 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.32:34360 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.30:34172 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 0.0 in stage 2.0 (TID 46) in 28 ms on XXX.XXX.211.30 (executor 0) (1/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 1.0 in stage 2.0 (TID 48) in 28 ms on XXX.XXX.211.33 (executor 1) (2/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 2.0 in stage 2.0 (TID 45) in 30 ms on XXX.XXX.211.31 (executor 2) (3/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 3.0 in stage 2.0 (TID 47) in 32 ms on XXX.XXX.211.32 (executor 3) (4/4)
17/11/17 10:16:43 INFO TaskSchedulerImpl: Removed TaskSet 2.0, whose tasks have all completed, from pool
17/11/17 10:16:43 INFO DAGScheduler: ResultStage 2 (foreach at InternalBackendUtils.scala:175) finished in 0.034 s
17/11/17 10:16:43 INFO DAGScheduler: Job 2 finished: foreach at InternalBackendUtils.scala:175, took 0.043737 s
17/11/17 10:16:43 INFO InternalH2OBackend: Starting H2O client on the Spark Driver (192.168.103.46): -name sparkling-water-username_app-20171117101603-0031 -nthreads 40 -ga_opt_out -quiet -log_level WARN -log_dir /path/to/app/h2ologs/app-20171117101603-0031 -baseport 54321 -client -ip 192.168.103.46 -flatfile /var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/1510913803950-0/flatfile.txt
17/11/17 10:16:44 INFO NativeLibrary: Loaded XGBoost library from lib/osx_64/libxgboost4j.dylib (/var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/libxgboost4j2584224510491657515.dylib)
Found XGBoost backend with library: xgboost4j
Your system supports only minimal version of XGBoost (no GPUs, no multithreading)!
IP address not found on this machine
17/11/17 10:16:45 INFO SparkContext: Invoking stop() from shutdown hook
17/11/17 10:16:45 INFO SparkUI: Stopped Spark web UI at http://192.168.103.46:4040
17/11/17 10:16:45 INFO StandaloneSchedulerBackend: Shutting down all executors
17/11/17 10:16:45 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Asking each executor to shut down
17/11/17 10:16:45 INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
17/11/17 10:16:45 INFO MemoryStore: MemoryStore cleared
17/11/17 10:16:45 INFO BlockManager: BlockManager stopped
17/11/17 10:16:45 INFO BlockManagerMaster: BlockManagerMaster stopped
17/11/17 10:16:45 INFO OutputCommitCoordinator$OutputCommitCoordinatorEndpoint: OutputCommitCoordinator stopped!
17/11/17 10:16:45 INFO SparkContext: Successfully stopped SparkContext
17/11/17 10:16:45 INFO ShutdownHookManager: Shutdown hook called
17/11/17 10:16:45 INFO ShutdownHookManager: Deleting directory /private/var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/spark-51594e29-1ea0-4a4d-9aa0-dd65ef5146dd
```

答:
your exception is thrown from this line of code:
https://github.com/h2oai/h2o-3/blob/master/h2o-core/src/main/java/water/init/HostnameGuesser.java#L227

because of this condition:

```<code> if (!allowedIps.contains(addr)) {
          throw new HostnameGuessingException("IP address not found on this machine");
        }
</code>```

addr is the driver ip:

17/11/17 10:16:43 INFO InternalH2OBackend: Starting H2O client on the
  Spark Driver (192.168.103.46): -name
  sparkling-water-username_app-20171117101603-0031 -nthreads 40
  -ga_opt_out -quiet -log_level WARN -log_dir /path/to/app/h2ologs/app-20171117101603-0031 -baseport 54321 -client
  -ip 192.168.103.46 -flatfile /var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/1510913803950-0/flatfile.txt

and allowedIps is calculated with the function calcPrioritizedInetAddressList: https://github.com/h2oai/h2o-3/blob/master/h2o-core/src/main/java/water/init/HostnameGuesser.java#L161

for some reason, addr is not in allowedIps.

It's hard to know why from here, so I would recommend you to the run function calcPrioritizedInetAddressList by yourself, and try to understand the cause (it is private, but you can just copy the code)

Body:
I am setting up for the first time Sparkling Water on a standalone cluster running spark 2.2. I have run Sparkling Water on such a cluster before via R (using rsparkling + sparklyr + h2o), but am having issues setting this up as a spark application (in scala).

The app is built with Maven, so I have added the latest sparkling water dependancy:

    <dependency>
        <groupId>ai.h2o</groupId>
        <artifactId>sparkling-water-core_2.11</artifactId>
        <version>2.2.2</version>
    </dependency>

Then the app code is as follows:

package com.me.app

import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.h2o._
import water.Key
import water.fvec.Frame

object sparklingWaterH2o {

  def sparklingWaterH2o(): Unit = {

    val sparkSession = SparkSession
      .builder()
      .master("spark://clsuter.address:0077")
      .appName("sparklingWaterH2o")
      .config("spark.executor.memory", "32G")
      .config("spark.executor.cores", "5")
      .config("spark.cores.max", "40")
      .config("spark.ext.h2o.nthreads", "40")
      .config("spark.jars", "/path/to/fat/jar/app-1.0-SNAPSHOT-jar-with-dependencies.jar")
      .getOrCreate()

    val h2oContext = H2OContext.getOrCreate(sparkSession)

    import h2oContext._

    val df = Seq(
      (1, "2014/07/31 23:00:01"),
      (1, "2016/12/09 10:12:43")).toDF("id", "date")

    val h2oTrainFrame = h2oContext.asH2OFrame(df)

    println(s"h2oContext = ${h2oContext.toString()}")

I then compile the fat jar to send to the cluster, however the h2oContext never gets created and the SparkContext gets shut down with exit code 255. The app exiting with no error codes before an h2o context is created - the only potentially useful message is IP address not found on this machine.

I've tried this with Sparkling Water version 2.2.0 and get the same issues, also tried adding dependencies for sparkling-water-ml and sparkling-water-repl, as well as adding all the h2o core dependencies (though assuming these are not needed as they are integrated into sparkling water?). See log file below.

objc[39611]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x10ab4b4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10bb724e0). One of the two will be used. Which one is undefined.
Usinrg Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/username/.m2/repository/org/slf4j/slf4j-log4j12/1.7.16/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/username/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.6.2/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
17/11/17 10:16:01 INFO SparkContext: Running Spark version 2.2.0
17/11/17 10:16:02 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
17/11/17 10:16:02 INFO SparkContext: Submitted application: sparklingWaterH2o
17/11/17 10:16:02 INFO SecurityManager: Changing view acls to: username
17/11/17 10:16:02 INFO SecurityManager: Changing modify acls to: username
17/11/17 10:16:02 INFO SecurityManager: Changing view acls groups to:
17/11/17 10:16:02 INFO SecurityManager: Changing modify acls groups to:
17/11/17 10:16:02 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users  with view permissions: Set(username); groups with view permissions: Set(); users  with modify permissions: Set(username); groups with modify permissions: Set()
17/11/17 10:16:03 INFO Utils: Successfully started service 'sparkDriver' on port 53775.
17/11/17 10:16:03 INFO SparkEnv: Registering MapOutputTracker
17/11/17 10:16:03 INFO SparkEnv: Registering BlockManagerMaster
17/11/17 10:16:03 INFO BlockManagerMasterEndpoint: Using org.apache.spark.storage.DefaultTopologyMapper for getting topology information
17/11/17 10:16:03 INFO BlockManagerMasterEndpoint: BlockManagerMasterEndpoint up
17/11/17 10:16:03 INFO DiskBlockManager: Created local directory at /private/var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/blockmgr-d29de5c5-9116-4abf-812c-04ca680781fe
17/11/17 10:16:03 INFO MemoryStore: MemoryStore started with capacity 1002.3 MB
17/11/17 10:16:03 INFO SparkEnv: Registering OutputCommitCoordinator
17/11/17 10:16:03 INFO Utils: Successfully started service 'SparkUI' on port 4040.
17/11/17 10:16:03 INFO SparkUI: Bound SparkUI to 0.0.0.0, and started at http://192.168.103.46:4040
17/11/17 10:16:03 INFO SparkContext: Added JAR /path/to/app/target/app-1.0-SNAPSHOT-jar-with-dependencies.jar at spark://192.168.103.46:53775/jars/app-1.0-SNAPSHOT-jar-with-dependencies.jar with timestamp 1510913763424
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Connecting to master spark://rnd-centos7-ben-31.nominet.org.uk:7077...
17/11/17 10:16:03 INFO TransportClientFactory: Successfully created connection to rnd-centos7-ben-31.nominet.org.uk/XXX.XXX.211.31:7077 after 26 ms (0 ms spent in bootstraps)
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Connected to Spark cluster with app ID app-20171117101603-0031
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/0 on worker-20171013100055-XXX.XXX.211.30-33565 (XXX.XXX.211.30:33565) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/0 on hostPort XXX.XXX.211.30:33565 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/1 on worker-20171013100055-XXX.XXX.211.33-34424 (XXX.XXX.211.33:34424) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/1 on hostPort XXX.XXX.211.33:34424 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/2 on worker-20171013100055-XXX.XXX.211.31-37513 (XXX.XXX.211.31:37513) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/2 on hostPort XXX.XXX.211.31:37513 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor added: app-20171117101603-0031/3 on worker-20171013100054-XXX.XXX.211.32-36797 (XXX.XXX.211.32:36797) with 5 cores
17/11/17 10:16:03 INFO StandaloneSchedulerBackend: Granted executor ID app-20171117101603-0031/3 on hostPort XXX.XXX.211.32:36797 with 5 cores, 32.0 GB RAM
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/2 is now RUNNING
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/1 is now RUNNING
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/3 is now RUNNING
17/11/17 10:16:03 INFO StandaloneAppClient$ClientEndpoint: Executor updated: app-20171117101603-0031/0 is now RUNNING
17/11/17 10:16:03 INFO Utils: Successfully started service 'org.apache.spark.network.netty.NettyBlockTransferService' on port 53777.
17/11/17 10:16:03 INFO NettyBlockTransferService: Server created on 192.168.103.46:53777
17/11/17 10:16:03 INFO BlockManager: Using org.apache.spark.storage.RandomBlockReplicationPolicy for block replication policy
17/11/17 10:16:03 INFO BlockManagerMaster: Registering BlockManager BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:03 INFO BlockManagerMasterEndpoint: Registering block manager 192.168.103.46:53777 with 1002.3 MB RAM, BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:03 INFO BlockManagerMaster: Registered BlockManager BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:03 INFO BlockManager: Initialized BlockManager: BlockManagerId(driver, 192.168.103.46, 53777, None)
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.31:46906) with ID 2
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.30:54738) with ID 0
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.31:45376 with 8.4 GB RAM, BlockManagerId(2, XXX.XXX.211.31, 45376, None)
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.30:34172 with 8.4 GB RAM, BlockManagerId(0, XXX.XXX.211.30, 34172, None)
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.32:53076) with ID 3
17/11/17 10:16:05 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Registered executor NettyRpcEndpointRef(spark-client://Executor) (XXX.XXX.211.33:47478) with ID 1
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.32:34360 with 8.4 GB RAM, BlockManagerId(3, XXX.XXX.211.32, 34360, None)
17/11/17 10:16:05 INFO BlockManagerMasterEndpoint: Registering block manager XXX.XXX.211.33:34342 with 8.4 GB RAM, BlockManagerId(1, XXX.XXX.211.33, 34342, None)
17/11/17 10:16:33 INFO StandaloneSchedulerBackend: SchedulerBackend is ready for scheduling beginning after waiting maxRegisteredResourcesWaitingTime: 30000(ms)
17/11/17 10:16:33 INFO SharedState: Setting hive.metastore.warehouse.dir ('null') to the value of spark.sql.warehouse.dir ('file:/path/to/app/spark-warehouse/').
17/11/17 10:16:33 INFO SharedState: Warehouse path is 'file:/path/to/app/spark-warehouse/'.
17/11/17 10:16:34 INFO StateStoreCoordinatorRef: Registered StateStoreCoordinator endpoint
17/11/17 10:16:34 WARN InternalH2OBackend: Increasing 'spark.locality.wait' to value 30000
17/11/17 10:16:34 WARN InternalH2OBackend: Due to non-deterministic behavior of Spark broadcast-based joins
We recommend to disable them by
configuring `spark.sql.autoBroadcastJoinThreshold` variable to value `-1`:
sqlContext.sql("SET spark.sql.autoBroadcastJoinThreshold=-1")
17/11/17 10:16:34 INFO InternalH2OBackend: Starting H2O services: Sparkling Water configuration:
  backend cluster mode : internal
  workers              : None
  cloudName            : sparkling-water-username_app-20171117101603-0031
  flatfile             : true
  clientBasePort       : 54321
  nodeBasePort         : 54321
  cloudTimeout         : 60000
  h2oNodeLog           : INFO
  h2oClientLog         : WARN
  nthreads             : 40
  drddMulFactor        : 10
17/11/17 10:16:34 INFO SparkContext: Starting job: collect at SpreadRDDBuilder.scala:105
17/11/17 10:16:34 INFO DAGScheduler: Got job 0 (collect at SpreadRDDBuilder.scala:105) with 41 output partitions
17/11/17 10:16:34 INFO DAGScheduler: Final stage: ResultStage 0 (collect at SpreadRDDBuilder.scala:105)
17/11/17 10:16:34 INFO DAGScheduler: Parents of final stage: List()
17/11/17 10:16:34 INFO DAGScheduler: Missing parents: List()
17/11/17 10:16:34 INFO DAGScheduler: Submitting ResultStage 0 (MapPartitionsRDD[1] at mapPartitionsWithIndex at SpreadRDDBuilder.scala:102), which has no missing parents
17/11/17 10:16:34 INFO MemoryStore: Block broadcast_0 stored as values in memory (estimated size 2.1 KB, free 1002.3 MB)
17/11/17 10:16:34 INFO MemoryStore: Block broadcast_0_piece0 stored as bytes in memory (estimated size 1379.0 B, free 1002.3 MB)
17/11/17 10:16:34 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on 192.168.103.46:53777 (size: 1379.0 B, free: 1002.3 MB)
17/11/17 10:16:34 INFO SparkContext: Created broadcast 0 from broadcast at DAGScheduler.scala:1006
17/11/17 10:16:34 INFO DAGScheduler: Submitting 41 missing tasks from ResultStage 0 (MapPartitionsRDD[1] at mapPartitionsWithIndex at SpreadRDDBuilder.scala:102) (first 15 tasks are for partitions Vector(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14))
17/11/17 10:16:34 INFO TaskSchedulerImpl: Adding task set 0.0 with 41 tasks
17/11/17 10:16:34 INFO TaskSetManager: Starting task 0.0 in stage 0.0 (TID 0, XXX.XXX.211.31, executor 2, partition 0, PROCESS_LOCAL, 4829 bytes)
17/11/17 10:16:34 INFO TaskSetManager: Starting task 1.0 in stage 0.0 (TID 1, XXX.XXX.211.30, executor 0, partition 1, PROCESS_LOCAL, 4829 bytes)
...
17/11/17 10:16:34 INFO TaskSetManager: Starting task 19.0 in stage 0.0 (TID 19, XXX.XXX.211.33, executor 1, partition 19, PROCESS_LOCAL, 4829 bytes)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.30:34172 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.32:34360 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.33:34342 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on XXX.XXX.211.31:45376 (size: 1379.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added rdd_0_13 in memory on XXX.XXX.211.30:34172 (size: 32.0 B, free: 8.4 GB)
...
17/11/17 10:16:43 INFO TaskSetManager: Finished task 40.0 in stage 0.0 (TID 40) in 29 ms on XXX.XXX.211.33 (executor 1) (41/41)
17/11/17 10:16:43 INFO TaskSchedulerImpl: Removed TaskSet 0.0, whose tasks have all completed, from pool
17/11/17 10:16:43 INFO DAGScheduler: ResultStage 0 (collect at SpreadRDDBuilder.scala:105) finished in 8.913 s
17/11/17 10:16:43 INFO DAGScheduler: Job 0 finished: collect at SpreadRDDBuilder.scala:105, took 9.072610 s
17/11/17 10:16:43 INFO ParallelCollectionRDD: Removing RDD 0 from persistence list
17/11/17 10:16:43 INFO BlockManager: Removing RDD 0
17/11/17 10:16:43 INFO SpreadRDDBuilder: Detected 4 spark executors for 4 H2O workers!
17/11/17 10:16:43 INFO InternalH2OBackend: Launching H2O on following 4 nodes: (0,XXX.XXX.211.30,-1),(1,XXX.XXX.211.33,-1),(2,XXX.XXX.211.31,-1),(3,XXX.XXX.211.32,-1)
17/11/17 10:16:43 INFO SparkContext: Starting job: collect at InternalBackendUtils.scala:163
17/11/17 10:16:43 INFO DAGScheduler: Got job 1 (collect at InternalBackendUtils.scala:163) with 4 output partitions
17/11/17 10:16:43 INFO DAGScheduler: Final stage: ResultStage 1 (collect at InternalBackendUtils.scala:163)
17/11/17 10:16:43 INFO DAGScheduler: Parents of final stage: List()
17/11/17 10:16:43 INFO DAGScheduler: Missing parents: List()
17/11/17 10:16:43 INFO DAGScheduler: Submitting ResultStage 1 (MapPartitionsRDD[3] at map at InternalBackendUtils.scala:100), which has no missing parents
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_1 stored as values in memory (estimated size 3.1 KB, free 1002.3 MB)
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_1_piece0 stored as bytes in memory (estimated size 2029.0 B, free 1002.3 MB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on 192.168.103.46:53777 (size: 2029.0 B, free: 1002.3 MB)
17/11/17 10:16:43 INFO SparkContext: Created broadcast 1 from broadcast at DAGScheduler.scala:1006
17/11/17 10:16:43 INFO DAGScheduler: Submitting 4 missing tasks from ResultStage 1 (MapPartitionsRDD[3] at map at InternalBackendUtils.scala:100) (first 15 tasks are for partitions Vector(0, 1, 2, 3))
17/11/17 10:16:43 INFO TaskSchedulerImpl: Adding task set 1.0 with 4 tasks
17/11/17 10:16:43 INFO TaskSetManager: Starting task 2.0 in stage 1.0 (TID 41, XXX.XXX.211.31, executor 2, partition 2, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 0.0 in stage 1.0 (TID 42, XXX.XXX.211.30, executor 0, partition 0, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 3.0 in stage 1.0 (TID 43, XXX.XXX.211.32, executor 3, partition 3, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 1.0 in stage 1.0 (TID 44, XXX.XXX.211.33, executor 1, partition 1, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.30:34172 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.31:45376 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.33:34342 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_1_piece0 in memory on XXX.XXX.211.32:34360 (size: 2029.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 0.0 in stage 1.0 (TID 42) in 349 ms on XXX.XXX.211.30 (executor 0) (1/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 2.0 in stage 1.0 (TID 41) in 358 ms on XXX.XXX.211.31 (executor 2) (2/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 3.0 in stage 1.0 (TID 43) in 394 ms on XXX.XXX.211.32 (executor 3) (3/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 1.0 in stage 1.0 (TID 44) in 408 ms on XXX.XXX.211.33 (executor 1) (4/4)
17/11/17 10:16:43 INFO TaskSchedulerImpl: Removed TaskSet 1.0, whose tasks have all completed, from pool
17/11/17 10:16:43 INFO DAGScheduler: ResultStage 1 (collect at InternalBackendUtils.scala:163) finished in 0.411 s
17/11/17 10:16:43 INFO DAGScheduler: Job 1 finished: collect at InternalBackendUtils.scala:163, took 0.428038 s
17/11/17 10:16:43 INFO SparkContext: Starting job: foreach at InternalBackendUtils.scala:175
17/11/17 10:16:43 INFO DAGScheduler: Got job 2 (foreach at InternalBackendUtils.scala:175) with 4 output partitions
17/11/17 10:16:43 INFO DAGScheduler: Final stage: ResultStage 2 (foreach at InternalBackendUtils.scala:175)
17/11/17 10:16:43 INFO DAGScheduler: Parents of final stage: List()
17/11/17 10:16:43 INFO DAGScheduler: Missing parents: List()
17/11/17 10:16:43 INFO DAGScheduler: Submitting ResultStage 2 (InvokeOnNodesRDD[2] at RDD at InvokeOnNodesRDD.scala:27), which has no missing parents
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_2 stored as values in memory (estimated size 1832.0 B, free 1002.3 MB)
17/11/17 10:16:43 INFO MemoryStore: Block broadcast_2_piece0 stored as bytes in memory (estimated size 1209.0 B, free 1002.3 MB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on 192.168.103.46:53777 (size: 1209.0 B, free: 1002.3 MB)
17/11/17 10:16:43 INFO SparkContext: Created broadcast 2 from broadcast at DAGScheduler.scala:1006
17/11/17 10:16:43 INFO DAGScheduler: Submitting 4 missing tasks from ResultStage 2 (InvokeOnNodesRDD[2] at RDD at InvokeOnNodesRDD.scala:27) (first 15 tasks are for partitions Vector(0, 1, 2, 3))
17/11/17 10:16:43 INFO TaskSchedulerImpl: Adding task set 2.0 with 4 tasks
17/11/17 10:16:43 INFO TaskSetManager: Starting task 2.0 in stage 2.0 (TID 45, XXX.XXX.211.31, executor 2, partition 2, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 0.0 in stage 2.0 (TID 46, XXX.XXX.211.30, executor 0, partition 0, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 3.0 in stage 2.0 (TID 47, XXX.XXX.211.32, executor 3, partition 3, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO TaskSetManager: Starting task 1.0 in stage 2.0 (TID 48, XXX.XXX.211.33, executor 1, partition 1, NODE_LOCAL, 4821 bytes)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.31:45376 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.33:34342 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.32:34360 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO BlockManagerInfo: Added broadcast_2_piece0 in memory on XXX.XXX.211.30:34172 (size: 1209.0 B, free: 8.4 GB)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 0.0 in stage 2.0 (TID 46) in 28 ms on XXX.XXX.211.30 (executor 0) (1/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 1.0 in stage 2.0 (TID 48) in 28 ms on XXX.XXX.211.33 (executor 1) (2/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 2.0 in stage 2.0 (TID 45) in 30 ms on XXX.XXX.211.31 (executor 2) (3/4)
17/11/17 10:16:43 INFO TaskSetManager: Finished task 3.0 in stage 2.0 (TID 47) in 32 ms on XXX.XXX.211.32 (executor 3) (4/4)
17/11/17 10:16:43 INFO TaskSchedulerImpl: Removed TaskSet 2.0, whose tasks have all completed, from pool
17/11/17 10:16:43 INFO DAGScheduler: ResultStage 2 (foreach at InternalBackendUtils.scala:175) finished in 0.034 s
17/11/17 10:16:43 INFO DAGScheduler: Job 2 finished: foreach at InternalBackendUtils.scala:175, took 0.043737 s
17/11/17 10:16:43 INFO InternalH2OBackend: Starting H2O client on the Spark Driver (192.168.103.46): -name sparkling-water-username_app-20171117101603-0031 -nthreads 40 -ga_opt_out -quiet -log_level WARN -log_dir /path/to/app/h2ologs/app-20171117101603-0031 -baseport 54321 -client -ip 192.168.103.46 -flatfile /var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/1510913803950-0/flatfile.txt
17/11/17 10:16:44 INFO NativeLibrary: Loaded XGBoost library from lib/osx_64/libxgboost4j.dylib (/var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/libxgboost4j2584224510491657515.dylib)
Found XGBoost backend with library: xgboost4j
Your system supports only minimal version of XGBoost (no GPUs, no multithreading)!
IP address not found on this machine
17/11/17 10:16:45 INFO SparkContext: Invoking stop() from shutdown hook
17/11/17 10:16:45 INFO SparkUI: Stopped Spark web UI at http://192.168.103.46:4040
17/11/17 10:16:45 INFO StandaloneSchedulerBackend: Shutting down all executors
17/11/17 10:16:45 INFO CoarseGrainedSchedulerBackend$DriverEndpoint: Asking each executor to shut down
17/11/17 10:16:45 INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
17/11/17 10:16:45 INFO MemoryStore: MemoryStore cleared
17/11/17 10:16:45 INFO BlockManager: BlockManager stopped
17/11/17 10:16:45 INFO BlockManagerMaster: BlockManagerMaster stopped
17/11/17 10:16:45 INFO OutputCommitCoordinator$OutputCommitCoordinatorEndpoint: OutputCommitCoordinator stopped!
17/11/17 10:16:45 INFO SparkContext: Successfully stopped SparkContext
17/11/17 10:16:45 INFO ShutdownHookManager: Shutdown hook called
17/11/17 10:16:45 INFO ShutdownHookManager: Deleting directory /private/var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/spark-51594e29-1ea0-4a4d-9aa0-dd65ef5146dd

Body:
your exception is thrown from this line of code:
https://github.com/h2oai/h2o-3/blob/master/h2o-core/src/main/java/water/init/HostnameGuesser.java#L227

because of this condition:

 if (!allowedIps.contains(addr)) {
          throw new HostnameGuessingException("IP address not found on this machine");
        }

addr is the driver ip:

17/11/17 10:16:43 INFO InternalH2OBackend: Starting H2O client on the
  Spark Driver (192.168.103.46): -name
  sparkling-water-username_app-20171117101603-0031 -nthreads 40
  -ga_opt_out -quiet -log_level WARN -log_dir /path/to/app/h2ologs/app-20171117101603-0031 -baseport 54321 -client
  -ip 192.168.103.46 -flatfile /var/folders/gl/vgw262w9227cwqvzk595rbvjygdzh8/T/1510913803950-0/flatfile.txt

and allowedIps is calculated with the function calcPrioritizedInetAddressList: https://github.com/h2oai/h2o-3/blob/master/h2o-core/src/main/java/water/init/HostnameGuesser.java#L161

for some reason, addr is not in allowedIps.

It's hard to know why from here, so I would recommend you to the run function calcPrioritizedInetAddressList by yourself, and try to understand the cause (it is private, but you can just copy the code)


来源: stackexchange

问:
Python comparing two lists and filtering items
I would like to do some word filtering (extracting only items in 'keyword' list that exist in 'whitelist').

Here is my code so far:

```<code>whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = []

for word in whitelist:
    for i in range(len(keyword)):
        if word in keyword[i]:
            keyword_filter.append(word)
        else: pass
</code>```

I want to remove every word except for 'Cat', 'Dog', and 'Cow' (which are in the
'whitelist') so that the result ('keyword_filter' list) will look like this:

```<code>['Cat, Cow', 'Dog', '', 'Cat']
</code>```

However, I got the result something like this:

```<code>['Cat', 'Cat', 'Dog', 'Cow']
</code>```

I would sincerely appreciate if you can give some advice.

答:
try this.. </p>

```<code>whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = []

for word in keyword:
    whitelistedWords = []
    for w in word.split(', '):
        if w in whitelist:
            whitelistedWords.append(w)
            #print whitelistedWords
    keyword_filter.append( ', '.join(whitelistedWords) )

print keyword_filter
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I would like to do some word filtering (extracting only items in 'keyword' list that exist in 'whitelist').

Here is my code so far:

whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = []

for word in whitelist:
    for i in range(len(keyword)):
        if word in keyword[i]:
            keyword_filter.append(word)
        else: pass

I want to remove every word except for 'Cat', 'Dog', and 'Cow' (which are in the
'whitelist') so that the result ('keyword_filter' list) will look like this:

['Cat, Cow', 'Dog', '', 'Cat']

However, I got the result something like this:

['Cat', 'Cat', 'Dog', 'Cow']

I would sincerely appreciate if you can give some advice.

Body:
try this..

whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = []

for word in keyword:
    whitelistedWords = []
    for w in word.split(', '):
        if w in whitelist:
            whitelistedWords.append(w)
            #print whitelistedWords
    keyword_filter.append( ', '.join(whitelistedWords) )

print keyword_filter

ContentLicense: CC BY-SA 3.0


问:
Rails - good way to convert string to array if not array rails
I receive a param and want it to be either a string like this :

```<code>"abc,efg"
</code>```

or an Array like this

```<code>["abc","efg"]
</code>```

In the first case I want to convert it into an Array, what would be the good way ?

Here is what I thought

```<code>if params[:ids] && params[:ids].is_a? Array
     ids = params[:ids]
else if params[:ids]
    ids = params[:ids].split(",")
</code>```

答:
You miss end tag and you have wrong else if and you can delete the check of params[:ids] because if :ids key do not exist is_a? return NilClass

I think you can do this

```<code>ids = if params[:ids].is_a? Array
          params[:ids]
      elsif params[:ids]
          params[:ids].split(",")
      end
</code>```

Body:
I receive a param and want it to be either a string like this :

"abc,efg"

or an Array like this

["abc","efg"]

In the first case I want to convert it into an Array, what would be the good way ?

Here is what I thought

if params[:ids] && params[:ids].is_a? Array
     ids = params[:ids]
else if params[:ids]
    ids = params[:ids].split(",")

Body:
You miss end tag and you have wrong else if and you can delete the check of params[:ids] because if :ids key do not exist is_a? return NilClass

I think you can do this

ids = if params[:ids].is_a? Array
          params[:ids]
      elsif params[:ids]
          params[:ids].split(",")
      end


问:
Validate each element in an array?
I am currently trying to validate a form server side, the way it works is all the data is put into and array, with the form field as the key and and the field data as the value, however I need to check that all the keys have a value associated with other wise I want the submittion to stop and the user having to edit there details and then re-submit, is there a quick check I can run rather that break the array apart and check that or check before it is put in the array with a switch or losts of if statements?</p>

答:
Sico87,

More of than not you don't want to test all of the fields simultaneously. For instance you may have a contact field that contains a phone-number entry option that isn't mandated, and rejecting the submission for that reason would be problematic.

In many cases, it's easier to test your fields individually so you can give each the special attention it needs. You'll want to give special attention to email addresses, and special attention to birthdays.

Treating all equal could cause very serious problems in the long-run.

ContentLicense: CC BY-SA 2.5

Title: Validate each element in an array?

LastEditorDisplayName: Rob

OwnerDisplayName: Jonathan Sampson


答:
Simple list comprehension:

```<code>whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = [', '.join(w for w in k.split(', ') if w in whitelist) for k in keyword]

print(keyword_filter)
</code>```

The output:

```<code>['Cat, Cow', 'Dog', '', 'Cat']
</code>```

Title: Python comparing two lists and filtering items

Body:
Simple list comprehension:

whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = [', '.join(w for w in k.split(', ') if w in whitelist) for k in keyword]

print(keyword_filter)

The output:

['Cat, Cow', 'Dog', '', 'Cat']


问:
Why can't I run my Regulafalsi code? Because of NoneType error
Here is my code for one of the root-finding methods.

```<code>from __future__ import division
def falseposition(f, a, b, imax=50, tolerance=1.0e-10):
  i = 0
  while i < imax:
    c = (a*f(b)-b*f(a))/(f(b) - f(a))
    if c == 0 or f(c) < tolerance:
      return c, i
    elif (f(a)*f(c))<0:
      b=c
    else:
      a=c
    i += 1
f = lambda x: x**5 - x + 1
root, steps = falseposition(f, 1, 8)
print ("The root is", root, "and the value is", f(root), "; steps taken:", steps)
</code>```

There is a TypeError. It says that 'NoneType' object is not iterable.

If you see what's the problem, please, tell.

答:
Your falseposition function only explicitely returns when it finds a value of c that meets the criterion before i gets greater than imax. If it doesn't, then it falls out of the while loop, reaches the end of the function body and then (implicitely) returns None. When that's the case, the root, steps = falseposition(f, 1, 8) statement effectively becomes root, steps = None, and since None is not iterable you get this error.

You mainly have two solutions here, either:

return None, i as default at the end of the function (with None signaling the function failed to found a matching value for c):

```<code>def falseposition(f, a, b, imax=50, tolerance=1.0e-10):
  i = 0
  while i < imax:
    c = (a*f(b)-b*f(a))/(f(b) - f(a))
    if c == 0 or f(c) < tolerance:
      return c, i
    elif (f(a)*f(c))<0:
      b=c
    else:
      a=c
    i += 1
  # nothing found
  return None, i
</code>```

or raise some exception instead (and let the caller code catch it):

```<code>def falseposition(f, a, b, imax=50, tolerance=1.0e-10):
  i = 0
  while i < imax:
    c = (a*f(b)-b*f(a))/(f(b) - f(a))
    if c == 0 or f(c) < tolerance:
      return c, i
    elif (f(a)*f(c))<0:
      b=c
    else:
      a=c
    i += 1
  # nothing found
  raise ValueError("No matching c value")
</code>```

Which of those solutions makes sense depends on the context and whatnot,  but since obviously there might be quite a few cases where no proper "solution" will be found the first one (returning (None, i)`) seems a good candidate.

You could of course leave the function as is and make sure you test the return value before trying to unpack it:

```<code>result = falseposition(f, 1, 8)
if result is None:
   print("oops, nothing found")
root, steps = result
# etc
</code>```

but that's really really ugly, and bad practice actually (one expects the return value of a function to be of consistant type).

Body:
Here is my code for one of the root-finding methods.

from __future__ import division
def falseposition(f, a, b, imax=50, tolerance=1.0e-10):
  i = 0
  while i < imax:
    c = (a*f(b)-b*f(a))/(f(b) - f(a))
    if c == 0 or f(c) < tolerance:
      return c, i
    elif (f(a)*f(c))<0:
      b=c
    else:
      a=c
    i += 1
f = lambda x: x**5 - x + 1
root, steps = falseposition(f, 1, 8)
print ("The root is", root, "and the value is", f(root), "; steps taken:", steps)

There is a TypeError. It says that 'NoneType' object is not iterable.

If you see what's the problem, please, tell.

Body:
Your falseposition function only explicitely returns when it finds a value of c that meets the criterion before i gets greater than imax. If it doesn't, then it falls out of the while loop, reaches the end of the function body and then (implicitely) returns None. When that's the case, the root, steps = falseposition(f, 1, 8) statement effectively becomes root, steps = None, and since None is not iterable you get this error.

You mainly have two solutions here, either:

return None, i as default at the end of the function (with None signaling the function failed to found a matching value for c):

def falseposition(f, a, b, imax=50, tolerance=1.0e-10):
  i = 0
  while i < imax:
    c = (a*f(b)-b*f(a))/(f(b) - f(a))
    if c == 0 or f(c) < tolerance:
      return c, i
    elif (f(a)*f(c))<0:
      b=c
    else:
      a=c
    i += 1
  # nothing found
  return None, i

or raise some exception instead (and let the caller code catch it):

def falseposition(f, a, b, imax=50, tolerance=1.0e-10):
  i = 0
  while i < imax:
    c = (a*f(b)-b*f(a))/(f(b) - f(a))
    if c == 0 or f(c) < tolerance:
      return c, i
    elif (f(a)*f(c))<0:
      b=c
    else:
      a=c
    i += 1
  # nothing found
  raise ValueError("No matching c value")

Which of those solutions makes sense depends on the context and whatnot,  but since obviously there might be quite a few cases where no proper "solution" will be found the first one (returning (None, i)`) seems a good candidate.

You could of course leave the function as is and make sure you test the return value before trying to unpack it:

result = falseposition(f, 1, 8)
if result is None:
   print("oops, nothing found")
root, steps = result
# etc

but that's really really ugly, and bad practice actually (one expects the return value of a function to be of consistant type).


问:
Android Studio 3.1 Canary 3 - Gradle project sync failed
Using Android Studio 3.1 Canary 3 for Android application development using Kotlin, whenever the studio is opened, I get the following error

Gradle project sync failed. Basic functionality(e.g editing debugging) will not work properly

It is not the issue relevant to the built-in kotlin in Android studio version3.0 and above but the Kotlin plugin's

答:
This is a common issue that occurs when you import all the settings from your old version of Android Studio (where you would have already installed Kotlin in plugins) to Android Studio 3 and 3.1

So, to rectify the issue go to

Preferences -> Plugins -> Search for Kotlin and Uninstall

since we have built-in Kotlin support in the latest Android Studios, we may not need this Kotlin plugin anymore

Hope this helps!


问:
Using a null value from an array in prepared MySQL statement
I have been trying to figure this out for a while and can't come to a proper solution. I have an array of data in which some items are null. They are used in prepared statement which looks as follows:

```<code>$query = "insert into stocktake_details
(created_at, created_by, deleted_at, deleted_by, department_id, edit_flag, full_cost, id, is_damaged, is_expired, is_synced, location_id, orientation, orientation_order,
            position_number, product_id, product_name, product_size_id, quantity, quantity_units, shelf_number, stocktake_id, store_locations_id, total_packed_items, unit_cost, unit_of_measure, updated_at, updated_by, user_id)

values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?)";
$rowdata =  array_values((array)($data));

            $statement = $conn->prepare($query);
            $product_id = $data->product_id;
            $statement->execute($rowdata);
</code>```

However, in the prepared statement the null gets converted to 'null'. I have seen some suggestions which mentioned bind parameters but I am unsure as to how am I supposed to use them with an array of items?

答:
Binding parameters is actually not -that- hard:</p>

```<code>// $statement is the prepared statement reference.
// $parameters is an array of all the parameters and as first entry the query itself in order of ? (for mysqli)
// ie. ('select 1...', '1', '1') etc.

$this->DetermineParameterTypes($parameters);
for($key = 1; $key < count($parameters); $key++)
    $parameters[$key] = &$parameters[$key];

// Bind parameters
call_user_func_array(array($statement, "bind_param"), $parameters);

    /**
     * Determine the types of the prepared statement parameters
     *
     * @param array by ref $parameters Parameters
     *
     */
    private function DetermineParameterTypes(&$parameters)
    {
        // Remove first argument this is the $query object
        array_shift($parameters);

        // Determine the parameter types
        $types = '';
        foreach($parameters as $param)
        {
            if((is_int($param) || is_bool($param)) && strpos($param, '.') === false)
                $types .= 'i';              //integer
            elseif (is_float($param) || is_double($param))
                $types .= 'd';              //double
            elseif (is_string($param))
                $types .= 's';              //string
            elseif (is_array($param))
                $types .= 'b';              //blob and unknown
            elseif (is_null($param))
                $types .= 's';              //set null to string type
            else
                $types .= '?';
        }
        array_unshift($parameters, $types);
    }
</code>```

Body:
I have been trying to figure this out for a while and can't come to a proper solution. I have an array of data in which some items are null. They are used in prepared statement which looks as follows:

$query = "insert into stocktake_details
(created_at, created_by, deleted_at, deleted_by, department_id, edit_flag, full_cost, id, is_damaged, is_expired, is_synced, location_id, orientation, orientation_order,
            position_number, product_id, product_name, product_size_id, quantity, quantity_units, shelf_number, stocktake_id, store_locations_id, total_packed_items, unit_cost, unit_of_measure, updated_at, updated_by, user_id)

values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?)";
$rowdata =  array_values((array)($data));

            $statement = $conn->prepare($query);
            $product_id = $data->product_id;
            $statement->execute($rowdata);

However, in the prepared statement the null gets converted to 'null'. I have seen some suggestions which mentioned bind parameters but I am unsure as to how am I supposed to use them with an array of items?

Body:
Binding parameters is actually not -that- hard:

// $statement is the prepared statement reference.
// $parameters is an array of all the parameters and as first entry the query itself in order of ? (for mysqli)
// ie. ('select 1...', '1', '1') etc.

$this->DetermineParameterTypes($parameters);
for($key = 1; $key < count($parameters); $key++)
    $parameters[$key] = &$parameters[$key];

// Bind parameters
call_user_func_array(array($statement, "bind_param"), $parameters);

    /**
     * Determine the types of the prepared statement parameters
     *
     * @param array by ref $parameters Parameters
     *
     */
    private function DetermineParameterTypes(&$parameters)
    {
        // Remove first argument this is the $query object
        array_shift($parameters);

        // Determine the parameter types
        $types = '';
        foreach($parameters as $param)
        {
            if((is_int($param) || is_bool($param)) && strpos($param, '.') === false)
                $types .= 'i';              //integer
            elseif (is_float($param) || is_double($param))
                $types .= 'd';              //double
            elseif (is_string($param))
                $types .= 's';              //string
            elseif (is_array($param))
                $types .= 'b';              //blob and unknown
            elseif (is_null($param))
                $types .= 's';              //set null to string type
            else
                $types .= '?';
        }
        array_unshift($parameters, $types);
    }


问:
PHP array is having a gap when printed out
This is the output of my 2 arrays:

code1:

```<code>     print_r($arr);
     echo implode(" ", $arr) ."\n";
</code>```

output:

```<code>    Array
    (
        [0] => 1
        [1] => 2

        [2] => 3
    )
    1 2
     3
</code>```

code 2:

```<code>     print_r($arr);
     echo implode(" ", $arr) ."\n";
</code>```

output

```<code>    Array
    (
        [0] => 1
        [1] => 4
        [2] => 2

        [3] => 3
    )
    1 4 2
     3
</code>```

Why do my arrays have a gap in them? This happens after I do some swapping of code in my project in order to sort the numbers (special case, can't sort($arr) ).

Edit: I did var_dump($arr); on both the array's, here is the output:
code 1:

```<code>    array(3) {
      [0]=>
      string(1) "1"
      [1]=>
      string(2) "2
    "
      [2]=>
      string(1) "3"
    }
</code>```

code 2:

```<code>    array(4) {
      [0]=>
      string(1) "1"
      [1]=>
      string(2) "2
    "
      [2]=>
      string(1) "3"
      [3]=>
      string(1) "4"
    }
</code>```

FUll code:

Content inside stdin file

```<code>    3
    3
    3 1 2
    4
    1 3 4 2
    5
    1 2 3 5 4
</code>```

code:

```<code>    <?php
    $_fp = fopen("php://stdin", "r");
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    fscanf ($_fp, "%d", $testcases);

    for ($i = 0; $i < $testcases; $i++)
    {
        fscanf($_fp, "%d", $n);
        $arr = explode(" ", fgets($_fp));

        for ($j = 0; $j < ($n-1); $j++)
        {
            print_r($arr);
            if ($arr[$j] > $arr[$j+1])
            {
                if ( isset($arr[$j-1]) )
                {

                    $loop = 0;
                    while($loop < 3)
                    {
                        $a = $arr[$j-1];
                        $b = $arr[$j];
                        $c = $arr[$j+1];

                        $arr[$j-1] = $b;
                        $arr[$j] = $c;
                        $arr[$j+1] = $a;

                        if ($arr[$j-1] <= $arr[$j] && $arr[$j] <= $arr[$j+1])
                        {
                            $possible = 1;
                            break;
                        }
                        $loop++;
                    }

                }
                elseif ( isset($arr[$j+2]) )
                {
                    $loop = 0;
                    while($loop < 3)
                    {
                        $a = $arr[$j];
                        $b = $arr[$j+1];
                        $c = $arr[$j+2];

                        $arr[$j] = $b;
                        $arr[$j+1] = $c;
                        $arr[$j+2] = $a;

                        if ($arr[$j] <= $arr[$j+1] && $arr[$j+1] <= $arr[$j+2])
                        {
                            $possible = 1;
                            break;
                        }
                        $loop++;
                    }
                }

            }
        }

        $possible = 0;

        print_r($arr);

        }

         ?>
</code>```

答:
Your array 2nd value has newline char

See Demo

```<code><?php

  $arr = array(1,'2'.PHP_EOL,3);
  print_r($arr);
  echo implode(" ", $arr);
?>
</code>```

Would produce:

```<code>Array
(
    [0] => 1
    [1] => 2

    [2] => 3
)
1 2
 3
</code>```

--edit--

Solution :

$arr = array_map('trim', $arr); after $arr = explode(" ", fgets($_fp));  because when reading a file using fgets() it will include the newline at the end. using trim() you can strip whitespace (or other characters) from the beginning and end of a string, you may use rtrim() which strips whitespace (or other characters) from the end of a string

Body:
This is the output of my 2 arrays:

code1:

     print_r($arr);
     echo implode(" ", $arr) ."\n";

output:

    Array
    (
        [0] => 1
        [1] => 2

        [2] => 3
    )
    1 2
     3

code 2:

     print_r($arr);
     echo implode(" ", $arr) ."\n";

output

    Array
    (
        [0] => 1
        [1] => 4
        [2] => 2

        [3] => 3
    )
    1 4 2
     3

Why do my arrays have a gap in them? This happens after I do some swapping of code in my project in order to sort the numbers (special case, can't sort($arr) ).

Edit: I did var_dump($arr); on both the array's, here is the output:
code 1:

    array(3) {
      [0]=>
      string(1) "1"
      [1]=>
      string(2) "2
    "
      [2]=>
      string(1) "3"
    }

code 2:

    array(4) {
      [0]=>
      string(1) "1"
      [1]=>
      string(2) "2
    "
      [2]=>
      string(1) "3"
      [3]=>
      string(1) "4"
    }

FUll code:

Content inside stdin file

    3
    3
    3 1 2
    4
    1 3 4 2
    5
    1 2 3 5 4

code:

    <?php
    $_fp = fopen("php://stdin", "r");
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    fscanf ($_fp, "%d", $testcases);

    for ($i = 0; $i < $testcases; $i++)
    {
        fscanf($_fp, "%d", $n);
        $arr = explode(" ", fgets($_fp));

        for ($j = 0; $j < ($n-1); $j++)
        {
            print_r($arr);
            if ($arr[$j] > $arr[$j+1])
            {
                if ( isset($arr[$j-1]) )
                {

                    $loop = 0;
                    while($loop < 3)
                    {
                        $a = $arr[$j-1];
                        $b = $arr[$j];
                        $c = $arr[$j+1];

                        $arr[$j-1] = $b;
                        $arr[$j] = $c;
                        $arr[$j+1] = $a;

                        if ($arr[$j-1] <= $arr[$j] && $arr[$j] <= $arr[$j+1])
                        {
                            $possible = 1;
                            break;
                        }
                        $loop++;
                    }

                }
                elseif ( isset($arr[$j+2]) )
                {
                    $loop = 0;
                    while($loop < 3)
                    {
                        $a = $arr[$j];
                        $b = $arr[$j+1];
                        $c = $arr[$j+2];

                        $arr[$j] = $b;
                        $arr[$j+1] = $c;
                        $arr[$j+2] = $a;

                        if ($arr[$j] <= $arr[$j+1] && $arr[$j+1] <= $arr[$j+2])
                        {
                            $possible = 1;
                            break;
                        }
                        $loop++;
                    }
                }

            }
        }

        $possible = 0;

        print_r($arr);

        }

         ?>

Body:
Your array 2nd value has newline char

See Demo

<?php

  $arr = array(1,'2'.PHP_EOL,3);
  print_r($arr);
  echo implode(" ", $arr);
?>

Would produce:

Array
(
    [0] => 1
    [1] => 2

    [2] => 3
)
1 2
 3

--edit--

Solution :

$arr = array_map('trim', $arr); after $arr = explode(" ", fgets($_fp));  because when reading a file using fgets() it will include the newline at the end. using trim() you can strip whitespace (or other characters) from the beginning and end of a string, you may use rtrim() which strips whitespace (or other characters) from the end of a string


答:
You could use regex:</p>

```<code>import re

whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = []

for words in keyword:
    match = re.findall('(' + r'|'.join(whitelist) + ')[,\s]*', words)
    keyword_filter.append(', '.join(match))
print(keyword_filter)
</code>```

Body:
You could use regex:

import re

whitelist = ['Cat', 'Dog', 'Cow']
keyword = ['Cat, Cow, Horse', 'Bird, Whale, Dog', 'Pig, Chicken', 'Tiger, Cat']
keyword_filter = []

for words in keyword:
    match = re.findall('(' + r'|'.join(whitelist) + ')[,\s]*', words)
    keyword_filter.append(', '.join(match))
print(keyword_filter)


问:
RegEx Parse Tool to extract digits from string
Using Alteryx, I have a field called Address which consists of fields like A32C, GH2X, ABC19E. So basically where digits are pinned between sets of letters. I am trying to use the RegEx tool to extract the digits out into a new column called ADDRESS1.

I have Address set to Field to Parse. Output method Parse.
My regular expression is typed in as:

```<code>(?:[[alpha]]+)(/d+)(?:[[alpha]]+)
</code>```

And then I have (/d+) outputting to ADDRESS1. However, when I run this it parses 0 records. What am I doing wrong?

答:
To match a digit, use [0-9] or \d. To match a letter, use [[:alpha:]].

Use

```<code>[[:alpha:]]+(\d+)[[:alpha:]]+
</code>```

See the regex demo.

Body:
Using Alteryx, I have a field called Address which consists of fields like A32C, GH2X, ABC19E. So basically where digits are pinned between sets of letters. I am trying to use the RegEx tool to extract the digits out into a new column called ADDRESS1.

I have Address set to Field to Parse. Output method Parse.
My regular expression is typed in as:

(?:[[alpha]]+)(/d+)(?:[[alpha]]+)

And then I have (/d+) outputting to ADDRESS1. However, when I run this it parses 0 records. What am I doing wrong?

Body:
To match a digit, use [0-9] or \d. To match a letter, use [[:alpha:]].

Use

[[:alpha:]]+(\d+)[[:alpha:]]+

See the regex demo.


问:
Navigate from Fragment to Activity MVVMCross
I am struggling to perform a navigation from a Fragment to an Activity at ViewModel level. I have an Activity with a DrawerLayout and this Activity has a FrameLayout to display different Fragments selected from the DrawerLayout. That navigation is perform by the ViewModel of this Activity and it is calling the ViewModel of each Fragment to display. In one of the Fragments I added a button binding a IMvxCommand method to perform the navigation from the Fragment to a new Activity and here is where I have the problem because when I click on the button nothing happens.

Find below my code.

ViewModel of Fragment

```<code>public class MainFrameViewModel : ContentViewModel
    {
        readonly IMvxNavigationService navigationService;

        public MainFrameViewModel(IMvxNavigationService navigationService) : base(navigationService)
        {
            this.navigationService = navigationService;
        }

        public IMvxCommand GoMoreInfo
        {
            get
            {
                IMvxCommand navigateCommand = new MvxCommand(() => navigationService.Navigate<MoreInfoViewModel>());
                return navigateCommand;
            }
        }
    }
</code>```

Fragment code

```<code>[MvxFragmentPresentation(typeof(ContentViewModel), Resource.Id.frameLayout)]
    [Register("mvvmdemo.droid.views.fragments.MainFrameFragment")]
    public class MainFrameFragment : MvxFragment<MainFrameViewModel>
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.MainFrame, container, false);
        }
    }
</code>```

Activity to navigate

```<code>[MvxActivityPresentation]
    [Activity(Label = "MoreInfoActivity")]
    public class MoreInfoActivity : MvxAppCompatActivity<MoreInfoViewModel>
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.MoreInfoLayout);
        }
    }
</code>```

ContentViewModel is the ViewModel of the Activity containing the FrameLayout and the DrawerLayout.

答:
Your bindings aren't working because you are using the default inflater, which knows nothing about MvvmCross bindings. You could solve this problem by using the MvvmCross inflater inside OnCreateView. Change return inflater.Inflate(Resource.Layout.MainFrame, container, false); call to return this.BindingInflate(Resource.Layout.MainFrame, null);

Also, you are ignoring the async part of the IMvxNavigationService. It would be an improvement to change from IMvxCommand to IMvxAsyncCommand and await or return the Task returned by IMvxNavigationService.Navigate()

Body:
I am struggling to perform a navigation from a Fragment to an Activity at ViewModel level. I have an Activity with a DrawerLayout and this Activity has a FrameLayout to display different Fragments selected from the DrawerLayout. That navigation is perform by the ViewModel of this Activity and it is calling the ViewModel of each Fragment to display. In one of the Fragments I added a button binding a IMvxCommand method to perform the navigation from the Fragment to a new Activity and here is where I have the problem because when I click on the button nothing happens.

Find below my code.

ViewModel of Fragment

public class MainFrameViewModel : ContentViewModel
    {
        readonly IMvxNavigationService navigationService;

        public MainFrameViewModel(IMvxNavigationService navigationService) : base(navigationService)
        {
            this.navigationService = navigationService;
        }

        public IMvxCommand GoMoreInfo
        {
            get
            {
                IMvxCommand navigateCommand = new MvxCommand(() => navigationService.Navigate<MoreInfoViewModel>());
                return navigateCommand;
            }
        }
    }

Fragment code

[MvxFragmentPresentation(typeof(ContentViewModel), Resource.Id.frameLayout)]
    [Register("mvvmdemo.droid.views.fragments.MainFrameFragment")]
    public class MainFrameFragment : MvxFragment<MainFrameViewModel>
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.MainFrame, container, false);
        }
    }

Activity to navigate

[MvxActivityPresentation]
    [Activity(Label = "MoreInfoActivity")]
    public class MoreInfoActivity : MvxAppCompatActivity<MoreInfoViewModel>
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.MoreInfoLayout);
        }
    }

ContentViewModel is the ViewModel of the Activity containing the FrameLayout and the DrawerLayout.


问:
PHP: Foreach Simlplexml output
I got a xmlfile exported from the datamodelling-tool wwwsqldesigner. It contains all tables, their comments, columns and their datatypes, primary key columns and so on.

I imported the xml file in my php script successfully with simplexml_load_file and now I want to loop through the nodes/tags to get 3 lists (to insert them into a database in 3 tables afterwards, for the moment echoing is enough).

Those lists are:
1) Tables: Tablenames in the datamodel/xml-file, their comments and primary key names.
2) Columns: The columns of that tables: columnname, datatype, nullable (and column order if possible).
3) Primary Key Columns: columnname and column order.

The xml looks like this:

```<code><?xml version="1.0" encoding="utf-8" ?>
<sql>
<table x="673" y="237" name="DIM_CUSTOMER">
<row name="CUSTOMER_SID" null="0" autoincrement="1">
<datatype>INTEGER</datatype>
<default>NULL</default><comment>Customer-SK</comment>
</row>
<row name="CUSTOMER_NAME" null="0" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>'NULL'</default><comment>Customername</comment>
</row>
<row name="CUSTOMER_GROUP" null="1" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>NULL</default></row>
<key type="PRIMARY" name="DIM_CUSTOMER_PK">
<part>CUSTOMER_SID</part>
</key>
<comment>Kunden</comment>
</table>
<table x="1150" y="204" name="DIM_PRODUCT">
<row name="PRODUCT_SID" null="0" autoincrement="1">
<datatype>INTEGER</datatype>
<default>NULL</default><comment>Produkt-Sk</comment>
</row>
<row name="PRODUCT_NAME" null="0" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>'NULL'</default><comment>Productname</comment>
</row>
<key type="PRIMARY" name="DIM_PRODUCT_PK">
<part>PRODUCT_SID</part>
</key>
<comment>Product</comment>
</table>
<table x="632" y="499" name="DIM_DATE">
<row name="DATE_ID" null="0" autoincrement="1">
<datatype>DATE</datatype>
<default>'NULL'</default></row>
<row name="YEAR" null="1" autoincrement="0">
<datatype>CHAR(4)</datatype>
<default>NULL</default></row>
<row name="QUARTER" null="1" autoincrement="0">
<datatype>CHAR(2)</datatype>
<default>NULL</default></row>
<row name="MONTH" null="1" autoincrement="0">
<datatype>VARCHAR(2)</datatype>
<default>NULL</default></row>
<key type="PRIMARY" name="DIM_DATE_PK">
<part>DATE_ID</part>
</key>
</table>
<table x="1214" y="511" name="DIM_INVENTORY">
<row name="INVENTORY_SID" null="1" autoincrement="1">
<datatype>INTEGER</datatype>
<default>NULL</default><comment>Inventory SK</comment>
</row>
<row name="INVENTORY_NAME" null="0" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>'NULL'</default><comment>Inventory</comment>
</row>
<key type="PRIMARY" name="DIM_DATE_PK">
<part>INVENTORY_SID</part>
</key>
</table>
<table x="950" y="358" name="FACT_SALES">
<row name="DATE_ID" null="0" autoincrement="0">
<datatype>DATE</datatype>
<default>'NULL'</default><relation table="DIM_DATE" row="DATE_ID" />
</row>
<row name="INVENTORY_SID" null="1" autoincrement="0">
<datatype>INTEGER</datatype>
<default>NULL</default><relation table="DIM_INVENTORY" row="INVENTORY_SID" />
<comment>Inventory SK</comment>
</row>
<row name="PRODUCT_SID" null="0" autoincrement="0">
<datatype>INTEGER</datatype>
<default>NULL</default><relation table="DIM_PRODUCT" row="PRODUCT_SID" />
<comment>Product-Sk</comment>
</row>
<row name="CUSTOMER_SID" null="0" autoincrement="0">
<datatype>INTEGER</datatype>
<default>NULL</default><relation table="DIM_CUSTOMER" row="CUSTOMER_SID" />
<comment>Customer-SK</comment>
</row>
<row name="REVENUE" null="0" autoincrement="0">
<datatype>DECIMAL(10,2)</datatype>
<default>NULL</default></row>
<key type="PRIMARY" name="FACT_SALES_PK">
<part>DATE_ID</part>
<part>INVENTORY_SID</part>
<part>PRODUCT_SID</part>
<part>CUSTOMER_SID</part>
</key>
<comment>Sales</comment>
</table>
</sql>
</code>```

For each database table there is a  tag that contains several  tags and a  tag for the primary key and a  tag. The  tag itself has several  tags for every primary key columns.

To create the 3 needed lists mentioned above I wrote follwing php script:

```<code><?php
$xml = simplexml_load_file('C:\xampp\htdocs\sqldesigner\backend\php-mysql\Datamart.xml');

// Tables
echo "+++++++++++++ TABLES +++++++++++++<br>";
foreach ($xml->table as $table) {
        echo "----------------------------------------<br>";
        echo "Tabelle: "  . $table['name']  . "<br>"; //Tabelle
        echo "Primary: "  . $table->key['name']     . "<br>"; //PK
        echo "Comment: "  . $table->comment     . "<br>"; //Table-Comment

}
// Columns
echo "+++++++++++++ COLUMNS +++++++++++++<br>";
foreach ($xml->table as $table) {

        foreach ($table as $row) {
            echo "Column: "   . $table['name'] . "." . $row['name'] . "<br>";
            echo "Nullable: " . $row["null"]    . "<br>";
            echo "Datatype: " . $row->datatype."<br>";
            echo "Comment: "  . $row->comment . "<br>";
            echo "---------------------<br>";
        }
}

// PK-Columns
echo "+++++++++++++ PRIMARY KEY COLS +++++++++++++<br>";
foreach ($xml->table as $table) {

        foreach ($table as $primarykey) {
            echo "PK-Col.: " . $table['name'] . "." . $primarykey->part . "<br>";

        }
}
?>
</code>```

Unfortunately the output is wrong for columns and primary keys. It produces empty lines. So my guess is, there is a connection from the inner foreach to the outer foreach missing. The output looks like this:

```<code>+++++++++++++ TABLES +++++++++++++
----------------------------------------
Tabelle: DIM_CUSTOMER
Primary: DIM_CUSTOMER_PK
Comment: Kunden
----------------------------------------
Tabelle: DIM_PRODUCT
Primary: DIM_PRODUCT_PK
Comment: Product
----------------------------------------
Tabelle: DIM_DATE
Primary: DIM_DATE_PK
Comment:
----------------------------------------
Tabelle: DIM_INVENTORY
Primary: DIM_DATE_PK
Comment:
----------------------------------------
Tabelle: FACT_SALES
Primary: FACT_SALES_PK
Comment: Sales
+++++++++++++ COLUMNS +++++++++++++
Column: DIM_CUSTOMER.CUSTOMER_SID
Nullable: 0
Datatype: INTEGER
Comment: Customer-SK
---------------------
Column: DIM_CUSTOMER.CUSTOMER_NAME
Nullable: 0
Datatype: VARCHAR(30)
Comment: Customername
---------------------
Column: DIM_CUSTOMER.CUSTOMER_GROUP
Nullable: 1
Datatype: VARCHAR(30)
Comment:
---------------------
Column: DIM_CUSTOMER.DIM_CUSTOMER_PK
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_CUSTOMER.
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_PRODUCT.PRODUCT_SID
Nullable: 0
Datatype: INTEGER
Comment: Produkt-Sk
---------------------
Column: DIM_PRODUCT.PRODUCT_NAME
Nullable: 0
Datatype: VARCHAR(30)
Comment: Productname
---------------------
Column: DIM_PRODUCT.DIM_PRODUCT_PK
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_PRODUCT.
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_DATE.DATE_ID
Nullable: 0
Datatype: DATE
Comment:
---------------------
Column: DIM_DATE.YEAR
Nullable: 1
Datatype: CHAR(4)
Comment:
---------------------
Column: DIM_DATE.QUARTER
Nullable: 1
Datatype: CHAR(2)
Comment:
---------------------
Column: DIM_DATE.MONTH
Nullable: 1
Datatype: VARCHAR(2)
Comment:
---------------------
Column: DIM_DATE.DIM_DATE_PK
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_INVENTORY.INVENTORY_SID
Nullable: 1
Datatype: INTEGER
Comment: Inventory SK
---------------------
Column: DIM_INVENTORY.INVENTORY_NAME
Nullable: 0
Datatype: VARCHAR(30)
Comment: Inventory
---------------------
Column: DIM_INVENTORY.DIM_DATE_PK
Nullable:
Datatype:
Comment:
---------------------
Column: FACT_SALES.DATE_ID
Nullable: 0
Datatype: DATE
Comment:
---------------------
Column: FACT_SALES.INVENTORY_SID
Nullable: 1
Datatype: INTEGER
Comment: Inventory SK
---------------------
Column: FACT_SALES.PRODUCT_SID
Nullable: 0
Datatype: INTEGER
Comment: Product-Sk
---------------------
Column: FACT_SALES.CUSTOMER_SID
Nullable: 0
Datatype: INTEGER
Comment: Customer-SK
---------------------
Column: FACT_SALES.REVENUE
Nullable: 0
Datatype: DECIMAL(10,2)
Comment:
---------------------
Column: FACT_SALES.FACT_SALES_PK
Nullable:
Datatype:
Comment:
---------------------
Column: FACT_SALES.
Nullable:
Datatype:
Comment:
---------------------
+++++++++++++ PRIMARY KEY COLS +++++++++++++
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_CUSTOMER.CUSTOMER_SID
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_PRODUCT.
PK-Col.: DIM_PRODUCT.
PK-Col.: DIM_PRODUCT.PRODUCT_SID
PK-Col.: DIM_PRODUCT.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.DATE_ID
PK-Col.: DIM_INVENTORY.
PK-Col.: DIM_INVENTORY.
PK-Col.: DIM_INVENTORY.INVENTORY_SID
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.DATE_ID
PK-Col.: FACT_SALES.
</code>```

Here is how the datamodel from the xml looks like in the wwwsqldesigner

Any suggestions how to handle this problem?

Thank you and cheers!

答:
Thats happening because you're iterating through all table nodes without filtering, try the following solution:</p>

```<code><?php
$xml = simplexml_load_file('C:\xampp\htdocs\sqldesigner\backend\php-mysql\Datamart.xml');

// Tables
echo "+++++++++++++ TABLES +++++++++++++<br>";
foreach ($xml->table as $table) {
        echo "----------------------------------------<br>";
        echo "Tabelle: "  . $table['name']  . "<br>"; //Tabelle
        echo "Primary: "  . $table->key['name']     . "<br>"; //PK
        echo "Comment: "  . $table->comment     . "<br>"; //Table-Comment

}
// Columns
echo "+++++++++++++ COLUMNS +++++++++++++<br>";
foreach ($xml->table as $table) {

        foreach ($table as $row) {
            if ($row->getName() != 'row') {
                continue;
            }

            echo "Column: "   . $table['name'] . "." . $row['name'] . "<br>";
            echo "Nullable: " . $row["null"]    . "<br>";
            echo "Datatype: " . $row->datatype."<br>";
            echo "Comment: "  . $row->comment . "<br>";
            echo "---------------------<br>";
        }
}

// PK-Columns
echo "+++++++++++++ PRIMARY KEY COLS +++++++++++++<br>";
foreach ($xml->table as $table) {

        foreach ($table as $primarykey) {
            if ($primarykey->getName() != 'key') {
                continue;
            }

            echo "PK-Col.: " . $table['name'] . "." . $primarykey->part . "<br>";

        }
}
?>
</code>```

Title: PHP: Foreach Simlplexml output

Body:
I got a xmlfile exported from the datamodelling-tool wwwsqldesigner. It contains all tables, their comments, columns and their datatypes, primary key columns and so on.

I imported the xml file in my php script successfully with simplexml_load_file and now I want to loop through the nodes/tags to get 3 lists (to insert them into a database in 3 tables afterwards, for the moment echoing is enough).

Those lists are:
1) Tables: Tablenames in the datamodel/xml-file, their comments and primary key names.
2) Columns: The columns of that tables: columnname, datatype, nullable (and column order if possible).
3) Primary Key Columns: columnname and column order.

The xml looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<sql>

<row name="CUSTOMER_SID" null="0" autoincrement="1">
<datatype>INTEGER</datatype>
<default>NULL</default><comment>Customer-SK</comment>
</row>
<row name="CUSTOMER_NAME" null="0" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>'NULL'</default><comment>Customername</comment>
</row>
<row name="CUSTOMER_GROUP" null="1" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>NULL</default></row>
<key type="PRIMARY" name="DIM_CUSTOMER_PK">
<part>CUSTOMER_SID</part>
</key>
<comment>Kunden</comment>

<row name="PRODUCT_SID" null="0" autoincrement="1">
<datatype>INTEGER</datatype>
<default>NULL</default><comment>Produkt-Sk</comment>
</row>
<row name="PRODUCT_NAME" null="0" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>'NULL'</default><comment>Productname</comment>
</row>
<key type="PRIMARY" name="DIM_PRODUCT_PK">
<part>PRODUCT_SID</part>
</key>
<comment>Product</comment>

<row name="DATE_ID" null="0" autoincrement="1">
<datatype>DATE</datatype>
<default>'NULL'</default></row>
<row name="YEAR" null="1" autoincrement="0">
<datatype>CHAR(4)</datatype>
<default>NULL</default></row>
<row name="QUARTER" null="1" autoincrement="0">
<datatype>CHAR(2)</datatype>
<default>NULL</default></row>
<row name="MONTH" null="1" autoincrement="0">
<datatype>VARCHAR(2)</datatype>
<default>NULL</default></row>
<key type="PRIMARY" name="DIM_DATE_PK">
<part>DATE_ID</part>
</key>

<row name="INVENTORY_SID" null="1" autoincrement="1">
<datatype>INTEGER</datatype>
<default>NULL</default><comment>Inventory SK</comment>
</row>
<row name="INVENTORY_NAME" null="0" autoincrement="0">
<datatype>VARCHAR(30)</datatype>
<default>'NULL'</default><comment>Inventory</comment>
</row>
<key type="PRIMARY" name="DIM_DATE_PK">
<part>INVENTORY_SID</part>
</key>

<row name="DATE_ID" null="0" autoincrement="0">
<datatype>DATE</datatype>
<default>'NULL'</default><relation table="DIM_DATE" row="DATE_ID" />
</row>
<row name="INVENTORY_SID" null="1" autoincrement="0">
<datatype>INTEGER</datatype>
<default>NULL</default><relation table="DIM_INVENTORY" row="INVENTORY_SID" />
<comment>Inventory SK</comment>
</row>
<row name="PRODUCT_SID" null="0" autoincrement="0">
<datatype>INTEGER</datatype>
<default>NULL</default><relation table="DIM_PRODUCT" row="PRODUCT_SID" />
<comment>Product-Sk</comment>
</row>
<row name="CUSTOMER_SID" null="0" autoincrement="0">
<datatype>INTEGER</datatype>
<default>NULL</default><relation table="DIM_CUSTOMER" row="CUSTOMER_SID" />
<comment>Customer-SK</comment>
</row>
<row name="REVENUE" null="0" autoincrement="0">
<datatype>DECIMAL(10,2)</datatype>
<default>NULL</default></row>
<key type="PRIMARY" name="FACT_SALES_PK">
<part>DATE_ID</part>
<part>INVENTORY_SID</part>
<part>PRODUCT_SID</part>
<part>CUSTOMER_SID</part>
</key>
<comment>Sales</comment>

</sql>

For each database table there is a  tag that contains several  tags and a  tag for the primary key and a  tag. The  tag itself has several  tags for every primary key columns.

To create the 3 needed lists mentioned above I wrote follwing php script:

<?php
$xml = simplexml_load_file('C:\xampp\htdocs\sqldesigner\backend\php-mysql\Datamart.xml');

// Tables
echo "+++++++++++++ TABLES +++++++++++++
";
foreach ($xml->table as $table) {
        echo "----------------------------------------
";
        echo "Tabelle: "  . $table['name']  . "
"; //Tabelle
        echo "Primary: "  . $table->key['name']     . "
"; //PK
        echo "Comment: "  . $table->comment     . "
"; //Table-Comment

}
// Columns
echo "+++++++++++++ COLUMNS +++++++++++++
";
foreach ($xml->table as $table) {

        foreach ($table as $row) {
            echo "Column: "   . $table['name'] . "." . $row['name'] . "
";
            echo "Nullable: " . $row["null"]    . "
";
            echo "Datatype: " . $row->datatype."
";
            echo "Comment: "  . $row->comment . "
";
            echo "---------------------
";
        }
}

// PK-Columns
echo "+++++++++++++ PRIMARY KEY COLS +++++++++++++
";
foreach ($xml->table as $table) {

        foreach ($table as $primarykey) {
            echo "PK-Col.: " . $table['name'] . "." . $primarykey->part . "
";

        }
}
?>

Unfortunately the output is wrong for columns and primary keys. It produces empty lines. So my guess is, there is a connection from the inner foreach to the outer foreach missing. The output looks like this:

+++++++++++++ TABLES +++++++++++++
----------------------------------------
Tabelle: DIM_CUSTOMER
Primary: DIM_CUSTOMER_PK
Comment: Kunden
----------------------------------------
Tabelle: DIM_PRODUCT
Primary: DIM_PRODUCT_PK
Comment: Product
----------------------------------------
Tabelle: DIM_DATE
Primary: DIM_DATE_PK
Comment:
----------------------------------------
Tabelle: DIM_INVENTORY
Primary: DIM_DATE_PK
Comment:
----------------------------------------
Tabelle: FACT_SALES
Primary: FACT_SALES_PK
Comment: Sales
+++++++++++++ COLUMNS +++++++++++++
Column: DIM_CUSTOMER.CUSTOMER_SID
Nullable: 0
Datatype: INTEGER
Comment: Customer-SK
---------------------
Column: DIM_CUSTOMER.CUSTOMER_NAME
Nullable: 0
Datatype: VARCHAR(30)
Comment: Customername
---------------------
Column: DIM_CUSTOMER.CUSTOMER_GROUP
Nullable: 1
Datatype: VARCHAR(30)
Comment:
---------------------
Column: DIM_CUSTOMER.DIM_CUSTOMER_PK
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_CUSTOMER.
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_PRODUCT.PRODUCT_SID
Nullable: 0
Datatype: INTEGER
Comment: Produkt-Sk
---------------------
Column: DIM_PRODUCT.PRODUCT_NAME
Nullable: 0
Datatype: VARCHAR(30)
Comment: Productname
---------------------
Column: DIM_PRODUCT.DIM_PRODUCT_PK
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_PRODUCT.
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_DATE.DATE_ID
Nullable: 0
Datatype: DATE
Comment:
---------------------
Column: DIM_DATE.YEAR
Nullable: 1
Datatype: CHAR(4)
Comment:
---------------------
Column: DIM_DATE.QUARTER
Nullable: 1
Datatype: CHAR(2)
Comment:
---------------------
Column: DIM_DATE.MONTH
Nullable: 1
Datatype: VARCHAR(2)
Comment:
---------------------
Column: DIM_DATE.DIM_DATE_PK
Nullable:
Datatype:
Comment:
---------------------
Column: DIM_INVENTORY.INVENTORY_SID
Nullable: 1
Datatype: INTEGER
Comment: Inventory SK
---------------------
Column: DIM_INVENTORY.INVENTORY_NAME
Nullable: 0
Datatype: VARCHAR(30)
Comment: Inventory
---------------------
Column: DIM_INVENTORY.DIM_DATE_PK
Nullable:
Datatype:
Comment:
---------------------
Column: FACT_SALES.DATE_ID
Nullable: 0
Datatype: DATE
Comment:
---------------------
Column: FACT_SALES.INVENTORY_SID
Nullable: 1
Datatype: INTEGER
Comment: Inventory SK
---------------------
Column: FACT_SALES.PRODUCT_SID
Nullable: 0
Datatype: INTEGER
Comment: Product-Sk
---------------------
Column: FACT_SALES.CUSTOMER_SID
Nullable: 0
Datatype: INTEGER
Comment: Customer-SK
---------------------
Column: FACT_SALES.REVENUE
Nullable: 0
Datatype: DECIMAL(10,2)
Comment:
---------------------
Column: FACT_SALES.FACT_SALES_PK
Nullable:
Datatype:
Comment:
---------------------
Column: FACT_SALES.
Nullable:
Datatype:
Comment:
---------------------
+++++++++++++ PRIMARY KEY COLS +++++++++++++
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_CUSTOMER.CUSTOMER_SID
PK-Col.: DIM_CUSTOMER.
PK-Col.: DIM_PRODUCT.
PK-Col.: DIM_PRODUCT.
PK-Col.: DIM_PRODUCT.PRODUCT_SID
PK-Col.: DIM_PRODUCT.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.
PK-Col.: DIM_DATE.DATE_ID
PK-Col.: DIM_INVENTORY.
PK-Col.: DIM_INVENTORY.
PK-Col.: DIM_INVENTORY.INVENTORY_SID
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.
PK-Col.: FACT_SALES.DATE_ID
PK-Col.: FACT_SALES.

Here is how the datamodel from the xml looks like in the wwwsqldesigner

Any suggestions how to handle this problem?

Thank you and cheers!

Body:
Thats happening because you're iterating through all table nodes without filtering, try the following solution:

<?php
$xml = simplexml_load_file('C:\xampp\htdocs\sqldesigner\backend\php-mysql\Datamart.xml');

// Tables
echo "+++++++++++++ TABLES +++++++++++++<br>";
foreach ($xml->table as $table) {
        echo "----------------------------------------<br>";
        echo "Tabelle: "  . $table['name']  . "<br>"; //Tabelle
        echo "Primary: "  . $table->key['name']     . "<br>"; //PK
        echo "Comment: "  . $table->comment     . "<br>"; //Table-Comment

}
// Columns
echo "+++++++++++++ COLUMNS +++++++++++++<br>";
foreach ($xml->table as $table) {

        foreach ($table as $row) {
            if ($row->getName() != 'row') {
                continue;
            }

            echo "Column: "   . $table['name'] . "." . $row['name'] . "<br>";
            echo "Nullable: " . $row["null"]    . "<br>";
            echo "Datatype: " . $row->datatype."<br>";
            echo "Comment: "  . $row->comment . "<br>";
            echo "---------------------<br>";
        }
}

// PK-Columns
echo "+++++++++++++ PRIMARY KEY COLS +++++++++++++<br>";
foreach ($xml->table as $table) {

        foreach ($table as $primarykey) {
            if ($primarykey->getName() != 'key') {
                continue;
            }

            echo "PK-Col.: " . $table['name'] . "." . $primarykey->part . "<br>";

        }
}
?>