问:
java.lang.NoClassDefFoundError: com/amazonaws/services/s3/AmazonS3Client in Spark
I have a java spark process which I call using -
spark-submit --class MyClass target/MyJar.jar

The last part of this process is to write it locally and then copy it to s3 as it need to have specific name (can also be written to s3 and mv'ed there, for the sake of the question the error stays the same).

The code compiles and runs but when it get to the code snippet below I get the following error -

java.lang.NoClassDefFoundError: com/amazonaws/services/s3/AmazonS3    at
  java.lang.Class.forName0(Native Method)   at
  java.lang.Class.forName(Class.java:348)   at
  org.apache.spark.util.Utils$.classForName(Utils.scala:229)    at
  org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:700)
    at
  org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:187)
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:212)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:126)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) Caused
  by: java.lang.ClassNotFoundException:
  com.amazonaws.services.s3.AmazonS3    at
  java.net.URLClassLoader.findClass(URLClassLoader.java:381)    at
  java.lang.ClassLoader.loadClass(ClassLoader.java:424)     at
  java.lang.ClassLoader.loadClass(ClassLoader.java:357)

public static void saveToS3(Dataset<Row> df, String outputBucket, String outputPath) throws IOException {
    String tmpFile = "temp" + Long.toString(System.nanoTime());
    df.coalesce(1).write().option("header", true).csv(tmpFile);
    File directory = new File(tmpFile);
    AmazonS3 s3client = new AmazonS3Client();
    for (File file : directory.listFiles()) {
        if (file.getName().startsWith("part_") || file.getName().endsWith("csv")) {
            s3client.putObject(new PutObjectRequest(outputBucket, getS3path(outputPath), file));
            }
        file.delete();
    }
    directory.delete();
}
```

<p>I use the following dependencies which might be relevant </p>

```        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-aws</artifactId>
            <version>2.7.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>servlet-api-2.5</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
```

<p>Any idea?</p>

<p>Dependency tree - </p>

```[INFO] +- org.apache.spark:spark-core_2.11:jar:2.1.0:compile
[INFO] |  +- org.apache.avro:avro-mapred:jar:hadoop2:1.7.7:compile
[INFO] |  |  +- org.apache.avro:avro-ipc:jar:1.7.7:compile
[INFO] |  |  +- org.apache.avro:avro-ipc:jar:tests:1.7.7:compile
[INFO] |  |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] |  |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] |  +- com.twitter:chill_2.11:jar:0.8.0:compile
[INFO] |  |  \- com.esotericsoftware:kryo-shaded:jar:3.0.3:compile
[INFO] |  |     +- com.esotericsoftware:minlog:jar:1.3.0:compile
[INFO] |  |     \- org.objenesis:objenesis:jar:2.1:compile
[INFO] |  +- com.twitter:chill-java:jar:0.8.0:compile
[INFO] |  +- org.apache.xbean:xbean-asm5-shaded:jar:4.4:compile
[INFO] |  +- org.apache.hadoop:hadoop-client:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-hdfs:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-app:jar:2.2.0:compile
[INFO] |  |  |  +- org.apache.hadoop:hadoop-mapreduce-client-common:jar:2.2.0:compile
[INFO] |  |  |  |  +- org.apache.hadoop:hadoop-yarn-client:jar:2.2.0:compile
[INFO] |  |  |  |  |  \- com.google.inject:guice:jar:3.0:compile
[INFO] |  |  |  |  |     +- javax.inject:javax.inject:jar:1:compile
[INFO] |  |  |  |  |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  |  |  \- org.apache.hadoop:hadoop-yarn-server-common:jar:2.2.0:compile
[INFO] |  |  |  \- org.apache.hadoop:hadoop-mapreduce-client-shuffle:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-yarn-api:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-core:jar:2.2.0:compile
[INFO] |  |  |  \- org.apache.hadoop:hadoop-yarn-common:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-jobclient:jar:2.2.0:compile
[INFO] |  |  \- org.apache.hadoop:hadoop-annotations:jar:2.2.0:compile
[INFO] |  +- org.apache.spark:spark-launcher_2.11:jar:2.1.0:compile
[INFO] |  +- org.apache.spark:spark-network-common_2.11:jar:2.1.0:compile
[INFO] |  |  \- org.fusesource.leveldbjni:leveldbjni-all:jar:1.8:compile
[INFO] |  +- org.apache.spark:spark-network-shuffle_2.11:jar:2.1.0:compile
[INFO] |  +- org.apache.spark:spark-unsafe_2.11:jar:2.1.0:compile
[INFO] |  +- net.java.dev.jets3t:jets3t:jar:0.7.1:compile
[INFO] |  |  \- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] |  +- org.apache.curator:curator-recipes:jar:2.4.0:compile
[INFO] |  |  +- org.apache.curator:curator-framework:jar:2.4.0:compile
[INFO] |  |  +- org.apache.zookeeper:zookeeper:jar:3.4.5:compile
[INFO] |  |  \- com.google.guava:guava:jar:14.0.1:compile
[INFO] |  +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] |  +- org.apache.commons:commons-math3:jar:3.4.1:compile
[INFO] |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] |  +- com.ning:compress-lzf:jar:1.0.3:compile
[INFO] |  +- org.xerial.snappy:snappy-java:jar:1.1.2.6:compile
[INFO] |  +- net.jpountz.lz4:lz4:jar:1.3.0:compile
[INFO] |  +- org.roaringbitmap:RoaringBitmap:jar:0.5.11:compile
[INFO] |  +- commons-net:commons-net:jar:2.2:compile
[INFO] |  +- org.scala-lang:scala-library:jar:2.11.8:compile
[INFO] |  +- org.json4s:json4s-jackson_2.11:jar:3.2.11:compile
[INFO] |  |  \- org.json4s:json4s-core_2.11:jar:3.2.11:compile
[INFO] |  |     +- org.json4s:json4s-ast_2.11:jar:3.2.11:compile
[INFO] |  |     +- com.thoughtworks.paranamer:paranamer:jar:2.6:compile
[INFO] |  |     \- org.scala-lang:scalap:jar:2.11.0:compile
[INFO] |  |        \- org.scala-lang:scala-compiler:jar:2.11.0:compile
[INFO] |  |           \- org.scala-lang.modules:scala-parser-combinators_2.11:jar:1.0.1:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-client:jar:2.22.2:compile
[INFO] |  |  +- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile
[INFO] |  |  +- org.glassfish.hk2:hk2-api:jar:2.4.0-b34:compile
[INFO] |  |  |  +- org.glassfish.hk2:hk2-utils:jar:2.4.0-b34:compile
[INFO] |  |  |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b34:compile
[INFO] |  |  +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b34:compile
[INFO] |  |  \- org.glassfish.hk2:hk2-locator:jar:2.4.0-b34:compile
[INFO] |  |     \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-common:jar:2.22.2:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] |  |  +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.22.2:compile
[INFO] |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-server:jar:2.22.2:compile
[INFO] |  |  +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.22.2:compile
[INFO] |  |  \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.22.2:compile
[INFO] |  +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.22.2:compile
[INFO] |  +- io.netty:netty-all:jar:4.0.42.Final:compile
[INFO] |  +- io.netty:netty:jar:3.8.0.Final:compile
[INFO] |  +- com.clearspring.analytics:stream:jar:2.7.0:compile
[INFO] |  +- io.dropwizard.metrics:metrics-core:jar:3.1.2:compile
[INFO] |  +- io.dropwizard.metrics:metrics-jvm:jar:3.1.2:compile
[INFO] |  +- io.dropwizard.metrics:metrics-json:jar:3.1.2:compile
[INFO] |  +- io.dropwizard.metrics:metrics-graphite:jar:3.1.2:compile
[INFO] |  +- com.fasterxml.jackson.module:jackson-module-scala_2.11:jar:2.6.5:compile
[INFO] |  |  +- org.scala-lang:scala-reflect:jar:2.11.7:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-paranamer:jar:2.6.5:compile
[INFO] |  +- org.apache.ivy:ivy:jar:2.4.0:compile
[INFO] |  +- oro:oro:jar:2.0.8:compile
[INFO] |  +- net.razorvine:pyrolite:jar:4.13:compile
[INFO] |  +- net.sf.py4j:py4j:jar:0.10.4:compile
[INFO] |  +- org.apache.spark:spark-tags_2.11:jar:2.1.0:compile
[INFO] |  |  \- org.scalatest:scalatest_2.11:jar:2.2.6:compile
[INFO] |  |     \- org.scala-lang.modules:scala-xml_2.11:jar:1.0.2:compile
[INFO] |  +- org.apache.commons:commons-crypto:jar:1.0.0:compile
[INFO] |  \- org.spark-project.spark:unused:jar:1.0.0:compile
[INFO] +- org.apache.hadoop:hadoop-aws:jar:2.7.2:compile
[INFO] |  \- org.apache.hadoop:hadoop-common:jar:2.7.2:compile
[INFO] |     +- commons-cli:commons-cli:jar:1.2:compile
[INFO] |     +- xmlenc:xmlenc:jar:0.52:compile
[INFO] |     +- commons-io:commons-io:jar:2.4:compile
[INFO] |     +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] |     +- org.mortbay.jetty:jetty:jar:6.1.26:compile
[INFO] |     +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
[INFO] |     +- javax.servlet.jsp:jsp-api:jar:2.1:runtime
[INFO] |     +- com.sun.jersey:jersey-core:jar:1.9:compile
[INFO] |     +- com.sun.jersey:jersey-json:jar:1.9:compile
[INFO] |     |  +- org.codehaus.jettison:jettison:jar:1.1:compile
[INFO] |     |  +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:compile
[INFO] |     |  |  \- javax.xml.bind:jaxb-api:jar:2.2.2:compile
[INFO] |     |  |     +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] |     |  |     \- javax.activation:activation:jar:1.1:compile
[INFO] |     |  +- org.codehaus.jackson:jackson-jaxrs:jar:1.8.3:compile
[INFO] |     |  \- org.codehaus.jackson:jackson-xc:jar:1.8.3:compile
[INFO] |     +- com.sun.jersey:jersey-server:jar:1.9:compile
[INFO] |     |  \- asm:asm:jar:3.1:compile
[INFO] |     +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |     +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO] |     |  +- commons-digester:commons-digester:jar:1.8:compile
[INFO] |     |  |  \- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] |     |  \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO] |     +- org.apache.avro:avro:jar:1.7.4:compile
[INFO] |     +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
[INFO] |     +- com.google.code.gson:gson:jar:2.2.4:compile
[INFO] |     +- org.apache.hadoop:hadoop-auth:jar:2.7.2:compile
[INFO] |     |  \- org.apache.directory.server:apacheds-kerberos-codec:jar:2.0.0-M15:compile
[INFO] |     |     +- org.apache.directory.server:apacheds-i18n:jar:2.0.0-M15:compile
[INFO] |     |     +- org.apache.directory.api:api-asn1-api:jar:1.0.0-M20:compile
[INFO] |     |     \- org.apache.directory.api:api-util:jar:1.0.0-M20:compile
[INFO] |     +- com.jcraft:jsch:jar:0.1.42:compile
[INFO] |     +- org.apache.curator:curator-client:jar:2.7.1:compile
[INFO] |     +- org.apache.htrace:htrace-core:jar:3.1.0-incubating:compile
[INFO] |     \- org.apache.commons:commons-compress:jar:1.4.1:compile
[INFO] |        \- org.tukaani:xz:jar:1.0:compile
[INFO] +- org.apache.spark:spark-sql_2.10:jar:2.1.0:compile
[INFO] |  +- com.univocity:univocity-parsers:jar:2.2.1:compile
[INFO] |  +- org.apache.spark:spark-sketch_2.10:jar:2.1.0:compile
[INFO] |  +- org.apache.spark:spark-core_2.10:jar:2.1.0:compile
[INFO] |  |  +- com.twitter:chill_2.10:jar:0.8.0:compile
[INFO] |  |  +- org.apache.spark:spark-launcher_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.apache.spark:spark-network-common_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.apache.spark:spark-network-shuffle_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.apache.spark:spark-unsafe_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.json4s:json4s-jackson_2.10:jar:3.2.11:compile
[INFO] |  |  |  \- org.json4s:json4s-core_2.10:jar:3.2.11:compile
[INFO] |  |  |     \- org.json4s:json4s-ast_2.10:jar:3.2.11:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-scala_2.10:jar:2.6.5:compile
[INFO] |  +- org.apache.spark:spark-catalyst_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.codehaus.janino:janino:jar:3.0.0:compile
[INFO] |  |  +- org.codehaus.janino:commons-compiler:jar:3.0.0:compile
[INFO] |  |  \- org.antlr:antlr4-runtime:jar:4.5.3:compile
[INFO] |  +- org.apache.spark:spark-tags_2.10:jar:2.1.0:compile
[INFO] |  |  \- org.scalatest:scalatest_2.10:jar:2.2.6:compile
[INFO] |  +- org.apache.parquet:parquet-column:jar:1.8.1:compile
[INFO] |  |  +- org.apache.parquet:parquet-common:jar:1.8.1:compile
[INFO] |  |  \- org.apache.parquet:parquet-encoding:jar:1.8.1:compile
[INFO] |  \- org.apache.parquet:parquet-hadoop:jar:1.8.1:compile
[INFO] |     +- org.apache.parquet:parquet-format:jar:2.3.0-incubating:compile
[INFO] |     \- org.apache.parquet:parquet-jackson:jar:1.8.1:compile
[INFO] +- com.amazonaws:aws-java-sdk:jar:1.7.4:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.2:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.2:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.3:compile
[INFO] |  \- joda-time:joda-time:jar:2.9.9:compile (version selected from constraint [2.2,))
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.7.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.3:compile
[INFO] \- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.3:compile
```

答:
After massaging my pom file I was able to get to a stable with the following pom file and packaging with -

mvn clean compile assembly:single;

```<code><?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.group</groupId>
    <artifactId>arifact</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <jdk.version>1.8</jdk.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-aws -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-aws</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency> <!-- Spark dependency -->
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.11</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.229</version>
        </dependency>
    </dependencies>
    <build>
      <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>2.3.2</version>
                  <configuration>
                      <source>${jdk.version}</source>
                      <target>${jdk.version}</target>
                  </configuration>
              </plugin>
          <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                  <archive>
                      <manifest>
                          <mainClass>com.group.artifact.MainClass</mainClass>
                      </manifest>
                  </archive>
                  <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
              </configuration>
          </plugin>
      </plugins>
    </build>
</project>
</code>```

Body:
I have a java spark process which I call using -
spark-submit --class MyClass target/MyJar.jar

The last part of this process is to write it locally and then copy it to s3 as it need to have specific name (can also be written to s3 and mv'ed there, for the sake of the question the error stays the same).

The code compiles and runs but when it get to the code snippet below I get the following error -

java.lang.NoClassDefFoundError: com/amazonaws/services/s3/AmazonS3    at
  java.lang.Class.forName0(Native Method)   at
  java.lang.Class.forName(Class.java:348)   at
  org.apache.spark.util.Utils$.classForName(Utils.scala:229)    at
  org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:700)
    at
  org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:187)
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:212)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:126)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) Caused
  by: java.lang.ClassNotFoundException:
  com.amazonaws.services.s3.AmazonS3    at
  java.net.URLClassLoader.findClass(URLClassLoader.java:381)    at
  java.lang.ClassLoader.loadClass(ClassLoader.java:424)     at
  java.lang.ClassLoader.loadClass(ClassLoader.java:357)

public static void saveToS3(Dataset<Row> df, String outputBucket, String outputPath) throws IOException {
    String tmpFile = "temp" + Long.toString(System.nanoTime());
    df.coalesce(1).write().option("header", true).csv(tmpFile);
    File directory = new File(tmpFile);
    AmazonS3 s3client = new AmazonS3Client();
    for (File file : directory.listFiles()) {
        if (file.getName().startsWith("part_") || file.getName().endsWith("csv")) {
            s3client.putObject(new PutObjectRequest(outputBucket, getS3path(outputPath), file));
            }
        file.delete();
    }
    directory.delete();
}

I use the following dependencies which might be relevant

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-aws</artifactId>
            <version>2.7.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>servlet-api-2.5</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Any idea?

Dependency tree -

[INFO] +- org.apache.spark:spark-core_2.11:jar:2.1.0:compile
[INFO] |  +- org.apache.avro:avro-mapred:jar:hadoop2:1.7.7:compile
[INFO] |  |  +- org.apache.avro:avro-ipc:jar:1.7.7:compile
[INFO] |  |  +- org.apache.avro:avro-ipc:jar:tests:1.7.7:compile
[INFO] |  |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] |  |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] |  +- com.twitter:chill_2.11:jar:0.8.0:compile
[INFO] |  |  \- com.esotericsoftware:kryo-shaded:jar:3.0.3:compile
[INFO] |  |     +- com.esotericsoftware:minlog:jar:1.3.0:compile
[INFO] |  |     \- org.objenesis:objenesis:jar:2.1:compile
[INFO] |  +- com.twitter:chill-java:jar:0.8.0:compile
[INFO] |  +- org.apache.xbean:xbean-asm5-shaded:jar:4.4:compile
[INFO] |  +- org.apache.hadoop:hadoop-client:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-hdfs:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-app:jar:2.2.0:compile
[INFO] |  |  |  +- org.apache.hadoop:hadoop-mapreduce-client-common:jar:2.2.0:compile
[INFO] |  |  |  |  +- org.apache.hadoop:hadoop-yarn-client:jar:2.2.0:compile
[INFO] |  |  |  |  |  \- com.google.inject:guice:jar:3.0:compile
[INFO] |  |  |  |  |     +- javax.inject:javax.inject:jar:1:compile
[INFO] |  |  |  |  |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  |  |  \- org.apache.hadoop:hadoop-yarn-server-common:jar:2.2.0:compile
[INFO] |  |  |  \- org.apache.hadoop:hadoop-mapreduce-client-shuffle:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-yarn-api:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-core:jar:2.2.0:compile
[INFO] |  |  |  \- org.apache.hadoop:hadoop-yarn-common:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-jobclient:jar:2.2.0:compile
[INFO] |  |  \- org.apache.hadoop:hadoop-annotations:jar:2.2.0:compile
[INFO] |  +- org.apache.spark:spark-launcher_2.11:jar:2.1.0:compile
[INFO] |  +- org.apache.spark:spark-network-common_2.11:jar:2.1.0:compile
[INFO] |  |  \- org.fusesource.leveldbjni:leveldbjni-all:jar:1.8:compile
[INFO] |  +- org.apache.spark:spark-network-shuffle_2.11:jar:2.1.0:compile
[INFO] |  +- org.apache.spark:spark-unsafe_2.11:jar:2.1.0:compile
[INFO] |  +- net.java.dev.jets3t:jets3t:jar:0.7.1:compile
[INFO] |  |  \- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] |  +- org.apache.curator:curator-recipes:jar:2.4.0:compile
[INFO] |  |  +- org.apache.curator:curator-framework:jar:2.4.0:compile
[INFO] |  |  +- org.apache.zookeeper:zookeeper:jar:3.4.5:compile
[INFO] |  |  \- com.google.guava:guava:jar:14.0.1:compile
[INFO] |  +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] |  +- org.apache.commons:commons-math3:jar:3.4.1:compile
[INFO] |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] |  +- com.ning:compress-lzf:jar:1.0.3:compile
[INFO] |  +- org.xerial.snappy:snappy-java:jar:1.1.2.6:compile
[INFO] |  +- net.jpountz.lz4:lz4:jar:1.3.0:compile
[INFO] |  +- org.roaringbitmap:RoaringBitmap:jar:0.5.11:compile
[INFO] |  +- commons-net:commons-net:jar:2.2:compile
[INFO] |  +- org.scala-lang:scala-library:jar:2.11.8:compile
[INFO] |  +- org.json4s:json4s-jackson_2.11:jar:3.2.11:compile
[INFO] |  |  \- org.json4s:json4s-core_2.11:jar:3.2.11:compile
[INFO] |  |     +- org.json4s:json4s-ast_2.11:jar:3.2.11:compile
[INFO] |  |     +- com.thoughtworks.paranamer:paranamer:jar:2.6:compile
[INFO] |  |     \- org.scala-lang:scalap:jar:2.11.0:compile
[INFO] |  |        \- org.scala-lang:scala-compiler:jar:2.11.0:compile
[INFO] |  |           \- org.scala-lang.modules:scala-parser-combinators_2.11:jar:1.0.1:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-client:jar:2.22.2:compile
[INFO] |  |  +- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile
[INFO] |  |  +- org.glassfish.hk2:hk2-api:jar:2.4.0-b34:compile
[INFO] |  |  |  +- org.glassfish.hk2:hk2-utils:jar:2.4.0-b34:compile
[INFO] |  |  |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b34:compile
[INFO] |  |  +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b34:compile
[INFO] |  |  \- org.glassfish.hk2:hk2-locator:jar:2.4.0-b34:compile
[INFO] |  |     \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-common:jar:2.22.2:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] |  |  +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.22.2:compile
[INFO] |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-server:jar:2.22.2:compile
[INFO] |  |  +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.22.2:compile
[INFO] |  |  \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.22.2:compile
[INFO] |  +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.22.2:compile
[INFO] |  +- io.netty:netty-all:jar:4.0.42.Final:compile
[INFO] |  +- io.netty:netty:jar:3.8.0.Final:compile
[INFO] |  +- com.clearspring.analytics:stream:jar:2.7.0:compile
[INFO] |  +- io.dropwizard.metrics:metrics-core:jar:3.1.2:compile
[INFO] |  +- io.dropwizard.metrics:metrics-jvm:jar:3.1.2:compile
[INFO] |  +- io.dropwizard.metrics:metrics-json:jar:3.1.2:compile
[INFO] |  +- io.dropwizard.metrics:metrics-graphite:jar:3.1.2:compile
[INFO] |  +- com.fasterxml.jackson.module:jackson-module-scala_2.11:jar:2.6.5:compile
[INFO] |  |  +- org.scala-lang:scala-reflect:jar:2.11.7:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-paranamer:jar:2.6.5:compile
[INFO] |  +- org.apache.ivy:ivy:jar:2.4.0:compile
[INFO] |  +- oro:oro:jar:2.0.8:compile
[INFO] |  +- net.razorvine:pyrolite:jar:4.13:compile
[INFO] |  +- net.sf.py4j:py4j:jar:0.10.4:compile
[INFO] |  +- org.apache.spark:spark-tags_2.11:jar:2.1.0:compile
[INFO] |  |  \- org.scalatest:scalatest_2.11:jar:2.2.6:compile
[INFO] |  |     \- org.scala-lang.modules:scala-xml_2.11:jar:1.0.2:compile
[INFO] |  +- org.apache.commons:commons-crypto:jar:1.0.0:compile
[INFO] |  \- org.spark-project.spark:unused:jar:1.0.0:compile
[INFO] +- org.apache.hadoop:hadoop-aws:jar:2.7.2:compile
[INFO] |  \- org.apache.hadoop:hadoop-common:jar:2.7.2:compile
[INFO] |     +- commons-cli:commons-cli:jar:1.2:compile
[INFO] |     +- xmlenc:xmlenc:jar:0.52:compile
[INFO] |     +- commons-io:commons-io:jar:2.4:compile
[INFO] |     +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] |     +- org.mortbay.jetty:jetty:jar:6.1.26:compile
[INFO] |     +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
[INFO] |     +- javax.servlet.jsp:jsp-api:jar:2.1:runtime
[INFO] |     +- com.sun.jersey:jersey-core:jar:1.9:compile
[INFO] |     +- com.sun.jersey:jersey-json:jar:1.9:compile
[INFO] |     |  +- org.codehaus.jettison:jettison:jar:1.1:compile
[INFO] |     |  +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:compile
[INFO] |     |  |  \- javax.xml.bind:jaxb-api:jar:2.2.2:compile
[INFO] |     |  |     +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] |     |  |     \- javax.activation:activation:jar:1.1:compile
[INFO] |     |  +- org.codehaus.jackson:jackson-jaxrs:jar:1.8.3:compile
[INFO] |     |  \- org.codehaus.jackson:jackson-xc:jar:1.8.3:compile
[INFO] |     +- com.sun.jersey:jersey-server:jar:1.9:compile
[INFO] |     |  \- asm:asm:jar:3.1:compile
[INFO] |     +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |     +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO] |     |  +- commons-digester:commons-digester:jar:1.8:compile
[INFO] |     |  |  \- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] |     |  \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO] |     +- org.apache.avro:avro:jar:1.7.4:compile
[INFO] |     +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
[INFO] |     +- com.google.code.gson:gson:jar:2.2.4:compile
[INFO] |     +- org.apache.hadoop:hadoop-auth:jar:2.7.2:compile
[INFO] |     |  \- org.apache.directory.server:apacheds-kerberos-codec:jar:2.0.0-M15:compile
[INFO] |     |     +- org.apache.directory.server:apacheds-i18n:jar:2.0.0-M15:compile
[INFO] |     |     +- org.apache.directory.api:api-asn1-api:jar:1.0.0-M20:compile
[INFO] |     |     \- org.apache.directory.api:api-util:jar:1.0.0-M20:compile
[INFO] |     +- com.jcraft:jsch:jar:0.1.42:compile
[INFO] |     +- org.apache.curator:curator-client:jar:2.7.1:compile
[INFO] |     +- org.apache.htrace:htrace-core:jar:3.1.0-incubating:compile
[INFO] |     \- org.apache.commons:commons-compress:jar:1.4.1:compile
[INFO] |        \- org.tukaani:xz:jar:1.0:compile
[INFO] +- org.apache.spark:spark-sql_2.10:jar:2.1.0:compile
[INFO] |  +- com.univocity:univocity-parsers:jar:2.2.1:compile
[INFO] |  +- org.apache.spark:spark-sketch_2.10:jar:2.1.0:compile
[INFO] |  +- org.apache.spark:spark-core_2.10:jar:2.1.0:compile
[INFO] |  |  +- com.twitter:chill_2.10:jar:0.8.0:compile
[INFO] |  |  +- org.apache.spark:spark-launcher_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.apache.spark:spark-network-common_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.apache.spark:spark-network-shuffle_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.apache.spark:spark-unsafe_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.json4s:json4s-jackson_2.10:jar:3.2.11:compile
[INFO] |  |  |  \- org.json4s:json4s-core_2.10:jar:3.2.11:compile
[INFO] |  |  |     \- org.json4s:json4s-ast_2.10:jar:3.2.11:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-scala_2.10:jar:2.6.5:compile
[INFO] |  +- org.apache.spark:spark-catalyst_2.10:jar:2.1.0:compile
[INFO] |  |  +- org.codehaus.janino:janino:jar:3.0.0:compile
[INFO] |  |  +- org.codehaus.janino:commons-compiler:jar:3.0.0:compile
[INFO] |  |  \- org.antlr:antlr4-runtime:jar:4.5.3:compile
[INFO] |  +- org.apache.spark:spark-tags_2.10:jar:2.1.0:compile
[INFO] |  |  \- org.scalatest:scalatest_2.10:jar:2.2.6:compile
[INFO] |  +- org.apache.parquet:parquet-column:jar:1.8.1:compile
[INFO] |  |  +- org.apache.parquet:parquet-common:jar:1.8.1:compile
[INFO] |  |  \- org.apache.parquet:parquet-encoding:jar:1.8.1:compile
[INFO] |  \- org.apache.parquet:parquet-hadoop:jar:1.8.1:compile
[INFO] |     +- org.apache.parquet:parquet-format:jar:2.3.0-incubating:compile
[INFO] |     \- org.apache.parquet:parquet-jackson:jar:1.8.1:compile
[INFO] +- com.amazonaws:aws-java-sdk:jar:1.7.4:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.2:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.2:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.3:compile
[INFO] |  \- joda-time:joda-time:jar:2.9.9:compile (version selected from constraint [2.2,))
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.7.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.3:compile
[INFO] \- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.3:compile

Body:
After massaging my pom file I was able to get to a stable with the following pom file and packaging with -

mvn clean compile assembly:single;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.group</groupId>
    <artifactId>arifact</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <jdk.version>1.8</jdk.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-aws -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-aws</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency> <!-- Spark dependency -->
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.11</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.229</version>
        </dependency>
    </dependencies>
    <build>
      <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>2.3.2</version>
                  <configuration>
                      <source>${jdk.version}</source>
                      <target>${jdk.version}</target>
                  </configuration>
              </plugin>
          <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                  <archive>
                      <manifest>
                          <mainClass>com.group.artifact.MainClass</mainClass>
                      </manifest>
                  </archive>
                  <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
              </configuration>
          </plugin>
      </plugins>
    </build>
</project>


来源: stackexchange

问:
Angular auth guard Can't resolve all parameters
im tryin to create an auth.guard.service but for some reason i get error Can't resolve all parameters for AuthGuard: (?, [object Object])., here is the services.

```<code>import { Observable } from 'rxjs/Observable';
// import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  user = true;
  constructor(private auth: any, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | boolean {
    if (this.user) {
      return true;
    }
    this.router.navigate(['']);
    return false;
  }
}
</code>```

and here is the app.module

```<code>import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavComponent } from './components/nav/nav.component';
import { HomeComponent } from './components/home/home.component';
import { GalleryComponent } from './components/gallery/gallery.component';
import { FooterComponent } from './components/footer/footer.component';

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ScrollToModule } from 'ng2-scroll-to';
import { NgxCarouselModule } from 'ngx-carousel';
import { AnimateOnScrollModule } from 'ng2-animate-on-scroll';

import { AuthGuard } from './services/auth.guard.service';

import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'gallery', component: GalleryComponent, canActivate: [AuthGuard] }
];

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

@NgModule({
  declarations: [
    AppComponent,
    NavComponent,
    HomeComponent,
    GalleryComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgxCarouselModule,
    NgbModule.forRoot(),
    ScrollToModule.forRoot(),
    RouterModule.forRoot(routes),
    AnimateOnScrollModule.forRoot(),
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  exports: [
    TranslateModule
  ],
  providers: [AuthGuard],
  bootstrap: [AppComponent]
})
export class AppModule { }
</code>```

i dont know where could be error is, the services that im importin on the auth.guard.services is commented because i not using it yet, just in case, for some reason i can resolve all the parameters if somebody have an idea.

答: You can use "any" in your constructor.  constructor(private auth: any, private router: Router).  Change it to your service like authService.

ContentLicense: CC BY-SA 3.0

Body:
im tryin to create an auth.guard.service but for some reason i get error Can't resolve all parameters for AuthGuard: (?, [object Object])., here is the services.

import { Observable } from 'rxjs/Observable';
// import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  user = true;
  constructor(private auth: any, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | boolean {
    if (this.user) {
      return true;
    }
    this.router.navigate(['']);
    return false;
  }
}

and here is the app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavComponent } from './components/nav/nav.component';
import { HomeComponent } from './components/home/home.component';
import { GalleryComponent } from './components/gallery/gallery.component';
import { FooterComponent } from './components/footer/footer.component';

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ScrollToModule } from 'ng2-scroll-to';
import { NgxCarouselModule } from 'ngx-carousel';
import { AnimateOnScrollModule } from 'ng2-animate-on-scroll';

import { AuthGuard } from './services/auth.guard.service';

import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'gallery', component: GalleryComponent, canActivate: [AuthGuard] }
];

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

@NgModule({
  declarations: [
    AppComponent,
    NavComponent,
    HomeComponent,
    GalleryComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgxCarouselModule,
    NgbModule.forRoot(),
    ScrollToModule.forRoot(),
    RouterModule.forRoot(routes),
    AnimateOnScrollModule.forRoot(),
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  exports: [
    TranslateModule
  ],
  providers: [AuthGuard],
  bootstrap: [AppComponent]
})
export class AppModule { }

i dont know where could be error is, the services that im importin on the auth.guard.services is commented because i not using it yet, just in case, for some reason i can resolve all the parameters if somebody have an idea.


问:
How to implement Array.prototype.sort default compare function?

Background

I need to implement a function with the same behaviour as the default compare function of Array.prototype.sort

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

After reading the documentation I stumbled upon this:

The default sort order is according to string Unicode code points.

What does this mean? Does it mean I convert every object to a string?

If so, assuming I have the array [2, "a", { hello: "world" }] would these steps be correct?

convert [2, "a", { hello: "world" }] to ["2", "a", '{ hello: "world" }']

convert the first character of every string to a Number

order by that number

Question

How do I implement a compare function that given any object, behaves exactly as the compare function from sort?

Notes

Reading the ECMA specification:

http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort

I now believe that if comparefn is undefined, then they use this algorithm as a default:

http://www.ecma-international.org/ecma-262/6.0/#sec-sortcompare

Can someone confirm?

答:
It looks like the > and < are enough for this.

You could take a look at an implementation of Array.prototype.sort I did a while back. It might help give you ideas for what you need.


问:
Empty DataFrame doesn't admit its empty
I must not understand something about emptiness when it comes to pandas DataFrames. I have a DF with empty rows but when I isolate one of these rows its not empty.

Here I've made a dataframe:

```<code>>>> df = pandas.DataFrame(columns=[1,2,3], data=[[1,2,3],[1,None,3],[None, None, None],[3,2,1],[4,5,6],[None,None,None],[None,None,None]])
>>> df
     1    2    3
0  1.0  2.0  3.0
1  1.0  NaN  3.0
2  NaN  NaN  NaN
3  3.0  2.0  1.0
4  4.0  5.0  6.0
5  NaN  NaN  NaN
6  NaN  NaN  NaN
</code>```

Then I know row '2' is full of  nothing so I check for that...

```<code>>>> df[2:3].empty
    False
</code>```

Odd. So I split it out into its own dataframe:

```<code>>>> df1 = df[2:3]
>>> df1
    1   2   3
2 NaN NaN NaN

>>> df1.empty
False
</code>```

How do I check for emptiness (all the elements in a row being None or NaN?)

http://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.empty.html

答:
I guess you are looking for something like this:

```<code>In [296]: df[5:]
Out[296]:
    1   2   3
5 NaN NaN NaN
6 NaN NaN NaN

In [297]: df[5:].isnull().all(1).all()
Out[297]: True
</code>```

or even better (as proposed by @IanS):

```<code>In [300]: df[5:].isnull().all().all()
Out[300]: True
</code>```

Title: Empty DataFrame doesn't admit its empty

Body:
I must not understand something about emptiness when it comes to pandas DataFrames. I have a DF with empty rows but when I isolate one of these rows its not empty.

Here I've made a dataframe:

>>> df = pandas.DataFrame(columns=[1,2,3], data=[[1,2,3],[1,None,3],[None, None, None],[3,2,1],[4,5,6],[None,None,None],[None,None,None]])
>>> df
     1    2    3
0  1.0  2.0  3.0
1  1.0  NaN  3.0
2  NaN  NaN  NaN
3  3.0  2.0  1.0
4  4.0  5.0  6.0
5  NaN  NaN  NaN
6  NaN  NaN  NaN

Then I know row '2' is full of  nothing so I check for that...

>>> df[2:3].empty
    False

Odd. So I split it out into its own dataframe:

>>> df1 = df[2:3]
>>> df1
    1   2   3
2 NaN NaN NaN

>>> df1.empty
False

How do I check for emptiness (all the elements in a row being None or NaN?)

http://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.empty.html

Body:
I guess you are looking for something like this:

In [296]: df[5:]
Out[296]:
    1   2   3
5 NaN NaN NaN
6 NaN NaN NaN

In [297]: df[5:].isnull().all(1).all()
Out[297]: True

or even better (as proposed by @IanS):

In [300]: df[5:].isnull().all().all()
Out[300]: True


问:
How to highlight time ranges on a plot?
I have a few signals sampled over time which I plot in R using continuous lines. Additionally, I would like to highlight several specific time ranges on the plot.

My current approach is to draw full-height transparent rectangles with appropriate width and horizontal position which match the time range. I think this is a good representation, as it clearly separates points inside the range from those outside of it; but are there better ones?

And the second, practical part of the question. Now I'm plotting the signals like this:

```<code>p <- ggplot(data=gs, mapping=aes(x=frameno, y=value, col=variable)) +
       geom_line()
p
</code>```

I have tried to draw an alpha blended rectangle manually:

```<code>p + geom_rect(aes(xmin=600, xmax=650, ymin=-3, ymax=3),
              colour=alpha("grey20", 0.5), fill.alpha=0.5)
</code>```

— but no success so far. Any hints?

答:
I think drawing rectangles just work fine, I have no idea about better solution, if a simple vertical line or lines are not enough.

And just use alpha=0.5 instead of fill.alpha=0.5 for the transparency issue also specifying inherit.aes = FALSE in geom_rect(). E.g. making a plot from the diamonds data:

```<code>p <- ggplot(diamonds, aes(x=price, y=carat)) +
     geom_line(aes(color=color))

rect <- data.frame(xmin=5000, xmax=10000, ymin=-Inf, ymax=Inf)
p + geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
              color="grey20",
              alpha=0.5,
              inherit.aes = FALSE)
</code>```

Also note that ymin and ymax could be set to -Inf and Inf with ease.

Title: How to highlight time ranges on a plot?

Body:
I have a few signals sampled over time which I plot in R using continuous lines. Additionally, I would like to highlight several specific time ranges on the plot.

My current approach is to draw full-height transparent rectangles with appropriate width and horizontal position which match the time range. I think this is a good representation, as it clearly separates points inside the range from those outside of it; but are there better ones?

And the second, practical part of the question. Now I'm plotting the signals like this:

p <- ggplot(data=gs, mapping=aes(x=frameno, y=value, col=variable)) +
       geom_line()
p

I have tried to draw an alpha blended rectangle manually:

p + geom_rect(aes(xmin=600, xmax=650, ymin=-3, ymax=3),
              colour=alpha("grey20", 0.5), fill.alpha=0.5)

— but no success so far. Any hints?

Body:
I think drawing rectangles just work fine, I have no idea about better solution, if a simple vertical line or lines are not enough.

And just use alpha=0.5 instead of fill.alpha=0.5 for the transparency issue also specifying inherit.aes = FALSE in geom_rect(). E.g. making a plot from the diamonds data:

p <- ggplot(diamonds, aes(x=price, y=carat)) +
     geom_line(aes(color=color))

rect <- data.frame(xmin=5000, xmax=10000, ymin=-Inf, ymax=Inf)
p + geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
              color="grey20",
              alpha=0.5,
              inherit.aes = FALSE)

Also note that ymin and ymax could be set to -Inf and Inf with ease.


问:
Multiple Case Statements With Identical Expressions
I'm trying to evaluate a specific column to return five different columns - but the columns are based off the same expression in the CASE statements.

```<code>     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 1
          ELSE 0
     END AS [Invalid] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validMICcode] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validSerialNumber] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validFormat] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validProductionYear] ,
</code>```

I feel like is causing the table / column in question to be searched for the pattern five times, but I cannot figure out how rewrite it - or if it is even possible - to have one pattern search and define the columns based on the one search.

I'm trying different variations, but I cannot come up with the correct syntax for this issue:

```<code>CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 1
          ELSE 0
            CASE WHEN 1 THEN 1 END AS [Invalid]
            CASE WHEN 0 THEN 0 END AS [validMICode]
            CASE WHEN 0 THEN 0 END AS AS [validSerialNumber]
            CASE WHEN 0 THEN 0 END AS AS [validFormat]
            CASE WHEN 0 THEN 0 END AS AS [validProductionYear]
    END
</code>```

答:
You could do that with a CTE..</p>

```<code>with cte as(
select *,
CASE
   WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%'
      THEN 1
      ELSE 0
END as SomeColumn)

select *,
            CASE WHEN SomeColumn = 1 THEN 1 END AS [Invalid]
            CASE WHEN SomeColumn = 0 THEN 0 END AS [validMICode]
            ...
from cte
</code>```

Body:
I'm trying to evaluate a specific column to return five different columns - but the columns are based off the same expression in the CASE statements.

     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 1
          ELSE 0
     END AS [Invalid] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validMICcode] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validSerialNumber] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validFormat] ,
     CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 0
          ELSE 1
     END AS [validProductionYear] ,

I feel like is causing the table / column in question to be searched for the pattern five times, but I cannot figure out how rewrite it - or if it is even possible - to have one pattern search and define the columns based on the one search.

I'm trying different variations, but I cannot come up with the correct syntax for this issue:

CASE WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%' THEN 1
          ELSE 0
            CASE WHEN 1 THEN 1 END AS [Invalid]
            CASE WHEN 0 THEN 0 END AS [validMICode]
            CASE WHEN 0 THEN 0 END AS AS [validSerialNumber]
            CASE WHEN 0 THEN 0 END AS AS [validFormat]
            CASE WHEN 0 THEN 0 END AS AS [validProductionYear]
    END

Body:
You could do that with a CTE..

with cte as(
select *,
CASE
   WHEN va.HIN LIKE '%[-=!@#$%^&*()<>?:|\;./,]%'
      THEN 1
      ELSE 0
END as SomeColumn)

select *,
            CASE WHEN SomeColumn = 1 THEN 1 END AS [Invalid]
            CASE WHEN SomeColumn = 0 THEN 0 END AS [validMICode]
            ...
from cte


问:
Corda main repo v1.0 installation issue
I have downloaded corda main repo v1.0, example repo and template repo. To get started i imported the main repo but got error saying "repository not found". so i downloaded the M14.0 milestone release and imported it but got same error. Inorder to build my CorDapp can i directly import template? or is it required to install main repo or milestone as first step?</p>

答:
You do not need to install the main repo/milestone. Each Corda release is published online (e.g. here: https://mvnrepository.com/artifact/net.corda/corda), and the required Corda libraries will be downloaded and cached automatically when you import the template or example project.

Try following the instructions here: https://docs.corda.net/getting-set-up.html.

Title: Corda main repo v1.0 installation issue


问:
Can't get values submitted using thymeleaf
I am new with thymeleaf and I am having a doubt about how to retrieve a field from html (using Thymeleaf) to Java (Spring Boot). Follow the code and the error that I am having:

HTML (part with issue)

```<code>    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1.0" />
<title>Entity Migration</title>

<!-- CSS  -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
    rel="stylesheet">

<link href="css/style.css" type="text/css" rel="stylesheet"
    media="screen,projection" />
<link href="css/materialize.css" type="text/css" rel="stylesheet"
    media="screen,projection" />

</head>
<body>
    <nav class="light-blue lighten-1" role="navigation">
        <div class="nav-wrapper container">
            <a id="logo-container" href="#" class="brand-logo">Entity
                Migration</a>
            <ul class="right hide-on-med-and-down">
                <li><a href="#">Logout</a></li>
            </ul>

            <ul id="nav-mobile" class="side-nav">
                <li><a href="#">Entity Migration</a></li>
            </ul>
            <a href="#" data-activates="nav-mobile" class="button-collapse"><i
                class="material-icons">Logout</i></a>
        </div>
    </nav>
    <div class="section no-pad-bot" id="index-banner">
        <div class="container">
            <br> <br>
            <div class="row center">
                <h5 class="header col s12 light">Fill the form below to
                    generate your XML:</h5>
            </div>
            <br> <br>

        </div>
    </div>
    <form class="col s12" action="#" th:action="@{/prepareData}" th:object="${entity}" method="post">
        <div class="row">
            <div class="input-field col s4">
                <input type="number" id="release" name="release" placeholder="Release"
                    class="validate" th:value="*{release}"/>

            </div>
            <div class="input-field col s4">
                <input placeholder="Version" id="version" name="version" type="number"
                    class="validate" th:value="*{version}"/>
            </div>
        </div>
        <input type="submit" value="Generate XML" id="generate"
                class="btn-large waves-effect waves-light orange" />
        </div>
    </form>
        <!--  Scripts-->
    <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="js/init.js"></script>
    <script src="js/materialize.js"></script>
    <script>
        $(document).ready(function() {
            $('select').material_select();
        });
    </script>

</body>
</html>
</code>```

Java (Spring Boot Controller)

```<code> @PostMapping(value = "/prepareData")
  public String prepareData(@ModelAttribute(value="entity") EntityMigration entity) {
    TemplatePrepare tP = new TemplatePrepare();
    tP.prepareMainTemplate(entity);
    return "results";
</code>```

EntityMigration (Java Model)

```<code>public class EntityMigration {

    private String release;
    private String version;

    public String getRelease() {
        return release;
    }
    public void setRelease(String release) {
        this.release = release;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
}
</code>```

Error

```<code>    2017-11-16 14:01:02.445 ERROR 26932 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "mainForm": An error happened during template parsing (template: "class path resource [templates/mainForm.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/mainForm.html]")
</code>```

(...)

```<code>Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "release" (template: "mainForm" - line 55, col 23)
</code>```

(...)

```<code>org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'release' cannot be found on null
</code>```

(...)

What am I doing wrong?
Thank you.

答:
Parsing html exception was caused by forgetting to close input tags. Please replace:

```<code><input type="number" id="release" placeholder="Release" class="validate" th:value="*{release}">
<input placeholder="Version" id="version" type="number" class="validate">
</code>```

with:

```<code><input type="number" id="release" placeholder="Release" class="validate" th:value="*{release}"/>
<input placeholder="Version" id="version" type="number" class="validate"/>
</code>```

Latter error:

```<code>org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'release' cannot be found on null
</code>```

is caused by trying to access 'release' on 'entity' -> entity is null so Thymeleaf can't render it.

You must addAttribute 'entity' to model in order to render it.
In order to avoid SpelEvaluationException you can check for null in controller:

```<code>if (entityManager!= null) {
    model.addAttribute("entity", entityManager);
} else {
    model.addAttribute("entity", new EntityManager());
}
</code>```

Body:
I am new with thymeleaf and I am having a doubt about how to retrieve a field from html (using Thymeleaf) to Java (Spring Boot). Follow the code and the error that I am having:

HTML (part with issue)

    <!DOCTYPE html>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1.0" />
<title>Entity Migration</title>

<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
    rel="stylesheet">

<link href="css/style.css" type="text/css" rel="stylesheet"
    media="screen,projection" />
<link href="css/materialize.css" type="text/css" rel="stylesheet"
    media="screen,projection" />

            Entity
                Migration

Logout

Entity Migration

            Logout

Fill the form below to
                    generate your XML:

    <form class="col s12" action="#" th:action="@{/prepareData}" th:object="${entity}" method="post">

                <input type="number" id="release" name="release" placeholder="Release"
                    class="validate" th:value="*{release}"/>

                <input placeholder="Version" id="version" name="version" type="number"
                    class="validate" th:value="*{version}"/>

        <input type="submit" value="Generate XML" id="generate"
                class="btn-large waves-effect waves-light orange" />

    </form>

Java (Spring Boot Controller)

 @PostMapping(value = "/prepareData")
  public String prepareData(@ModelAttribute(value="entity") EntityMigration entity) {
    TemplatePrepare tP = new TemplatePrepare();
    tP.prepareMainTemplate(entity);
    return "results";

EntityMigration (Java Model)

public class EntityMigration {

    private String release;
    private String version;

    public String getRelease() {
        return release;
    }
    public void setRelease(String release) {
        this.release = release;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
}

Error

    2017-11-16 14:01:02.445 ERROR 26932 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "mainForm": An error happened during template parsing (template: "class path resource [templates/mainForm.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/mainForm.html]")

(...)

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "release" (template: "mainForm" - line 55, col 23)

(...)

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'release' cannot be found on null

(...)

What am I doing wrong?
Thank you.

Body:
Parsing html exception was caused by forgetting to close input tags. Please replace:

<input type="number" id="release" placeholder="Release" class="validate" th:value="*{release}">
<input placeholder="Version" id="version" type="number" class="validate">

with:

<input type="number" id="release" placeholder="Release" class="validate" th:value="*{release}"/>
<input placeholder="Version" id="version" type="number" class="validate"/>

Latter error:

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'release' cannot be found on null

is caused by trying to access 'release' on 'entity' -> entity is null so Thymeleaf can't render it.

You must addAttribute 'entity' to model in order to render it.
In order to avoid SpelEvaluationException you can check for null in controller:

if (entityManager!= null) {
    model.addAttribute("entity", entityManager);
} else {
    model.addAttribute("entity", new EntityManager());
}


答:
You're misunderstanding what empty is for. It's meant to check that the size of a series/dataframe is greater than 0, meaning there are rows. For example,

```<code>df.iloc[1:0]

Empty DataFrame
Columns: [1, 2, 3]
Index: []

df.iloc[1:0].empty
True
</code>```

If you want to check that a row has all NaNs, use isnull + all:

```<code>df.isnull().all(1)

0    False
1    False
2     True
3    False
4    False
5     True
6     True
dtype: bool
</code>```

For your example, this should do:

```<code>df[2:3].isnull().all(1).item()
True
</code>```

Note that you can't use item if your slice is more than one row in size.

Title: Empty DataFrame doesn't admit its empty

Body:
You're misunderstanding what empty is for. It's meant to check that the size of a series/dataframe is greater than 0, meaning there are rows. For example,

df.iloc[1:0]

Empty DataFrame
Columns: [1, 2, 3]
Index: []

df.iloc[1:0].empty
True

If you want to check that a row has all NaNs, use isnull + all:

df.isnull().all(1)

0    False
1    False
2     True
3    False
4    False
5     True
6     True
dtype: bool

For your example, this should do:

df[2:3].isnull().all(1).item()
True

Note that you can't use item if your slice is more than one row in size.


问:
Ionic Framework saas fatal error
I have changed nothing to my application, and the UI was created in the ionic GUI creator, but it still crashes when attempting to run ionic serve

Below is the error that I am getting:

```<code>[11:17:39]  sass: stdin, line: 1
            File to import not found or unreadable: /var/lndFinal/src/theme/variables.scss. Parent style sheet: stdin

[11:17:40]  dev server running: http://localhost:8100/

[OK] Development server running!
     Local: http://localhost:8100
     External: http://130.215.45.72:8100

[11:17:40]  Failed to open the browser: Command failed: xdg-open "http://localhost:8100" /bin/sh: 1: xdg-open: not found
{ Error: A watch configured to watch the following paths failed to start. It likely that a file referenced does not exist: /var/lndFinal/src/assets/**/*, /var/lndFinal/src/index.html, /var/lndFinal/src/manifest.json, /var/lndFinal/src/service-worker.js, /var/lndFinal/node_modules/ionicons/dist/fonts/**/*, /var/lndFinal/node_modules/ionic-angular/fonts/**/*, /var/lndFinal/node_modules/ionic-angular/polyfills/polyfills.js, /var/lndFinal/node_modules/sw-toolbox/sw-toolbox.js
    at new BuildError (/var/lndFinal/node_modules/@ionic/app-scripts/dist/util/errors.js:16:28)
    at Timeout._onTimeout (/var/lndFinal/node_modules/@ionic/app-scripts/dist/watch.js:71:20)
    at ontimeout (timers.js:471:11)
    at tryOnTimeout (timers.js:306:5)
    at Timer.listOnTimeout (timers.js:266:5) hasBeenLogged: false, isFatal: false }
</code>```

The file to import error at path: /var/lndFinal/src/theme/variables.scss doesn't exist, and neither do the other files being pointed at in the error.  Any ideas as to why this is happening?

答:
The answer ended up being that the original src directory generated using just a simple ionic start appName blank had the config files in them.  The UI src folder exported from the Ionic GUI creator does not have the config files (It also doesn't support the theme files in the src directory yet for version 3).

Steps:
1. Before importing new src UI folder, move all config and .js files to the main application folder
2. Move the theme folder from src to the main application folder as well.
3. Overwrite the src folder with the new UI src folder
4. Move the theme folder back into the src folder

Should be all set now!

Title: Ionic Framework saas fatal error

Body:
I have changed nothing to my application, and the UI was created in the ionic GUI creator, but it still crashes when attempting to run ionic serve

Below is the error that I am getting:

[11:17:39]  sass: stdin, line: 1
            File to import not found or unreadable: /var/lndFinal/src/theme/variables.scss. Parent style sheet: stdin

[11:17:40]  dev server running: http://localhost:8100/

[OK] Development server running!
     Local: http://localhost:8100
     External: http://130.215.45.72:8100

[11:17:40]  Failed to open the browser: Command failed: xdg-open "http://localhost:8100" /bin/sh: 1: xdg-open: not found
{ Error: A watch configured to watch the following paths failed to start. It likely that a file referenced does not exist: /var/lndFinal/src/assets/**/*, /var/lndFinal/src/index.html, /var/lndFinal/src/manifest.json, /var/lndFinal/src/service-worker.js, /var/lndFinal/node_modules/ionicons/dist/fonts/**/*, /var/lndFinal/node_modules/ionic-angular/fonts/**/*, /var/lndFinal/node_modules/ionic-angular/polyfills/polyfills.js, /var/lndFinal/node_modules/sw-toolbox/sw-toolbox.js
    at new BuildError (/var/lndFinal/node_modules/@ionic/app-scripts/dist/util/errors.js:16:28)
    at Timeout._onTimeout (/var/lndFinal/node_modules/@ionic/app-scripts/dist/watch.js:71:20)
    at ontimeout (timers.js:471:11)
    at tryOnTimeout (timers.js:306:5)
    at Timer.listOnTimeout (timers.js:266:5) hasBeenLogged: false, isFatal: false }

The file to import error at path: /var/lndFinal/src/theme/variables.scss doesn't exist, and neither do the other files being pointed at in the error.  Any ideas as to why this is happening?


问:
SQL dependency event not being triggered
First time working with SQL Dependency... but after having gone over several examples I feel as I am doing everything correct. I've checked that the Broker is Enabled. I've further checked that my query is correct. I am not receiving any exceptions at all! All and all everything seems as it should work... but it is not, and I have no idea how to begin to troubleshoot it without any exceptions being thrown.

Any help would be VERY much appreciated!

Here is my class:

```<code>public class NotificationEvent
{
    private delegate void RateChangeNotification(DataTable table);
    private SqlDependency dependency;
    string ConnectionString = @"ConnectionString";
    string UserName = Environment.UserName;

    public async void StartNotification()
    {
        SqlDependency.Start(this.ConnectionString, "UserNotificationsQueue");
        SqlConnection connection = new SqlConnection(this.ConnectionString);
        await connection.OpenAsync();

        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = string.Format("SELECT [NotificationID],[UserFrom],[UserTo],[DateTimeSent],[Notification] FROM [dbo].[PersonnellNotifications]", UserName);
        command.Notification = null;

        this.dependency = new SqlDependency(command, "Service=PostUserNotificationsQueue;", Int32.MaxValue);
        dependency.OnChange += new OnChangeEventHandler(this.SqlDependencyOnChange);
        await command.ExecuteReaderAsync();
    }

    private void SqlDependencyOnChange(object sender, SqlNotificationEventArgs eventArgs)
    {
        if (eventArgs.Info == SqlNotificationInfo.Invalid)
        {
            Console.WriteLine("The above notification query is not valid.");
        }
        else
        {
            Console.WriteLine("Notification Info: " + eventArgs.Info);
            Console.WriteLine("Notification source: " + eventArgs.Source);
            Console.WriteLine("Notification type: " + eventArgs.Type);
        }
    }

    public void StopNotification()
    {
        SqlDependency.Stop(this.ConnectionString, "QueueName");
    }
}
</code>```

I am initializing this from another classes IniatializeComponent() as seen:

```<code>private void InitializeComponent()
{
    // Initialize SQL Dependancy
    ne.StartNotification();
}
</code>```

答:
I have just tested following in my Code and Its working good. I have simplified your code. Please see if this is working and you are getting a call in OnNotificationChange on Db Change.</p>

```<code>public async void RegisterForNotification()
{
     var connectionString = @"ConnectionString";
     using (var connection = new SqlConnection(connectionString))
     {
         await connection.OpenAsync();

          var queryString = "Your Query String";
          using (var oCommand = new SqlCommand(queryString, connection))
          {
              // Starting the listener infrastructure...
              SqlDependency.Start(connectionString);

               var oDependency = new SqlDependency(oCommand);
               oDependency.OnChange += OnNotificationChange;

               // NOTE: You have to execute the command, or the notification will never fire.
                await oCommand.ExecuteReaderAsync();
            }
        }
    }

private void OnNotificationChange(object sender, SqlNotificationEventArgs e)
{
   Console.WriteLine("Notification Info: " + e.Info);
    //Re-register the SqlDependency.
   RegisterForNotification();
}
</code>```

Body:
First time working with SQL Dependency... but after having gone over several examples I feel as I am doing everything correct. I've checked that the Broker is Enabled. I've further checked that my query is correct. I am not receiving any exceptions at all! All and all everything seems as it should work... but it is not, and I have no idea how to begin to troubleshoot it without any exceptions being thrown.

Any help would be VERY much appreciated!

Here is my class:

public class NotificationEvent
{
    private delegate void RateChangeNotification(DataTable table);
    private SqlDependency dependency;
    string ConnectionString = @"ConnectionString";
    string UserName = Environment.UserName;

    public async void StartNotification()
    {
        SqlDependency.Start(this.ConnectionString, "UserNotificationsQueue");
        SqlConnection connection = new SqlConnection(this.ConnectionString);
        await connection.OpenAsync();

        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = string.Format("SELECT [NotificationID],[UserFrom],[UserTo],[DateTimeSent],[Notification] FROM [dbo].[PersonnellNotifications]", UserName);
        command.Notification = null;

        this.dependency = new SqlDependency(command, "Service=PostUserNotificationsQueue;", Int32.MaxValue);
        dependency.OnChange += new OnChangeEventHandler(this.SqlDependencyOnChange);
        await command.ExecuteReaderAsync();
    }

    private void SqlDependencyOnChange(object sender, SqlNotificationEventArgs eventArgs)
    {
        if (eventArgs.Info == SqlNotificationInfo.Invalid)
        {
            Console.WriteLine("The above notification query is not valid.");
        }
        else
        {
            Console.WriteLine("Notification Info: " + eventArgs.Info);
            Console.WriteLine("Notification source: " + eventArgs.Source);
            Console.WriteLine("Notification type: " + eventArgs.Type);
        }
    }

    public void StopNotification()
    {
        SqlDependency.Stop(this.ConnectionString, "QueueName");
    }
}

I am initializing this from another classes IniatializeComponent() as seen:

private void InitializeComponent()
{
    // Initialize SQL Dependancy
    ne.StartNotification();
}

Body:
I have just tested following in my Code and Its working good. I have simplified your code. Please see if this is working and you are getting a call in OnNotificationChange on Db Change.

public async void RegisterForNotification()
{
     var connectionString = @"ConnectionString";
     using (var connection = new SqlConnection(connectionString))
     {
         await connection.OpenAsync();

          var queryString = "Your Query String";
          using (var oCommand = new SqlCommand(queryString, connection))
          {
              // Starting the listener infrastructure...
              SqlDependency.Start(connectionString);

               var oDependency = new SqlDependency(oCommand);
               oDependency.OnChange += OnNotificationChange;

               // NOTE: You have to execute the command, or the notification will never fire.
                await oCommand.ExecuteReaderAsync();
            }
        }
    }

private void OnNotificationChange(object sender, SqlNotificationEventArgs e)
{
   Console.WriteLine("Notification Info: " + e.Info);
    //Re-register the SqlDependency.
   RegisterForNotification();
}


问:
Add NuGet package (ie. xunit.runner.console) at solution level
I need a NuGet (example xunit.runner.console) to run UT from command line.

Right now in one of project I have added xunit.runner.console as dependency so it is downloaded, path is known and I can use it in my build script.

This Project was removed and build-script is broken now. I will need to add it to another project, but the same situation can occur again (or other reasons).

I there a way to download this package but don't link it with concrete project?

答: You can install the nuget cli and then get the package needed using the build script. So, to answer your question, you can just use it in the build script and fetch the package before starting to build your project.</p>


问:
Is it possible to use Selenium Webdriver to stimulate several users hitting a site?
I have a crucial question please. Is it possible to use selenium webdriver to stimulate like 50 users hitting on a site at the same time? If not any recommendations on what to use for this test?

Thanks!

答:
You may want to better define the "at the same time" piece as normal populations of users arrive and depart chaotically with varying times between requests.   The client server model is pretty much built around this chaotic use and the availability of resources to service B when A in idle on its request stream.  You can easily pick out inexperienced performance testing scripts when the think time and iteration pacing go to zero when representing a population of users and not automated agents.

Do not take my word for this.  Look at your own HTTP logs for an application which has a human user population and your will begin to understand this chaotic usage.

As to the 50 users, pick just about any performance testing tool on the market to do the work.  All of the existing commercial tools have community editions which will scale to that level, in addition to the open source tools.  The large advantage of the commercial tools is that you typically get a single app installation with all integrations worked out.  The open source is a mashup of multiple tools if you want the same functionality.

No matter what tool you pick, open source or commercial, you will face a steep learning curve if this is not your core job with a good amount of experience behind your effort.  Plan for that efficiency drop.


问:
pymunk + tkinter: How to get ball to move to left or right with pymunk?
I have created a small example program where I use pymunk to do the physics and tkinter to do the GUI stuff (because I have given up trying to get pygame to install on my Mac...)

I have it working where a single ball is created, dropped, and bounces off the floor.  If I tilt the floor it starts bouncing to the left or right correctly.

What I cannot figure out is how to apply a force to the ball initially, so that its initial velocity is toward the right.

I'm including my code below.  It is probably something really simple, but I'm not a physicist and don't know anything about moments, etc., etc.

Thanks.

Vic

```<code>"""Simple example that bounces one ball against a floor.
The BallPhysics class defines the "model".  The Ball class is the "view".

@author: Victor Norman
"""

from tkinter import *
import pymunk
import pymunk.util
from pymunk import Vec2d
import math, sys, random

class Ball:

    RADIUS = 10

    def __init__(self, window):
        self._window = window
        self._window.title("Bouncing Ball with pymunk physics")

        self._model = BallPhysics()

        self._width = 400

        self._canvas = Canvas(self._window, bg='black',
                              width=self._width, height=self._width)
        self._canvas.pack()

        self._render()

    def _render(self):

        self._model.next_step()
        x, y = self._model.get_xy_for_ball()

        # subtract y values from self._width because y increases from 0 downward.
        self._canvas.create_oval(x - self.RADIUS, self._width - (y - self.RADIUS),
                                 x + self.RADIUS, self._width - (y + self.RADIUS),
                                 fill = 'white')
        self._canvas.after(20, self._render)

class BallPhysics:
    def __init__(self):

        self._space = pymunk.Space()
        self._space.gravity = (0.0, -900.0)

        self._balls = []

        mass = 10
        inertia = pymunk.moment_for_circle(mass, 0, Ball.RADIUS, (0, 0))
        body = pymunk.Body(mass, inertia)
        x = random.randint(50, 350)
        body.position = x, 400
        shape = pymunk.Circle(body, Ball.RADIUS, Vec2d(0,0))
        shape.elasticity = 0.9
        self._space.add(body, shape)
        self._balls.append(shape)

        # floor
        floor = pymunk.Segment(self._space.static_body, (0.0, 10.0), (400.0, 10.0), 1.0)
        floor.friction = 1.0
        floor.elasticity = 0.9
        self._space.add(floor)

    def next_step(self):
        # Remove balls that are below the bottom.
        balls_to_remove = []
        for ball in self._balls:
            if ball.body.position.y < 0:
                balls_to_remove.append(ball)
        for ball in balls_to_remove:
            self._space.remove(ball, ball.body)
            self._balls.remove(ball)

        if len(self._balls) >= 1:
            v = self._balls[0].body.position
            print("point = %.2f, %.2f" % (v.x, v.y))

        self._space.step(1 / 50)

    def get_xy_for_ball(self):
        ball_num = 0
        return (self._balls[ball_num].body.position.x,
                self._balls[ball_num].body.position.y)

main = Tk()
app = Ball(main)
main.mainloop()
</code>```

答:
You can use body.apply_impulse_at_local_point (or *at_world_point) on the body you want to push. The API is described here: http://www.pymunk.org/en/latest/pymunk.html#pymunk.Body.apply_impulse_at_local_point

So, in your code you could do body.apply_impulse_at_local_point((10000,0)) just right after you set the body position to x,400. Note that you need to modify the strength of the impulse to get the effect you want. Usually I create a vector with length one in the direction I want it to push, and then multiply that with how much impulse I want. like this: body.apply_impulse_at_local_point(10000 * Vec2d(1,0)). Then it gets easy to instead aim slightly upwards for example.

This example includes a shooting ball using apply impulse: https://github.com/viblo/pymunk/blob/master/examples/box2d_vertical_stack.py

Body:
I have created a small example program where I use pymunk to do the physics and tkinter to do the GUI stuff (because I have given up trying to get pygame to install on my Mac...)

I have it working where a single ball is created, dropped, and bounces off the floor.  If I tilt the floor it starts bouncing to the left or right correctly.

What I cannot figure out is how to apply a force to the ball initially, so that its initial velocity is toward the right.

I'm including my code below.  It is probably something really simple, but I'm not a physicist and don't know anything about moments, etc., etc.

Thanks.

Vic

"""Simple example that bounces one ball against a floor.
The BallPhysics class defines the "model".  The Ball class is the "view".

@author: Victor Norman
"""

from tkinter import *
import pymunk
import pymunk.util
from pymunk import Vec2d
import math, sys, random

class Ball:

    RADIUS = 10

    def __init__(self, window):
        self._window = window
        self._window.title("Bouncing Ball with pymunk physics")

        self._model = BallPhysics()

        self._width = 400

        self._canvas = Canvas(self._window, bg='black',
                              width=self._width, height=self._width)
        self._canvas.pack()

        self._render()

    def _render(self):

        self._model.next_step()
        x, y = self._model.get_xy_for_ball()

        # subtract y values from self._width because y increases from 0 downward.
        self._canvas.create_oval(x - self.RADIUS, self._width - (y - self.RADIUS),
                                 x + self.RADIUS, self._width - (y + self.RADIUS),
                                 fill = 'white')
        self._canvas.after(20, self._render)

class BallPhysics:
    def __init__(self):

        self._space = pymunk.Space()
        self._space.gravity = (0.0, -900.0)

        self._balls = []

        mass = 10
        inertia = pymunk.moment_for_circle(mass, 0, Ball.RADIUS, (0, 0))
        body = pymunk.Body(mass, inertia)
        x = random.randint(50, 350)
        body.position = x, 400
        shape = pymunk.Circle(body, Ball.RADIUS, Vec2d(0,0))
        shape.elasticity = 0.9
        self._space.add(body, shape)
        self._balls.append(shape)

        # floor
        floor = pymunk.Segment(self._space.static_body, (0.0, 10.0), (400.0, 10.0), 1.0)
        floor.friction = 1.0
        floor.elasticity = 0.9
        self._space.add(floor)

    def next_step(self):
        # Remove balls that are below the bottom.
        balls_to_remove = []
        for ball in self._balls:
            if ball.body.position.y < 0:
                balls_to_remove.append(ball)
        for ball in balls_to_remove:
            self._space.remove(ball, ball.body)
            self._balls.remove(ball)

        if len(self._balls) >= 1:
            v = self._balls[0].body.position
            print("point = %.2f, %.2f" % (v.x, v.y))

        self._space.step(1 / 50)

    def get_xy_for_ball(self):
        ball_num = 0
        return (self._balls[ball_num].body.position.x,
                self._balls[ball_num].body.position.y)

main = Tk()
app = Ball(main)
main.mainloop()


问:
How to get multiple controls to fire same event/command in Silverlight / XAML?
I am using Mvvm-Light Commands to route events from my Views to my ViewModels. Works great.

But I think I can be more elegant. This is actually probably just a standard XAML question:

I want to change this (10 buttons all with Click="keypadButton_Click" for their event handler):

```<code><Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0" Click="keypadButton_Click" />
<Button  Name="button2" Content="2" Grid.Row="0" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button3" Content="3" Grid.Row="0" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button4" Content="4" Grid.Row="1" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button5" Content="5" Grid.Row="1" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button6" Content="6" Grid.Row="1" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button7" Content="7" Grid.Row="2" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button8" Content="8" Grid.Row="2" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button9" Content="9" Grid.Row="2" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button0" Content="0" Grid.Row="3" Grid.Column="1" Click="keypadButton_Click"/>
</code>```

To 10 of these:

```<code><Button  x:Name="button1" Content="1" Grid.Row="0" Grid.Column="0">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <Command:EventToCommand
               Command="{Binding KeyPadButtonCommand}"
               CommandParameter="{Binding ElementName=button1, Path=Content }"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>
</code>```

But I don't want that much XAML. I would think there would be a way to "macroize" the Trigger so it would look something like this:

```<code><Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0"
         Click="{Binding _SendTheEventHere_}" />
</code>```

Where "{Binding _SendTheEventHere_}" should be replaced with some magic incantation that I am clueless about, but I know some wizard here on stackoverflow knows :-).

Make sense?

答:
I'm not sure if I understand you correctly, but you can use RelayCommand in MVVM Light Toolkit here is a sample code

Generally RelayCommand are implementation of ICommand interface that you can use

ContentLicense: CC BY-SA 2.5

Body:
I am using Mvvm-Light Commands to route events from my Views to my ViewModels. Works great.

But I think I can be more elegant. This is actually probably just a standard XAML question:

I want to change this (10 buttons all with Click="keypadButton_Click" for their event handler):

<Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0" Click="keypadButton_Click" />
<Button  Name="button2" Content="2" Grid.Row="0" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button3" Content="3" Grid.Row="0" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button4" Content="4" Grid.Row="1" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button5" Content="5" Grid.Row="1" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button6" Content="6" Grid.Row="1" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button7" Content="7" Grid.Row="2" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button8" Content="8" Grid.Row="2" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button9" Content="9" Grid.Row="2" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button0" Content="0" Grid.Row="3" Grid.Column="1" Click="keypadButton_Click"/>

To 10 of these:

<Button  x:Name="button1" Content="1" Grid.Row="0" Grid.Column="0">

            <Command:EventToCommand
               Command="{Binding KeyPadButtonCommand}"
               CommandParameter="{Binding ElementName=button1, Path=Content }"/>

</Button>

But I don't want that much XAML. I would think there would be a way to "macroize" the Trigger so it would look something like this:

<Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0"
         Click="{Binding _SendTheEventHere_}" />

Where "{Binding _SendTheEventHere_}" should be replaced with some magic incantation that I am clueless about, but I know some wizard here on stackoverflow knows :-).

Make sense?


问:
Combining two images into one
I need to take two images (given through URL inputs) and output something that looks like this:

I'm using Jimp to generate the image. This should be a relatively simple thing to do with a mask. For my project, I cannot use the canvas so if you have examples, try to not include the DOM. This is what i'm going for:

```<code>const jimp = require("jimp")
const split = (url1, url2) => {
    jimp.read(url1, (err, image) => {
    //mask
    //paste image from url2
    //return new image
    })
}
</code>```

答:
The example below demonstrates how to do that using MarvinJ.

Input Image A:

Input Image B:

Combination:

var canvas = document.getElementById("canvas");

// Image A
var imageA = new MarvinImage();
imageA.load("https://i.imgur.com/FLaixrz.jpg", imageLoaded);
// Image B
var imageB = new MarvinImage();
imageB.load("https://i.imgur.com/ayVZfpF.jpg", imageLoaded);

var loadedImages=0;
function imageLoaded(){

  if(++loadedImages == 2){
    var imageOut = new MarvinImage(imageA.getWidth(), imageA.getHeight());

    var end=imageA.getWidth();
    var step = imageA.getWidth()/imageA.getHeight();
    for(var y=0; y<imageA.getHeight(); y++){
      for(var x=0; x<imageA.getWidth(); x++){

         if(x < end){
          imageOut.setIntColor(x,y,imageA.getIntColor(x,y));
         } else{
          imageOut.setIntColor(x,y,imageB.getIntColor(x,y));
         }
      }
      end -= step;
    }

     imageOut.draw(canvas);
  }
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://www.marvinj.org/releases/marvinj-0.7.js"></script>
<canvas id="canvas" width="400" height="300"></canvas></code>```

Title: Combining two images into one

Body:
I need to take two images (given through URL inputs) and output something that looks like this:

I'm using Jimp to generate the image. This should be a relatively simple thing to do with a mask. For my project, I cannot use the canvas so if you have examples, try to not include the DOM. This is what i'm going for:

const jimp = require("jimp")
const split = (url1, url2) => {
    jimp.read(url1, (err, image) => {
    //mask
    //paste image from url2
    //return new image
    })
}

Body:
The example below demonstrates how to do that using MarvinJ.

Input Image A:

Input Image B:

Combination:

var canvas = document.getElementById("canvas");

// Image A
var imageA = new MarvinImage();
imageA.load("https://i.imgur.com/FLaixrz.jpg", imageLoaded);
// Image B
var imageB = new MarvinImage();
imageB.load("https://i.imgur.com/ayVZfpF.jpg", imageLoaded);

var loadedImages=0;
function imageLoaded(){

  if(++loadedImages == 2){
    var imageOut = new MarvinImage(imageA.getWidth(), imageA.getHeight());

    var end=imageA.getWidth();
    var step = imageA.getWidth()/imageA.getHeight();
    for(var y=0; y<imageA.getHeight(); y++){
      for(var x=0; x<imageA.getWidth(); x++){

         if(x < end){
          imageOut.setIntColor(x,y,imageA.getIntColor(x,y));
         } else{
          imageOut.setIntColor(x,y,imageB.getIntColor(x,y));
         }
      }
      end -= step;
    }

     imageOut.draw(canvas);
  }
}

<script src="https://www.marvinj.org/releases/marvinj-0.7.js"></script>
<canvas id="canvas" width="400" height="300"></canvas>


问:
frequency count on considering four columns in r
currently i am trying to count frequency of set of sequence of data frame.

```<code>A  B
1  a
1  b
1  c
2  a
2  b
2  c
</code>```

i have this data frame and i would like to count frequency of "B" of another data frame looking like this

```<code>C  D
1  a
1  a
1  b
1  b
2  b
2  c
2  c
</code>```

As you can see the number of rows is different so datatable(counts) does not work. i would like to it to look like this after frequency count is done

```<code>a  b  freq
1  a   2
1  b   2
1  c   0
2  a   0
2  b   1
2  c   2
</code>```

As you can see it makes counts of all the frequency even the 0 as the on some groups there is no data on it.

thanks for anyone that helps!

答:
```<code>df1_rows = Reduce(paste, df1)
df2_rows = Reduce(paste, df2)
data.frame(df1, freq = sapply(df1_rows, function(x) sum(df2_rows %in% x)),
           row.names = NULL)
#  A B freq
#1 1 a    2
#2 1 b    2
#3 1 c    0
#4 2 a    0
#5 2 b    1
#6 2 c    2
</code>```

DATA

```<code>df1 = data.frame(A = c(1L, 1L, 1L, 2L, 2L, 2L),
                 B = c("a", "b", "c", "a", "b", "c"))

df2 = data.frame(C = c(1L, 1L, 1L, 1L, 2L, 2L, 2L),
                 D = c("a", "a", "b", "b", "b", "c", "c"))
</code>```

Body:
currently i am trying to count frequency of set of sequence of data frame.

A  B
1  a
1  b
1  c
2  a
2  b
2  c

i have this data frame and i would like to count frequency of "B" of another data frame looking like this

C  D
1  a
1  a
1  b
1  b
2  b
2  c
2  c

As you can see the number of rows is different so datatable(counts) does not work. i would like to it to look like this after frequency count is done

a  b  freq
1  a   2
1  b   2
1  c   0
2  a   0
2  b   1
2  c   2

As you can see it makes counts of all the frequency even the 0 as the on some groups there is no data on it.

thanks for anyone that helps!

Body:
df1_rows = Reduce(paste, df1)
df2_rows = Reduce(paste, df2)
data.frame(df1, freq = sapply(df1_rows, function(x) sum(df2_rows %in% x)),
           row.names = NULL)
#  A B freq
#1 1 a    2
#2 1 b    2
#3 1 c    0
#4 2 a    0
#5 2 b    1
#6 2 c    2

DATA

df1 = data.frame(A = c(1L, 1L, 1L, 2L, 2L, 2L),
                 B = c("a", "b", "c", "a", "b", "c"))

df2 = data.frame(C = c(1L, 1L, 1L, 1L, 2L, 2L, 2L),
                 D = c("a", "a", "b", "b", "b", "c", "c"))


问:
How to group arrays of objects by value and add the result to another array with same value at specific key
I have this situation:

```<code>const array1 = [
  {
    node:
    {
      id: '789dg020hgn20ijahq',
      family: 'ps4',
      featuredImage: 'URL'
    }
  },
  {
    node:
     {
       id: 'gf80s70ga09ggds90ds000s9',
       family: 'xbox-one',
       featuredImage: 'URL'
     }
   }
]

const array2 = [
  {
    node:
      {
      id: 'h83g01whiehq8haos',
      family: 'nintendo-switch',
      gameName: 'Mario Cart'
    }
  },
  {
    node:
      {
      id: '1290qas9a0k19po1',
      family: 'xbox-one',
      gameName: 'COD WWII'
      }
    },
    {
      node:
        {
        id: '09ga09guh3njf32olkls',
        family: 'xbox-one',
        gameName: 'GOW 2'
        }
      }
]
</code>```

What I am trying to achieve is to group these arrays by family key and have this new array:

```<code>*output I want*
[{
  family: {
    id: 'gf80s70ga09ggds90ds000s9',
    family: 'xbox-one',
    featuredImage: 'URL',

    games: [
      0: {
        id: '1290qas9a0k19po1',
        family: 'xbox-one',
        gameName: 'COD WWII'
      },
      1: {
        id: '09ga09guh3njf32olkls',
        family: 'xbox-one',
        gameName: 'GOW 2'
      }
    ]
  },

  family: {
    id: 'h83g01whiehq8haos',
    family: 'nintendo-switch',
    featuredImage: 'URL',

    games: []
  }
]

*if the family of the array1 is not matched in array2*
family: {
   id: '789dg020hgn20ijahq',
   family: 'ps4',
   featuredImage: 'URL',

   games: []
}
</code>```

Using this code, I grouped the arrays by family, but I missed some informations. How can I modify the following code to have the desired result? Sorry but I am a little bit stuck....

```<code>const testing2 = result[1].map((obj) => { // result[1] comes from a Promise
    return obj.node
})
_.mapValues(_.groupBy(testing2, 'family'), (list) => list.map((o) => _.omit(o, 'family')))
</code>```

答:
You could take a Map and group by family.

var array1 = [{ node: { id: '789dg020hgn20ijahq', family: 'nintendo-switch', featuredImage: 'URL' } }, { node: { id: 'gf80s70ga09ggds90ds000s9', family: 'xbox-one', featuredImage: 'URL' } }],
    array2 = [{ node: { id: 'h83g01whiehq8haos', family: 'nintendo-switch', gameName: 'Mario Cart' } }, { node: { id: '1290qas9a0k19po1', family: 'xbox-one', gameName: 'COD WWII' } }, { node: { id: '09ga09guh3njf32olkls', family: 'xbox-one', gameName: 'GOW 2' } }, { node: { id: '09ga09guh3njf32olkls', family: 'foo', gameName: 'GOW 2' } }],
    map = new Map,
    result = array1.map(function (o) {
        map.set(o.node.family, []);
        return { family: Object.assign({}, o.node, { games: map.get(o.node.family) }) };
    });

array2.forEach(o => {
    if (!map.has(o.node.family)) {
        map.set(o.node.family, []);
        result.push({ family: { family: o.node.family, games: map.get(o.node.family) } });
    }
    map.get(o.node.family).push(o.node);
});

console.log(result);```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }</code>```

Body:
I have this situation:

const array1 = [
  {
    node:
    {
      id: '789dg020hgn20ijahq',
      family: 'ps4',
      featuredImage: 'URL'
    }
  },
  {
    node:
     {
       id: 'gf80s70ga09ggds90ds000s9',
       family: 'xbox-one',
       featuredImage: 'URL'
     }
   }
]

const array2 = [
  {
    node:
      {
      id: 'h83g01whiehq8haos',
      family: 'nintendo-switch',
      gameName: 'Mario Cart'
    }
  },
  {
    node:
      {
      id: '1290qas9a0k19po1',
      family: 'xbox-one',
      gameName: 'COD WWII'
      }
    },
    {
      node:
        {
        id: '09ga09guh3njf32olkls',
        family: 'xbox-one',
        gameName: 'GOW 2'
        }
      }
]

What I am trying to achieve is to group these arrays by family key and have this new array:

*output I want*
[{
  family: {
    id: 'gf80s70ga09ggds90ds000s9',
    family: 'xbox-one',
    featuredImage: 'URL',

    games: [
      0: {
        id: '1290qas9a0k19po1',
        family: 'xbox-one',
        gameName: 'COD WWII'
      },
      1: {
        id: '09ga09guh3njf32olkls',
        family: 'xbox-one',
        gameName: 'GOW 2'
      }
    ]
  },

  family: {
    id: 'h83g01whiehq8haos',
    family: 'nintendo-switch',
    featuredImage: 'URL',

    games: []
  }
]

*if the family of the array1 is not matched in array2*
family: {
   id: '789dg020hgn20ijahq',
   family: 'ps4',
   featuredImage: 'URL',

   games: []
}

Using this code, I grouped the arrays by family, but I missed some informations. How can I modify the following code to have the desired result? Sorry but I am a little bit stuck....

const testing2 = result[1].map((obj) => { // result[1] comes from a Promise
    return obj.node
})
_.mapValues(_.groupBy(testing2, 'family'), (list) => list.map((o) => _.omit(o, 'family')))

Body:
You could take a Map and group by family.

var array1 = [{ node: { id: '789dg020hgn20ijahq', family: 'nintendo-switch', featuredImage: 'URL' } }, { node: { id: 'gf80s70ga09ggds90ds000s9', family: 'xbox-one', featuredImage: 'URL' } }],
    array2 = [{ node: { id: 'h83g01whiehq8haos', family: 'nintendo-switch', gameName: 'Mario Cart' } }, { node: { id: '1290qas9a0k19po1', family: 'xbox-one', gameName: 'COD WWII' } }, { node: { id: '09ga09guh3njf32olkls', family: 'xbox-one', gameName: 'GOW 2' } }, { node: { id: '09ga09guh3njf32olkls', family: 'foo', gameName: 'GOW 2' } }],
    map = new Map,
    result = array1.map(function (o) {
        map.set(o.node.family, []);
        return { family: Object.assign({}, o.node, { games: map.get(o.node.family) }) };
    });

array2.forEach(o => {
    if (!map.has(o.node.family)) {
        map.set(o.node.family, []);
        result.push({ family: { family: o.node.family, games: map.get(o.node.family) } });
    }
    map.get(o.node.family).push(o.node);
});

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }