问:
How to use spark Java API to read binary file stream from HDFS?
I am writing a component which needs to get the new binary file in a specific HDFS path, so that I can do some online learning based on this data. So, I want to read binary file created by Flume from HDFS in stream. I found several functions provided by spark API, such as

```<code>public JavaDStream<byte[]> binaryRecordsStream(String directory,int recordLength)
</code>```

and

```<code>public <K,V,F extends org.apache.hadoop.mapreduce.InputFormat<K,V>>
     JavaPairInputDStream<K,V> fileStream(String directory, Class<K> kClass, Class<V> vClass, Class<F> fClass)
</code>```

But, I really do not know how to use these functions. I have tried binaryRecordStream, but it defines the specific length of file, so it is not good.

For fileStream function, I have used:

```<code>SparkConf sparkConf = new   SparkConf().setAppName("SparkFileStreamTest").setMaster("local[2]");

// Create the context with the specified batch size
JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(durationInMillis));

JavaPairInputDStream<LongWritable, BytesWritable> inputDStream = jssc.fileStream(hdfsPath, LongWritable.class, BytesWritable.class, CustomInputFormat.class);

//**********************************************************************
JavaPairInputDStream<LongWritable, BytesWritable> inputDStream = jssc.fileStream(
            hdfsPath, LongWritable.class, BytesWritable.class, CustomInputFormat.class);

JavaDStream<byte[]> content = inputDStream.map(new Function<Tuple2<LongWritable, BytesWritable>, byte[]>() {
    @Override
    public byte[] call(Tuple2<LongWritable, BytesWritable> tuple2) {
        System.out.println("----------------[testReadFileStreamFromHDFS] ENTER ......");
        if (tuple2 == null) {
            System.out.println("----------------[testReadFileStreamFromHDFS] TUPLE = NULL");
            System.out.println("----------------[testReadFileStreamFromHDFS] END.");
            return null;
        }
        else {
            System.out.println("----------------[testReadFileStreamFromHDFS] KEY = [" + tuple2._1().toString() + "]");
            System.out.println("----------------[testReadFileStreamFromHDFS] VAL-LENGTH = [" + tuple2._2().getBytes().length + "]");
            System.out.println("----------------[testReadFileStreamFromHDFS] END.");
            return tuple2._2().getBytes();
        }
    }
});

/***********************************************************************/
if (content == null) {
        System.out.println("----------------[testReadFileStreamFromHDFS] CONTENT = NULL");
}
else {
    System.out.println("----------------[testReadFileStreamFromHDFS] CONTENT-length = [" + content.count());
    content.print();
}

System.out.println("----------------[testReadFileStreamFromHDFS] END-111.");

jssc.start();
jssc.awaitTermination();
System.out.println("----------------[testReadFileStreamFromHDFS] END-222.");
</code>```

For CustomInputFormat, I created

```<code>public class CustomInputFormat extends FileInputFormat<LongWritable, BytesWritable> {

private CustomInputSplit mInputSplit;

public CustomInputFormat() {
    mInputSplit = new CustomInputSplit();
}

@Override
public List<InputSplit> getSplits(JobContext context)
        throws IOException {

    System.out.println("----------------[CustomInputFormat] 1111 ......");
    final ArrayList<InputSplit> result = new ArrayList<InputSplit>();
    result.add(mInputSplit);

    System.out.println("----------------[CustomInputFormat] 2222 ......");
    return result;
}

@Override
public RecordReader<LongWritable, BytesWritable> createRecordReader(
        InputSplit inputSplit, TaskAttemptContext taskAttemptContext)
        throws IOException, InterruptedException {

    System.out.println("----------------[CustomInputFormat] 3333 ......");
    System.out.println("----------------[CustomInputFormat] ENTER createRecordReader, inputSplit-length = ["
            + inputSplit.getLength() + "]");

    mInputSplit.init(inputSplit);

    System.out.println("----------------[CustomInputFormat] 4444 ......");
    return new CustomRecordReader();
}

@Override
protected boolean isSplitable(JobContext context, Path filename) {
    System.out.println("----------------[CustomInputFormat] 5555 ......");
    return false;
}

public class CustomRecordReader extends RecordReader<LongWritable, BytesWritable> {

private BytesWritable mValues;
private int mCursor;

public CustomRecordReader() {
    System.out.println("----------------[CustomRecordReader] 1111 ......");
    mValues = null;
    mCursor = 0;
    System.out.println("----------------[CustomRecordReader] 2222 ......");
}

@Override
public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext)
        throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 3333 ......");
    CustomInputSplit customInputSplit = (CustomInputSplit) inputSplit;
    mValues = customInputSplit.getValues();
    System.out.println("----------------[CustomRecordReader] 4444 ......");
}

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 5555 ......");
    boolean existNext = (mCursor == 0);
    mCursor++;
    System.out.println("----------------[CustomRecordReader] 6666 ......");
    return existNext;
}

@Override
public LongWritable getCurrentKey() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 7777 ......");
    return new LongWritable(0);
}

@Override
public BytesWritable getCurrentValue() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 8888 ......");
    return mValues;
}

@Override
public float getProgress() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 9999 ......");
    return 0;
}

@Override
public void close() throws IOException {
    System.out.println("----------------[CustomRecordReader] AAAA ......");
    mValues = null;
}
}

public class CustomInputSplit extends InputSplit implements Writable {

private long mLength;
private String[] mLocations;

private final BytesWritable mContent;

public CustomInputSplit() {
    System.out.println("----------------[CustomInputSplit] 1111 ......");
    mLength = 0;
    mLocations = null;

    mContent = new BytesWritable();
    System.out.println("----------------[CustomInputSplit] 2222 ......");
}

public void init(InputSplit inputSplit) throws IOException, InterruptedException {
    System.out.println("----------------[CustomInputSplit] 3333 ......");
    mLength = inputSplit.getLength();

    String[] locations = inputSplit.getLocations();
    if (locations != null) {
        int numLocations = locations.length;
        mLocations = new String[numLocations];
        for (int i = 0; i < numLocations; i++) {
            mLocations[i] = locations[i];
        }
    }
    System.out.println("----------------[CustomInputSplit] 4444 ......");
}

@Override
public long getLength() throws IOException, InterruptedException {
    System.out.println("----------------[CustomInputSplit] 5555 ......");
    return mLength;
}

@Override
public String[] getLocations() throws IOException, InterruptedException {
    if (mLocations == null) {
        System.out.println("----------------[CustomInputSplit] 6666-0001 ...... mLocations = [NULL]");
        mLocations = new String[] {"localhost"};
    }
    System.out.println("----------------[CustomInputSplit] 6666-0002 ...... mLocations-length = [" + mLocations.length + "]");
    return mLocations;
}

@Override
public void write(DataOutput dataOutput) throws IOException {
    System.out.println("----------------[CustomInputSplit] 7777 ......");
    mContent.write(dataOutput);
}

@Override
public void readFields(DataInput dataInput) throws IOException {
    System.out.println("----------------[CustomInputSplit] 8888 ......");
    mContent.readFields(dataInput);
}

public BytesWritable getValues() {
    System.out.println("----------------[CustomInputSplit] 9999 ......");
    return mContent;
}
}
</code>```

But when I print:

System.out.println("----------------[testReadFileStreamFromHDFS] VAL-LENGTH = [" + tuple2._2().getBytes().length + "]");

I always get 0 length:

----------------[testReadFileStreamFromHDFS] VAL-LENGTH = [0]

Are there some problems with CustomerInputFormat.class? Does anybody know how to use Spark stream Java API to read binary file from HDFS?

答:
Use this Approach:
Write a Custom Receiver:

```<code>import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.DatatypeConverter;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.input.PortableDataStream;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairInputDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.receiver.Receiver;
class DFSReceiver extends Receiver<byte[]> {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -1051061769769056605L;
    Long windowSize = 20000l;

    /** Instantiates a new RMQ receiver. */
    DFSReceiver() {
        super(StorageLevel.MEMORY_AND_DISK_SER_2());
    }

    @Override
    public void onStart() {
        System.out.println("Inside onStart method");
        new Thread() {
            @Override
            public void run() {

                try {
                    receive();
                }
                } catch (Exception e) {
                    e.printStackTrace();
                    LOGGER.error("Exception raised at DFSReceiverHelper , exception : " + e);
                }

            }
        }.start();
    }

    /** Receive.
     *
     * @throws Exception
     *             the exception */
    protected void receive() throws Exception {
        try {
            ConnectionMetadata connectionMetadata = ConnectionMetadataFactory.getConnectionMetadataObj(ConnectionConstants.HDFS_DATA_STORE);
            String connectionId = connectionMetadata.getConnectionId(ConnectionConstants.HDFS_DATA_STORE, connectionName);
            ConnectionMetaDataDTO c = connectionMetadata.getConnectionMetaDataById(connectionId);

            Map<String, Object> map = connectionMetadata.getConnectionConfigParameters(c);
            FileSystem fs = HDFSUtils.getFileSystemInstance(map);

            JavaPairRDD<String, PortableDataStream> rdd = sparkContext.binaryFiles(fsURI + directoryPath);
            List<Tuple2<String, PortableDataStream>> rddList = rdd.collect();
            for (Tuple2<String, PortableDataStream> arg0 : rddList) {
                byte[] imageInByte = arg0._2().toArray();
                String base64Encoded = DatatypeConverter.printBase64Binary(imageInByte);
                store((arg0._1 + Constants.COMMA_DELIMITER + base64Encoded).getBytes());
            }

            Long time = System.currentTimeMillis();
            System.out.println();
            Thread.currentThread().sleep(windowSize);
            while (true) {
                List<Path> newFiles = checkIfNewFileCreated(fs, new Path(fsURI + directoryPath), time);
                for (Path p : newFiles) {
                    JavaPairRDD<String, PortableDataStream> rdd11 = sparkContext.binaryFiles(p.toString());
                    Tuple2<String, PortableDataStream> arg0 = rdd11.first();
                    byte[] imageInByte = arg0._2().toArray();
                    String base64Encoded = DatatypeConverter.printBase64Binary(imageInByte);
                    store((arg0._1 + Constants.COMMA_DELIMITER + base64Encoded).getBytes());
                }
                Thread.currentThread().sleep(windowSize);
                time += windowSize;
            }
        } catch (ShutdownSignalException s) {
            LOGGER.error("ShutdownSignalException raised in receive method of DFSReceiver", s);
        }
    }

    private List<Path> checkIfNewFileCreated(FileSystem fs, Path p, Long timeStamp) throws IOException {
        List<Path> fileList = new ArrayList<>();
        if (fs.isDirectory(p)) {
            FileStatus[] fStatus = fs.listStatus(p);
            for (FileStatus status : fStatus) {
                if (status.isFile() && timeStamp < status.getModificationTime() && timeStamp + windowSize >= status.getModificationTime()) {
                    fileList.add(status.getPath());
                }
            }
        }
        return fileList;
    }

    @Override
    public void onStop() {

    }
}
</code>```

With this receiver you will be able to read newly created files also every 20 second.

Body:
I am writing a component which needs to get the new binary file in a specific HDFS path, so that I can do some online learning based on this data. So, I want to read binary file created by Flume from HDFS in stream. I found several functions provided by spark API, such as

public JavaDStream<byte[]> binaryRecordsStream(String directory,int recordLength)

and

public <K,V,F extends org.apache.hadoop.mapreduce.InputFormat<K,V>>
     JavaPairInputDStream<K,V> fileStream(String directory, Class<K> kClass, Class<V> vClass, Class<F> fClass)

But, I really do not know how to use these functions. I have tried binaryRecordStream, but it defines the specific length of file, so it is not good.

For fileStream function, I have used:

SparkConf sparkConf = new   SparkConf().setAppName("SparkFileStreamTest").setMaster("local[2]");

// Create the context with the specified batch size
JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(durationInMillis));

JavaPairInputDStream<LongWritable, BytesWritable> inputDStream = jssc.fileStream(hdfsPath, LongWritable.class, BytesWritable.class, CustomInputFormat.class);

//**********************************************************************
JavaPairInputDStream<LongWritable, BytesWritable> inputDStream = jssc.fileStream(
            hdfsPath, LongWritable.class, BytesWritable.class, CustomInputFormat.class);

JavaDStream<byte[]> content = inputDStream.map(new Function<Tuple2<LongWritable, BytesWritable>, byte[]>() {
    @Override
    public byte[] call(Tuple2<LongWritable, BytesWritable> tuple2) {
        System.out.println("----------------[testReadFileStreamFromHDFS] ENTER ......");
        if (tuple2 == null) {
            System.out.println("----------------[testReadFileStreamFromHDFS] TUPLE = NULL");
            System.out.println("----------------[testReadFileStreamFromHDFS] END.");
            return null;
        }
        else {
            System.out.println("----------------[testReadFileStreamFromHDFS] KEY = [" + tuple2._1().toString() + "]");
            System.out.println("----------------[testReadFileStreamFromHDFS] VAL-LENGTH = [" + tuple2._2().getBytes().length + "]");
            System.out.println("----------------[testReadFileStreamFromHDFS] END.");
            return tuple2._2().getBytes();
        }
    }
});

/***********************************************************************/
if (content == null) {
        System.out.println("----------------[testReadFileStreamFromHDFS] CONTENT = NULL");
}
else {
    System.out.println("----------------[testReadFileStreamFromHDFS] CONTENT-length = [" + content.count());
    content.print();
}

System.out.println("----------------[testReadFileStreamFromHDFS] END-111.");

jssc.start();
jssc.awaitTermination();
System.out.println("----------------[testReadFileStreamFromHDFS] END-222.");

For CustomInputFormat, I created

public class CustomInputFormat extends FileInputFormat<LongWritable, BytesWritable> {

private CustomInputSplit mInputSplit;

public CustomInputFormat() {
    mInputSplit = new CustomInputSplit();
}

@Override
public List<InputSplit> getSplits(JobContext context)
        throws IOException {

    System.out.println("----------------[CustomInputFormat] 1111 ......");
    final ArrayList<InputSplit> result = new ArrayList<InputSplit>();
    result.add(mInputSplit);

    System.out.println("----------------[CustomInputFormat] 2222 ......");
    return result;
}

@Override
public RecordReader<LongWritable, BytesWritable> createRecordReader(
        InputSplit inputSplit, TaskAttemptContext taskAttemptContext)
        throws IOException, InterruptedException {

    System.out.println("----------------[CustomInputFormat] 3333 ......");
    System.out.println("----------------[CustomInputFormat] ENTER createRecordReader, inputSplit-length = ["
            + inputSplit.getLength() + "]");

    mInputSplit.init(inputSplit);

    System.out.println("----------------[CustomInputFormat] 4444 ......");
    return new CustomRecordReader();
}

@Override
protected boolean isSplitable(JobContext context, Path filename) {
    System.out.println("----------------[CustomInputFormat] 5555 ......");
    return false;
}

public class CustomRecordReader extends RecordReader<LongWritable, BytesWritable> {

private BytesWritable mValues;
private int mCursor;

public CustomRecordReader() {
    System.out.println("----------------[CustomRecordReader] 1111 ......");
    mValues = null;
    mCursor = 0;
    System.out.println("----------------[CustomRecordReader] 2222 ......");
}

@Override
public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext)
        throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 3333 ......");
    CustomInputSplit customInputSplit = (CustomInputSplit) inputSplit;
    mValues = customInputSplit.getValues();
    System.out.println("----------------[CustomRecordReader] 4444 ......");
}

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 5555 ......");
    boolean existNext = (mCursor == 0);
    mCursor++;
    System.out.println("----------------[CustomRecordReader] 6666 ......");
    return existNext;
}

@Override
public LongWritable getCurrentKey() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 7777 ......");
    return new LongWritable(0);
}

@Override
public BytesWritable getCurrentValue() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 8888 ......");
    return mValues;
}

@Override
public float getProgress() throws IOException, InterruptedException {
    System.out.println("----------------[CustomRecordReader] 9999 ......");
    return 0;
}

@Override
public void close() throws IOException {
    System.out.println("----------------[CustomRecordReader] AAAA ......");
    mValues = null;
}
}

public class CustomInputSplit extends InputSplit implements Writable {

private long mLength;
private String[] mLocations;

private final BytesWritable mContent;

public CustomInputSplit() {
    System.out.println("----------------[CustomInputSplit] 1111 ......");
    mLength = 0;
    mLocations = null;

    mContent = new BytesWritable();
    System.out.println("----------------[CustomInputSplit] 2222 ......");
}

public void init(InputSplit inputSplit) throws IOException, InterruptedException {
    System.out.println("----------------[CustomInputSplit] 3333 ......");
    mLength = inputSplit.getLength();

    String[] locations = inputSplit.getLocations();
    if (locations != null) {
        int numLocations = locations.length;
        mLocations = new String[numLocations];
        for (int i = 0; i < numLocations; i++) {
            mLocations[i] = locations[i];
        }
    }
    System.out.println("----------------[CustomInputSplit] 4444 ......");
}

@Override
public long getLength() throws IOException, InterruptedException {
    System.out.println("----------------[CustomInputSplit] 5555 ......");
    return mLength;
}

@Override
public String[] getLocations() throws IOException, InterruptedException {
    if (mLocations == null) {
        System.out.println("----------------[CustomInputSplit] 6666-0001 ...... mLocations = [NULL]");
        mLocations = new String[] {"localhost"};
    }
    System.out.println("----------------[CustomInputSplit] 6666-0002 ...... mLocations-length = [" + mLocations.length + "]");
    return mLocations;
}

@Override
public void write(DataOutput dataOutput) throws IOException {
    System.out.println("----------------[CustomInputSplit] 7777 ......");
    mContent.write(dataOutput);
}

@Override
public void readFields(DataInput dataInput) throws IOException {
    System.out.println("----------------[CustomInputSplit] 8888 ......");
    mContent.readFields(dataInput);
}

public BytesWritable getValues() {
    System.out.println("----------------[CustomInputSplit] 9999 ......");
    return mContent;
}
}

But when I print:

System.out.println("----------------[testReadFileStreamFromHDFS] VAL-LENGTH = [" + tuple2._2().getBytes().length + "]");

I always get 0 length:

----------------[testReadFileStreamFromHDFS] VAL-LENGTH = [0]

Are there some problems with CustomerInputFormat.class? Does anybody know how to use Spark stream Java API to read binary file from HDFS?

Body:
Use this Approach:
Write a Custom Receiver:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.DatatypeConverter;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.input.PortableDataStream;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairInputDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.receiver.Receiver;
class DFSReceiver extends Receiver<byte[]> {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -1051061769769056605L;
    Long windowSize = 20000l;

    /** Instantiates a new RMQ receiver. */
    DFSReceiver() {
        super(StorageLevel.MEMORY_AND_DISK_SER_2());
    }

    @Override
    public void onStart() {
        System.out.println("Inside onStart method");
        new Thread() {
            @Override
            public void run() {

                try {
                    receive();
                }
                } catch (Exception e) {
                    e.printStackTrace();
                    LOGGER.error("Exception raised at DFSReceiverHelper , exception : " + e);
                }

            }
        }.start();
    }

    /** Receive.
     *
     * @throws Exception
     *             the exception */
    protected void receive() throws Exception {
        try {
            ConnectionMetadata connectionMetadata = ConnectionMetadataFactory.getConnectionMetadataObj(ConnectionConstants.HDFS_DATA_STORE);
            String connectionId = connectionMetadata.getConnectionId(ConnectionConstants.HDFS_DATA_STORE, connectionName);
            ConnectionMetaDataDTO c = connectionMetadata.getConnectionMetaDataById(connectionId);

            Map<String, Object> map = connectionMetadata.getConnectionConfigParameters(c);
            FileSystem fs = HDFSUtils.getFileSystemInstance(map);

            JavaPairRDD<String, PortableDataStream> rdd = sparkContext.binaryFiles(fsURI + directoryPath);
            List<Tuple2<String, PortableDataStream>> rddList = rdd.collect();
            for (Tuple2<String, PortableDataStream> arg0 : rddList) {
                byte[] imageInByte = arg0._2().toArray();
                String base64Encoded = DatatypeConverter.printBase64Binary(imageInByte);
                store((arg0._1 + Constants.COMMA_DELIMITER + base64Encoded).getBytes());
            }

            Long time = System.currentTimeMillis();
            System.out.println();
            Thread.currentThread().sleep(windowSize);
            while (true) {
                List<Path> newFiles = checkIfNewFileCreated(fs, new Path(fsURI + directoryPath), time);
                for (Path p : newFiles) {
                    JavaPairRDD<String, PortableDataStream> rdd11 = sparkContext.binaryFiles(p.toString());
                    Tuple2<String, PortableDataStream> arg0 = rdd11.first();
                    byte[] imageInByte = arg0._2().toArray();
                    String base64Encoded = DatatypeConverter.printBase64Binary(imageInByte);
                    store((arg0._1 + Constants.COMMA_DELIMITER + base64Encoded).getBytes());
                }
                Thread.currentThread().sleep(windowSize);
                time += windowSize;
            }
        } catch (ShutdownSignalException s) {
            LOGGER.error("ShutdownSignalException raised in receive method of DFSReceiver", s);
        }
    }

    private List<Path> checkIfNewFileCreated(FileSystem fs, Path p, Long timeStamp) throws IOException {
        List<Path> fileList = new ArrayList<>();
        if (fs.isDirectory(p)) {
            FileStatus[] fStatus = fs.listStatus(p);
            for (FileStatus status : fStatus) {
                if (status.isFile() && timeStamp < status.getModificationTime() && timeStamp + windowSize >= status.getModificationTime()) {
                    fileList.add(status.getPath());
                }
            }
        }
        return fileList;
    }

    @Override
    public void onStop() {

    }
}

With this receiver you will be able to read newly created files also every 20 second.


来源: stackexchange

问:
jQuery custom validation: custom phone number format
I have a form in which you have to input your phone number and other fields.

I'm validating the form with jQuery Validate.

To validate the phone number field I do:

```<code>rules: {
    phone: {
    required: true,
        minlength: 9,
        number:true
    },..
</code>```

But I need to check also that phone starts with 6.

How can I do that?

答:
I think you will need to add a custom validator.</p>

```<code>$.validator.addMethod("phoneNumber", function(uid, element) {
    return (this.optional(element) || uid.match(phone number regex));
}
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have a form in which you have to input your phone number and other fields.

I'm validating the form with jQuery Validate.

To validate the phone number field I do:

rules: {
    phone: {
    required: true,
        minlength: 9,
        number:true
    },..

But I need to check also that phone starts with 6.

How can I do that?

Body:
I think you will need to add a custom validator.

$.validator.addMethod("phoneNumber", function(uid, element) {
    return (this.optional(element) || uid.match(phone number regex));
}

ContentLicense: CC BY-SA 2.5


问:
How can I install a recent version of GDAL on Amazon Linux?
I'd like to install GDAL on an EC2 instance running Amazon Linux (which I think is based on RHEL 6). I'd like to avoid compiling from source if possible.

The version of GDAL included in the EPEL Yum repository is too old for my purposes (gdal-1.7.3-15.el6.x86_64). EPEL 7 includes gdal-1.11.4-1.el7.x86_64 which would be perfect. Is there any way I could use this repo on Amazon Linux?

So far I've also tried:

Adding GDAL from the ELGIS 6 repo (which has version 1.9.2). However this failed to install – as found / by others. The ELGIS Wiki advises people to use EPEL now anyway.

Downloading and installing the more recent GDAL RPM from EPEL 7, but it fails due to mismatches between GDAL's dependencies and the available packages in my enabled repos.

I'm not at all experienced with Amazon Linux (or Yum) so any hints much appreciated.

答:
This worked for me.

```<code>sudo yum -y update
sudo yum-config-manager --enable epel
sudo yum -y install make automake gcc gcc-c++ libcurl-devel proj-devel geos-devel
cd /tmp
curl -L http://download.osgeo.org/gdal/2.0.0/gdal-2.0.0.tar.gz | tar zxf -
cd gdal-2.0.0/
./configure --prefix=/usr/local --without-python
make -j4
sudo make install
cd /usr/local
tar zcvf ~/gdal-2.0.0-amz1.tar.gz *
</code>```

From https://gist.github.com/mojodna/2f596ca2fca48f08438e

Body:
This worked for me.

sudo yum -y update
sudo yum-config-manager --enable epel
sudo yum -y install make automake gcc gcc-c++ libcurl-devel proj-devel geos-devel
cd /tmp
curl -L http://download.osgeo.org/gdal/2.0.0/gdal-2.0.0.tar.gz | tar zxf -
cd gdal-2.0.0/
./configure --prefix=/usr/local --without-python
make -j4
sudo make install
cd /usr/local
tar zcvf ~/gdal-2.0.0-amz1.tar.gz *

From https://gist.github.com/mojodna/2f596ca2fca48f08438e

ContentLicense: CC BY-SA 3.0


问:
Why @Inject not work after import as a jar file to another project?
I have a Maven base Java EE project: core-project.
In this project, I use @Inject and all things work as expected.
But after importing this project as a dependency to another project, parent-project, the injected fields are null.
Why, after importing a jar file to other project, the injection does not work?

答:
If your fields are empty this means that matching beans can not be resolved.
If they are in another package you need a file beans.xml (which can be empty).

From The Java EE 6 Tutorial:

Configuring a CDI Application

An application that uses CDI must have a file named beans.xml. The
  file can be completely empty (it has content only in certain limited
  situations), but it must be present. For a web application, the
  beans.xml file must be in the WEB-INF directory. For EJB modules or
  JAR files, the beans.xml file must be in the META-INF directory.

So make sure that there is a beans.xml in your core-projectand in  parent-project. For Maven:

from: https://stackoverflow.com/a/13057183/1838233

For EJB and JAR packaging you should place the beans.xml in
  src/main/resources/META-INF/. For WAR packaging you should place the
  beans.xml in src/main/webapp/WEB-INF/

Another reason could be that your Bean is annotated with @Alternative or @Specializes. In that case you need to specifiy the bean as alternative in the beans.xml:

```<code><beans ... >
    <alternatives>
        <class>sample.package.YourAlternativeBean</class>
    </alternatives>
</beans>
</code>```

See http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html for more information on that.

Body:
If your fields are empty this means that matching beans can not be resolved.
If they are in another package you need a file beans.xml (which can be empty).

From The Java EE 6 Tutorial:

Configuring a CDI Application

An application that uses CDI must have a file named beans.xml. The
  file can be completely empty (it has content only in certain limited
  situations), but it must be present. For a web application, the
  beans.xml file must be in the WEB-INF directory. For EJB modules or
  JAR files, the beans.xml file must be in the META-INF directory.

So make sure that there is a beans.xml in your core-projectand in  parent-project. For Maven:

from: https://stackoverflow.com/a/13057183/1838233

For EJB and JAR packaging you should place the beans.xml in
  src/main/resources/META-INF/. For WAR packaging you should place the
  beans.xml in src/main/webapp/WEB-INF/

Another reason could be that your Bean is annotated with @Alternative or @Specializes. In that case you need to specifiy the bean as alternative in the beans.xml:

<beans ... >
    <alternatives>
        <class>sample.package.YourAlternativeBean</class>
    </alternatives>
</beans>

See http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html for more information on that.


问:
wasd change in aframe
Hi I am new to aframe trying to create buttons on the screen (like the full-screen VR mode) to mimic wasd controls.

I tried info on this  Link it gave me some hint regarding this but did not solve my query.

If you guys have any idea please let me know.

and also I tried broadcasting keyboard events they did not help either.

here is the camera code i tried this but nothing happened

-Thanks

答:
If You want to move the camera when clicking at a button, just make a listener in a component:

```<code>AFRAME.registerComponent('click-handler',{
init:function(){
   let cam = document.querySelector('[camera]');
   this.el.addEventListener('click',function(e){
         let pos = cam.getAttribute('position');
         pos.z++;
         cam.setAttribute('position',pos);
   });
 });
</code>```

which moves the camera forward on click.
If You want to make it smooth like in the wasd controls, with 'friction', and some kind od speed, You need to make it in the tick(), where You define Your speed as if(speed>0)speed=acc+speed-friction, and make a acc peek once You click the button. You can increase the acc while holding the button, so the movement will be even smoother.

I found the universal-controls useful, when You move using a vive/other controller, not by looking at some entities.

Title: wasd change in aframe

Body:
If You want to move the camera when clicking at a button, just make a listener in a component:

AFRAME.registerComponent('click-handler',{
init:function(){
   let cam = document.querySelector('[camera]');
   this.el.addEventListener('click',function(e){
         let pos = cam.getAttribute('position');
         pos.z++;
         cam.setAttribute('position',pos);
   });
 });

which moves the camera forward on click.
If You want to make it smooth like in the wasd controls, with 'friction', and some kind od speed, You need to make it in the tick(), where You define Your speed as if(speed>0)speed=acc+speed-friction, and make a acc peek once You click the button. You can increase the acc while holding the button, so the movement will be even smoother.

I found the universal-controls useful, when You move using a vive/other controller, not by looking at some entities.


问:
Swapping nodes in linked list
I tried to make function(swapNodes) for swapping nodes in linked list.
Here i stored previous and next addresses of nodes which are to be swapped.
But my code stucked in infinite loop.

Can this code be made as working one or it is a wrong approach?

```<code>#include<stdio.h>
#include<stdlib.h>
 struct Node
{
    int data;
    struct Node *next;
};
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node =
        (struct Node*) malloc(sizeof(struct Node));

    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}

void printList(struct Node *node)
{
    while(node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
}

void swapNodes(struct Node** headr,int key1,int key2)
{
    struct Node* temp1 = *headr;
    struct Node* temp2 = *headr;

    if(key1 == key2)
    return;

    struct Node* prev1 =NULL;
    struct Node* next1 =temp1;
    while(temp1->data !=key1 && next1 !=NULL)
    {
    prev1 =temp1;
    temp1 =temp1->next;
    next1 =temp1->next;
    }
    struct Node* prev2 =NULL;
    struct Node* next2 =temp2;
    while(temp2->data !=key2 && next2 !=NULL)
    {
    prev2 =temp2;
    temp2 =temp2->next;
    next2 =temp2->next;
    }
    if(next1 == NULL||next2 == NULL)
    return;

    prev1->next =temp2;
    temp2->next =next1;
    prev2->next =temp1;
    temp1->next =next2;
}
int main()
{
    struct Node *start = NULL;

    push(&start, 7);
    push(&start, 6);
    push(&start, 5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1);

    printf("\n Linked list before calling swapNodes() ");
    printList(start);

    swapNodes(&start, 4, 3);

    printf("\n Linked list after calling swapNodes() ");
    printList(start);

    return 0;
}
</code>```

答:
You should rewrite you swapNodes function a bit:

```<code>void swapNodes(struct Node** headr, int key1, int key2)
{
    struct Node* temp1 = *headr;
    struct Node* temp2 = *headr;

    if(key1==key2)
        return;

    struct Node* prev1=NULL;
    while(temp1 && temp1->data!=key1)
    {
        prev1=temp1;
        temp1=temp1->next;
    }
    struct Node* prev2=NULL;
    while(temp2 && temp2->data!=key2)
    {
        prev2=temp2;
        temp2=temp2->next;
    }

    if(temp1==NULL || temp1==NULL)
        return;

    // temp1 is a head
    if (prev1 == NULL) {
        *headr = temp2;
    } else {
        prev1->next = temp2;
    }

    // temp2 is a head
    if (prev2 == NULL) {
        *headr = temp1;
    } else {
        prev2->next = temp1;
    }

    struct Node *buff = temp2->next;
    temp2->next = temp1->next;
    temp1->next = buff;
}
</code>```

As you can see you don't need next1 and next2 pointers. But you must check if temp1 or temp2 is a head: it is a special case when you need to replace head with another node. The rest is trivial - just swap nodes via buffer node.

Title: Swapping nodes in linked list

Body:
I tried to make function(swapNodes) for swapping nodes in linked list.
Here i stored previous and next addresses of nodes which are to be swapped.
But my code stucked in infinite loop.

Can this code be made as working one or it is a wrong approach?

#include<stdio.h>
#include<stdlib.h>
 struct Node
{
    int data;
    struct Node *next;
};
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node =
        (struct Node*) malloc(sizeof(struct Node));

    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}

void printList(struct Node *node)
{
    while(node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
}

void swapNodes(struct Node** headr,int key1,int key2)
{
    struct Node* temp1 = *headr;
    struct Node* temp2 = *headr;

    if(key1 == key2)
    return;

    struct Node* prev1 =NULL;
    struct Node* next1 =temp1;
    while(temp1->data !=key1 && next1 !=NULL)
    {
    prev1 =temp1;
    temp1 =temp1->next;
    next1 =temp1->next;
    }
    struct Node* prev2 =NULL;
    struct Node* next2 =temp2;
    while(temp2->data !=key2 && next2 !=NULL)
    {
    prev2 =temp2;
    temp2 =temp2->next;
    next2 =temp2->next;
    }
    if(next1 == NULL||next2 == NULL)
    return;

    prev1->next =temp2;
    temp2->next =next1;
    prev2->next =temp1;
    temp1->next =next2;
}
int main()
{
    struct Node *start = NULL;

    push(&start, 7);
    push(&start, 6);
    push(&start, 5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1);

    printf("\n Linked list before calling swapNodes() ");
    printList(start);

    swapNodes(&start, 4, 3);

    printf("\n Linked list after calling swapNodes() ");
    printList(start);

    return 0;
}

Body:
You should rewrite you swapNodes function a bit:

void swapNodes(struct Node** headr, int key1, int key2)
{
    struct Node* temp1 = *headr;
    struct Node* temp2 = *headr;

    if(key1==key2)
        return;

    struct Node* prev1=NULL;
    while(temp1 && temp1->data!=key1)
    {
        prev1=temp1;
        temp1=temp1->next;
    }
    struct Node* prev2=NULL;
    while(temp2 && temp2->data!=key2)
    {
        prev2=temp2;
        temp2=temp2->next;
    }

    if(temp1==NULL || temp1==NULL)
        return;

    // temp1 is a head
    if (prev1 == NULL) {
        *headr = temp2;
    } else {
        prev1->next = temp2;
    }

    // temp2 is a head
    if (prev2 == NULL) {
        *headr = temp1;
    } else {
        prev2->next = temp1;
    }

    struct Node *buff = temp2->next;
    temp2->next = temp1->next;
    temp1->next = buff;
}

As you can see you don't need next1 and next2 pointers. But you must check if temp1 or temp2 is a head: it is a special case when you need to replace head with another node. The rest is trivial - just swap nodes via buffer node.


问:
What could cause a undefinedpageWebRequestEvents.js appearing in my logs?
We're seeing this extremely rare, extremely odd bug in our logs.  When some people (I believe most are on Safari) are trying to access various pages on our site, they get an error and can't proceed.  When I check the logs, I see URLs with undefinedpageWebRequestEvents.js in them.

I've searched around but can't seem to find any explanation of what this might be or what might be causing them.  There is VERY little javascript on this site, and hundreds of thousands of others are not seeing this error (including us - we cannot duplicate) so any advice is appreciated.

答:
It looks like this could be related to an infected lastpass_x64.exe - the strings extracted from that file include reference to a "pageWebRequestEvents.js". I am in no way affiliated with hybrid-analysis.com, so I can't tell whether their analysis is legit or sane.

For what it's worth I've seen two instances of this file being requested in the last 24 hours.


问:
Wicked_pdf too few arguments in demo
I'm trying to get wicked_pdf working for the first time as a project in learning RoR, but i've run into an issue.

When I try to run any of the demo's I've read around the place using the format.pdf do command, I get the error "Too few arguments" with highlighting on the format.pdf do line.

This is the code:

```<code>  def show
    format.pdf do
      render :pdf => "file_name", :template => 'certificates/show.pdf.erb'
    end
  end
</code>```

What am I missing?

答:
I had this problem. You can add a defaults hash to the route and specify the format.

get "/pdf_name.pdf" => "your_controller#action", as: :pdf_route, defaults: { format: :pdf }

Title: Wicked_pdf too few arguments in demo

Body:
I'm trying to get wicked_pdf working for the first time as a project in learning RoR, but i've run into an issue.

When I try to run any of the demo's I've read around the place using the format.pdf do command, I get the error "Too few arguments" with highlighting on the format.pdf do line.

This is the code:

  def show
    format.pdf do
      render :pdf => "file_name", :template => 'certificates/show.pdf.erb'
    end
  end

What am I missing?


问:
Infinite Loops and Early Return Statements
I have a simple console application that outputs a menu and waits for user input.  After performing the appropriate action, the entire process repeats.  The program exits when a specific string is entered.  This is implemented with an infinite loop and an early return statement:

```<code>int main()
{
    while (true)
    {
        OutputMenu();

        string UserChoice;
        cin >> UserChoice;

        // ...

        if (UserChoice == "exit") return 0;
    }
}
</code>```

According to my teacher, it's bad practice to use an infinite loop and hack my way out of it with a return statement.  He suggests something like the following:

```<code>int main()
{
    bool ShouldExit = false;
    while (!ShouldExit)
    {
        OutputMenu();

        string UserChoice;
        cin >> UserChoice;

        // ...

        if (UserChoice == "exit") ShouldExit = true;
    }

    return 0;
}
</code>```

Is it really a bad idea to use an infinite loop and an early return statement?

If so, is there a technical reason or is it just bad practice?

答:
This might be one of those rare cases where do...while is appropriate. I avoid adding extra boolean state variables unless they genuinely make the code clearer.

```<code>int main()
{
    string UserChoice;
    do
    {
        OutputMenu();

        cin >> UserChoice;

        // ...

    } while (UserChoice != "exit");
}
</code>```

However, for a user input loop I would usually make a function that returns whether or not the input was successful. As it stands the code could easily end in an infinite loop if cin closes.

E.g.

```<code>bool GetNonExitInput( std::istream& in, std::string& s )
{
    OutputMenu();
    in >> s;

    return in.good() && s != "exit";
}

int main()
{
    std::string UserChoice;

    while (GetNonExitInput(std::cin, UserChoice))
    {
        // ...
    }
}
</code>```

Body:
I have a simple console application that outputs a menu and waits for user input.  After performing the appropriate action, the entire process repeats.  The program exits when a specific string is entered.  This is implemented with an infinite loop and an early return statement:

int main()
{
    while (true)
    {
        OutputMenu();

        string UserChoice;
        cin >> UserChoice;

        // ...

        if (UserChoice == "exit") return 0;
    }
}

According to my teacher, it's bad practice to use an infinite loop and hack my way out of it with a return statement.  He suggests something like the following:

int main()
{
    bool ShouldExit = false;
    while (!ShouldExit)
    {
        OutputMenu();

        string UserChoice;
        cin >> UserChoice;

        // ...

        if (UserChoice == "exit") ShouldExit = true;
    }

    return 0;
}

Is it really a bad idea to use an infinite loop and an early return statement?

If so, is there a technical reason or is it just bad practice?

Body:
This might be one of those rare cases where do...while is appropriate. I avoid adding extra boolean state variables unless they genuinely make the code clearer.

int main()
{
    string UserChoice;
    do
    {
        OutputMenu();

        cin >> UserChoice;

        // ...

    } while (UserChoice != "exit");
}

However, for a user input loop I would usually make a function that returns whether or not the input was successful. As it stands the code could easily end in an infinite loop if cin closes.

E.g.

bool GetNonExitInput( std::istream& in, std::string& s )
{
    OutputMenu();
    in >> s;

    return in.good() && s != "exit";
}

int main()
{
    std::string UserChoice;

    while (GetNonExitInput(std::cin, UserChoice))
    {
        // ...
    }
}


问:
How to save one Doctrine entity to two database tables (required for MySQL optimisation)
In my database I've a table file and a table file_content. The file table stores the metadata of the file such as name, mime and some more. The file_content stores a blob with the file content. I'm not storing the blob in the same table as the metadata for performance reasons only.

For a 'version 2' of my project I'm looking into Doctrine (2.3). To me, a "File" seems to be one entity, with properties such as name, mime, extension, content that should be used like this:

```<code>$file = new File();
$file->setName('hello.txt');
$file->setMime('text/plain');
$file->setContent('Hello world!')
$em->persist($file);
$em->flush();
</code>```

Is this behaviour possible? To me it makes no sense to create two entities for something that's really just one entity. I could not find anything about it in the documentation and I read in a 2-year-old topic that it isn't possible in Doctrine 2.1: Doctrine 2.1 - Map entity to multiple tables

Someone any suggestions how to handle this correctly? I'm new to Doctrine and have been playing around with it a bit to see if it's the right choice for my project. Thanks.

答:
You could map both tables, resulting in File and FileContent, and add a one-to-one relationship between the two.
Then add getter/setter methods for the file content in the File class. In these you'd have to call the corresponding methods in FileContent.

Body:
In my database I've a table file and a table file_content. The file table stores the metadata of the file such as name, mime and some more. The file_content stores a blob with the file content. I'm not storing the blob in the same table as the metadata for performance reasons only.

For a 'version 2' of my project I'm looking into Doctrine (2.3). To me, a "File" seems to be one entity, with properties such as name, mime, extension, content that should be used like this:

$file = new File();
$file->setName('hello.txt');
$file->setMime('text/plain');
$file->setContent('Hello world!')
$em->persist($file);
$em->flush();

Is this behaviour possible? To me it makes no sense to create two entities for something that's really just one entity. I could not find anything about it in the documentation and I read in a 2-year-old topic that it isn't possible in Doctrine 2.1: Doctrine 2.1 - Map entity to multiple tables

Someone any suggestions how to handle this correctly? I'm new to Doctrine and have been playing around with it a bit to see if it's the right choice for my project. Thanks.


问:
sqlsrv_num_rows() expects parameter 1 to be resource
I get a error --> sqlsrv_num_rows() expects parameter 1 to be resource

Can anyone figure out whats wrong with the coding, ive been trying for 3 hours now
Thanks in advance :)

```<code><?php
include("config.php");

 username and password sent from form
 $myusername=htmlspecialchars($_POST[username]);
 $mypassword=htmlspecialchars($_POST[password]);

 $sql="SELECT * FROM Login WHERE Username=$myusername and
  Password=$mypassword";
  $result=sqlsrv_query($conn,$sql);
  $count=sqlsrv_num_rows($result);

  if($count==1){
     header("location: something");
  }
 else {
     echo "Wrong Username or Password";

 }
 ?>
</code>```

CONFIG.PHP

```<code> <?php
   $serverName = '';
   $uid = '';
   $pwd = '';
   $databaseName = '';
   $connectionInfo = array( "UID"=>$uid,
                     "PWD"=>$pwd,
                     "Database"=>$databaseName);

   $conn = sqlsrv_connect( $serverName, $connectionInfo);
      if( $conn ) {
            echo "Connection established.<br />";
       }else{
              echo "Connection could not be established.<br />";

        }
        ?>
</code>```

答:
I have notice 2 things here:

1) You have a typo at line 4, I think you missed the comment markup

instead of:

```<code>username and password sent from form
</code>```

you mean:

```<code>// username and password sent from form
</code>```

2) Mayank already anwsered that second issue. You have another typo at SQL Query and the $result variable is returning FALSE instead of a statement resource

You could verify that checking if $result is equal to FALSE, and furthermore print some error for the user, like this:

```<code>if( $result === false ) {
     die( print_r( sqlsrv_errors(), true));
}
</code>```

Check this link for more information.

Finally mind what Mayank said about SQL injection.

Body:
I get a error --> sqlsrv_num_rows() expects parameter 1 to be resource

Can anyone figure out whats wrong with the coding, ive been trying for 3 hours now
Thanks in advance :)

<?php
include("config.php");

 username and password sent from form
 $myusername=htmlspecialchars($_POST[username]);
 $mypassword=htmlspecialchars($_POST[password]);

 $sql="SELECT * FROM Login WHERE Username=$myusername and
  Password=$mypassword";
  $result=sqlsrv_query($conn,$sql);
  $count=sqlsrv_num_rows($result);

  if($count==1){
     header("location: something");
  }
 else {
     echo "Wrong Username or Password";

 }
 ?>

CONFIG.PHP

 <?php
   $serverName = '';
   $uid = '';
   $pwd = '';
   $databaseName = '';
   $connectionInfo = array( "UID"=>$uid,
                     "PWD"=>$pwd,
                     "Database"=>$databaseName);

   $conn = sqlsrv_connect( $serverName, $connectionInfo);
      if( $conn ) {
            echo "Connection established.
";
       }else{
              echo "Connection could not be established.
";

        }
        ?>

Body:
I have notice 2 things here:

1) You have a typo at line 4, I think you missed the comment markup

instead of:

username and password sent from form

you mean:

// username and password sent from form

2) Mayank already anwsered that second issue. You have another typo at SQL Query and the $result variable is returning FALSE instead of a statement resource

You could verify that checking if $result is equal to FALSE, and furthermore print some error for the user, like this:

if( $result === false ) {
     die( print_r( sqlsrv_errors(), true));
}

Check this link for more information.

Finally mind what Mayank said about SQL injection.


问:
VBA sort function not working
The following is an excerpt from a larger section of VBA code. I'm having a problem sorting a list of values after I paste them from another sheet. The list of values are successfully pasted and are selected before the sorting should be happening. Also the sorting code which I'm using works fine if it's used seperately in a macro by itself. Any suggestions as to what may be going wrong here? Thanks</p>

```<code>Dim ProductType As String
Dim ValueDate As String
ProductType = Range("I19").value
ValueDate = Range("I18").value

ActiveSheet.name = ProductType & " " & ValueDate

Sheets("SensitivityResults").Cells(6, 1).Resize(653).Copy
Sheets(ProductType & " " & ValueDate).Range("A3").PasteSpecial Paste:=xlValues

Selection.Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlGuess
</code>```

答:
selection might not be set properly, try with</p>

```<code>Sheets(ProductType & " " & ValueDate).Range("A3:A656").Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlGuess
</code>```

Title: VBA sort function not working

Body:
The following is an excerpt from a larger section of VBA code. I'm having a problem sorting a list of values after I paste them from another sheet. The list of values are successfully pasted and are selected before the sorting should be happening. Also the sorting code which I'm using works fine if it's used seperately in a macro by itself. Any suggestions as to what may be going wrong here? Thanks

Dim ProductType As String
Dim ValueDate As String
ProductType = Range("I19").value
ValueDate = Range("I18").value

ActiveSheet.name = ProductType & " " & ValueDate

Sheets("SensitivityResults").Cells(6, 1).Resize(653).Copy
Sheets(ProductType & " " & ValueDate).Range("A3").PasteSpecial Paste:=xlValues

Selection.Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlGuess

Body:
selection might not be set properly, try with

Sheets(ProductType & " " & ValueDate).Range("A3:A656").Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlGuess


问:
Spring elasticsearch filter terms consume list
I have the following index and query

```<code>@Document(indexName="idx", type="worker")
public class Worker {
    @Id
    @Field(type = FieldType.Long)
    private Long id;
    @Field(type = FieldType.String)
    private String firstName;
    @Field(type = FieldType.String)
    private String lastName;
    --getter--setter
}

public interface WRepo extends ElasticsearchRepository<Worker, Long> {

    @Query("{
        "query": {"match": {"firstName": "?0"}},
        "filter": {"terms" : {"id" : ***ids***}}
    }")
    Page<Worker> searchWorker(String firstName, List<Long> ids, Pageable pageable);
}
</code>```

Number of ids is changable,  when adding ids static like

{"id" : [101, 102, 103]}

it works as expected, my question is how can I pass id list from method signature like firstName field

{"firstName": "?0"}

答:
I changed method body below

```<code>@Query("{
    "query": {"match": {"firstName": "?0"}},
    "filter": {"terms" : {"id" : [?1]}}
}")
Page<Worker> searchWorker(String firstName, String ids, Pageable pageable);
</code>```

and make my request as:

```<code>repository.searchProduct("firstName", "101,102", pageable)
</code>```

Body:
I have the following index and query

@Document(indexName="idx", type="worker")
public class Worker {
    @Id
    @Field(type = FieldType.Long)
    private Long id;
    @Field(type = FieldType.String)
    private String firstName;
    @Field(type = FieldType.String)
    private String lastName;
    --getter--setter
}

public interface WRepo extends ElasticsearchRepository<Worker, Long> {

    @Query("{
        "query": {"match": {"firstName": "?0"}},
        "filter": {"terms" : {"id" : ***ids***}}
    }")
    Page<Worker> searchWorker(String firstName, List<Long> ids, Pageable pageable);
}

Number of ids is changable,  when adding ids static like

{"id" : [101, 102, 103]}

it works as expected, my question is how can I pass id list from method signature like firstName field

{"firstName": "?0"}

Body:
I changed method body below

@Query("{
    "query": {"match": {"firstName": "?0"}},
    "filter": {"terms" : {"id" : [?1]}}
}")
Page<Worker> searchWorker(String firstName, String ids, Pageable pageable);

and make my request as:

repository.searchProduct("firstName", "101,102", pageable)


问:
Laravel Dusk, how to destroy session data between tests
I am getting started using Laravel Dusk for browser testing, and have created a couple of tests to test my login form. I have the following code:

```<code>class LoginTest extends DuskTestCase
{

public function testLogin()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/admin')
            ->type('email', 'inigo@mydomain.co.uk')
            ->type('password', 'MyPass')
            ->press('Login')
            ->assertSee('Loading...');
    });
}

public function testLoginFailure(){
    $this->browse(function (Browser $browser){

        $browser->visit('/admin/logout'); // I have to add this to logout first, otherwise it's already logged in for this test!

        $browser->visit('/admin')
            ->type('email', 'someemail@afakedomain.com')
            ->type('password', 'somefakepasswordthatdoesntwork')
            ->press('Login')
            ->assertSee('These credentials do not match our records.');
    });
}
</code>```

See the comment. The first function runs fine, but when it comes to the second function, I have to logout first, since the user is already logged in as a result of running the first function. This came as a surprise to me as I thought unit tests were completely independent, with session data being destroyed automatically.

Is there a better way of doing this- some Dusk method that I'm missing perhaps- than having to call $browser->visit('/admin/logout'); ?

Thanks

EDIT Thanks for the 2 answers so far, which both seem valid solutions. I've updated the second function to the following:

```<code>public function testLoginFailure(){
    $this->createBrowsersFor(function(Browser $browser){
        $browser->visit('/admin')
            ->type('email', 'someshit@afakedomain.com')
            ->type('password', 'somefakepasswordthatdoesntwork')
            ->press('Login')
            ->assertSee('These credentials do not match our records.');
    });
}
</code>```

Which does the job. So

I can safely assume that this second browser only exists for the duration of this single function, correct?

What are the obvious advantages/disadvantages of creating a second browser instance rather than using the teardown method?

答:
You could flush the session in a tearDown() method:

class LoginTest extends DuskTestCase
{
    // Your tests

    public function tearDown()
    {
        session()->flush();

        parent::tearDown();
    }
}
```

Body:
I am getting started using Laravel Dusk for browser testing, and have created a couple of tests to test my login form. I have the following code:

class LoginTest extends DuskTestCase
{

public function testLogin()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/admin')
            ->type('email', 'inigo@mydomain.co.uk')
            ->type('password', 'MyPass')
            ->press('Login')
            ->assertSee('Loading...');
    });
}

public function testLoginFailure(){
    $this->browse(function (Browser $browser){

        $browser->visit('/admin/logout'); // I have to add this to logout first, otherwise it's already logged in for this test!

        $browser->visit('/admin')
            ->type('email', 'someemail@afakedomain.com')
            ->type('password', 'somefakepasswordthatdoesntwork')
            ->press('Login')
            ->assertSee('These credentials do not match our records.');
    });
}

See the comment. The first function runs fine, but when it comes to the second function, I have to logout first, since the user is already logged in as a result of running the first function. This came as a surprise to me as I thought unit tests were completely independent, with session data being destroyed automatically.

Is there a better way of doing this- some Dusk method that I'm missing perhaps- than having to call $browser->visit('/admin/logout'); ?

Thanks

EDIT Thanks for the 2 answers so far, which both seem valid solutions. I've updated the second function to the following:

public function testLoginFailure(){
    $this->createBrowsersFor(function(Browser $browser){
        $browser->visit('/admin')
            ->type('email', 'someshit@afakedomain.com')
            ->type('password', 'somefakepasswordthatdoesntwork')
            ->press('Login')
            ->assertSee('These credentials do not match our records.');
    });
}

Which does the job. So

I can safely assume that this second browser only exists for the duration of this single function, correct?

What are the obvious advantages/disadvantages of creating a second browser instance rather than using the teardown method?


问:
Change events execution order using jQuery.live()
I have the following piece of code:

```<code><html>
<head>
 <script src="myplugin.js" />
 <script>
  $(document).ready(function() {
   $('#mytable tbody tr').live('click', function() {
    alert('Hi');
   });
  });
 </script>
</head>
<body>
 <script>
  $('#mytable').myplugin();
 </script>

 <table id="mytable">
  <tbody>
   <tr>
    <td>Something</td>
   </tr>
  </tbody>
 </table>
</body>
</html>

myplugin.js code

.....
return this.each(function(index, id) {
 $(id + ' tbody tr').live('click', function() {
  alert('Hello');
 });
});
.....
</code>```

Under that circustances, the order of executiong would be:

```<code>alert('Hi');
alert('Hello');
</code>```

but I want to be the inverse.
It's possible to change the execution order for this?
thank you

答:
This is not the prettiest way, but you could postpone your first call like this:

```<code>  $(document).ready(function() {
   $('#mytable tbody tr').live('click', function() {
    window.setTimeout(function() {
      alert('Hi');
    }, 0);
   });
  });
</code>```

But maybe there's an "official" way as well...

Body:
I have the following piece of code:

    Something

myplugin.js code

.....
return this.each(function(index, id) {
 $(id + ' tbody tr').live('click', function() {
  alert('Hello');
 });
});
.....

Under that circustances, the order of executiong would be:

alert('Hi');
alert('Hello');

but I want to be the inverse.
It's possible to change the execution order for this?
thank you

Body:
This is not the prettiest way, but you could postpone your first call like this:

  $(document).ready(function() {
   $('#mytable tbody tr').live('click', function() {
    window.setTimeout(function() {
      alert('Hi');
    }, 0);
   });
  });

But maybe there's an "official" way as well...


问:
AES/CBC/PKCS5Padding encrypt in java decrypt in python goes wrong
I'm trying to encrypt a data file in a Java and then decrypt in Python.

but the decrypted data file always got some padding bytes remainslike this

```<code>my normal content^@^@^@^@^@
</code>```

I actually do the UNPAD action in the python code ( func decrypt_file() )

and when I remove the UNPAD action, I got this:

```<code>my normal content^@^@^@^@^@^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P
</code>```

SO, It seems that the Java encrypt method did padding TWICE.

I'm so confused and stuck here. Can anybody help? Thanks a lot!

Here is my code.

(1) java version

```<code>import java.io.File;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {
    private static final String key = "wesurexZ5!Hcurfit";
    private static final String ivs = "zK2hzBvP%FRJ5%lD";
    public static byte[] encrypt(byte[] strInBytes) throws Exception {
        SecretKeySpec skeySpec = getKey(key);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivs.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        return cipher.doFinal(strInBytes);
    }

    public static byte[] decrypt(byte[] strIn) throws Exception {
        SecretKeySpec skeySpec = getKey(key);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivs.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] decrypted = cipher.doFinal(strIn);
        return decrypted;
    }

    private static SecretKeySpec getKey(String strKey) throws Exception {
        byte[] arrBTmp = strKey.getBytes();
        byte[] arrB = new byte[16];
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
        return skeySpec;
    }
}
</code>```

(2) python version

```<code>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

reload(sys)
sys.setdefaultencoding("utf-8")

from Crypto.Cipher import AES

class AESTool(object):

    def __init__(self, iv, key):
        self.iv = iv.decode("utf8")
        self.key = key
        bs = AES.block_size
        self.pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
        self.unpad = lambda s: s[0:-ord(s[-1])]

    def encrypt(self, plain_text):
        try:
            encryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
            encrypted_text = encryptor.encrypt(self.pad(plain_text))
        except Exception as ex:
            raise Exception(ex.message)

        return encrypted_text

    def decrypt(self, decrypted_text):
        try:
            decryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
            plain_text = decryptor.decrypt(decrypted_text)
        except Exception as ex:
            raise Exception(ex.message)

        return self.unpad(plain_text)

    def encrypt_file(self, from_full_path, to_full_path, chunksize=64*1024):
        encryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
        try:
            with open(from_full_path, 'rb') as infile:
                with open(to_full_path, 'wb') as outfile:
                    while True:
                        chunk = infile.read(chunksize)

                        if len(chunk) == 0:
                            break

                        if len(chunk) < chunksize:
                            outfile.write(encryptor.encrypt(self.pad(chunk)))
                            break

                        outfile.write(encryptor.encrypt(chunk))
        except Exception as ex:
            return -1

        return 0

    def decrypt_file(self, from_full_path, to_full_path, chunksize=64*1024):
        decryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
        try:
            with open(from_full_path, 'rb') as infile:
                with open(to_full_path, 'wb') as outfile:
                    prev_chunk = None
                    while True:
                        chunk = infile.read(chunksize)

                        if len(chunk) == 0 and prev_chunk:
                            outfile.write(self.unpad(decryptor.decrypt(prev_chunk)))
                            break

                        if prev_chunk:
                            outfile.write(decryptor.decrypt(prev_chunk))

                        if len(chunk) < chunksize:
                            outfile.write(self.unpad(decryptor.decrypt(chunk)))
                            break

                        prev_chunk = chunk
        except Exception as ex:
            return -1

        return 0
</code>```

答:
Do you know if this happens when you just try and enc/dec using the default modes? Curious to know because if that is the case then we are having the same issue. What I mean by default is using:

```<code>Cipher cipher = Cipher.getInstance("AES");
</code>```

Instead of;

```<code>Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
</code>```

Body:
I'm trying to encrypt a data file in a Java and then decrypt in Python.

but the decrypted data file always got some padding bytes remainslike this

my normal content^@^@^@^@^@

I actually do the UNPAD action in the python code ( func decrypt_file() )

and when I remove the UNPAD action, I got this:

my normal content^@^@^@^@^@^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P

SO, It seems that the Java encrypt method did padding TWICE.

I'm so confused and stuck here. Can anybody help? Thanks a lot!

Here is my code.

(1) java version

import java.io.File;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {
    private static final String key = "wesurexZ5!Hcurfit";
    private static final String ivs = "zK2hzBvP%FRJ5%lD";
    public static byte[] encrypt(byte[] strInBytes) throws Exception {
        SecretKeySpec skeySpec = getKey(key);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivs.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        return cipher.doFinal(strInBytes);
    }

    public static byte[] decrypt(byte[] strIn) throws Exception {
        SecretKeySpec skeySpec = getKey(key);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivs.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] decrypted = cipher.doFinal(strIn);
        return decrypted;
    }

    private static SecretKeySpec getKey(String strKey) throws Exception {
        byte[] arrBTmp = strKey.getBytes();
        byte[] arrB = new byte[16];
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
        return skeySpec;
    }
}

(2) python version

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

reload(sys)
sys.setdefaultencoding("utf-8")

from Crypto.Cipher import AES

class AESTool(object):

    def __init__(self, iv, key):
        self.iv = iv.decode("utf8")
        self.key = key
        bs = AES.block_size
        self.pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
        self.unpad = lambda s: s[0:-ord(s[-1])]

    def encrypt(self, plain_text):
        try:
            encryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
            encrypted_text = encryptor.encrypt(self.pad(plain_text))
        except Exception as ex:
            raise Exception(ex.message)

        return encrypted_text

    def decrypt(self, decrypted_text):
        try:
            decryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
            plain_text = decryptor.decrypt(decrypted_text)
        except Exception as ex:
            raise Exception(ex.message)

        return self.unpad(plain_text)

    def encrypt_file(self, from_full_path, to_full_path, chunksize=64*1024):
        encryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
        try:
            with open(from_full_path, 'rb') as infile:
                with open(to_full_path, 'wb') as outfile:
                    while True:
                        chunk = infile.read(chunksize)

                        if len(chunk) == 0:
                            break

                        if len(chunk) < chunksize:
                            outfile.write(encryptor.encrypt(self.pad(chunk)))
                            break

                        outfile.write(encryptor.encrypt(chunk))
        except Exception as ex:
            return -1

        return 0

    def decrypt_file(self, from_full_path, to_full_path, chunksize=64*1024):
        decryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
        try:
            with open(from_full_path, 'rb') as infile:
                with open(to_full_path, 'wb') as outfile:
                    prev_chunk = None
                    while True:
                        chunk = infile.read(chunksize)

                        if len(chunk) == 0 and prev_chunk:
                            outfile.write(self.unpad(decryptor.decrypt(prev_chunk)))
                            break

                        if prev_chunk:
                            outfile.write(decryptor.decrypt(prev_chunk))

                        if len(chunk) < chunksize:
                            outfile.write(self.unpad(decryptor.decrypt(chunk)))
                            break

                        prev_chunk = chunk
        except Exception as ex:
            return -1

        return 0

Body:
Do you know if this happens when you just try and enc/dec using the default modes? Curious to know because if that is the case then we are having the same issue. What I mean by default is using:

Cipher cipher = Cipher.getInstance("AES");

Instead of;

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");


问:
How can send mail as a text file from localhost to a folder mail to disk in xampp version 7.1.6 / PHP 7.1.6
How can send mail as a text file from localhost to a folder mail to disk in
xampp version 7.1.6 / PHP 7.1.6</p>

答:
How can send mail as a text file from localhost to a folder mail to disk in
xampp version 7.1.6 / PHP 7.1.6

There is a file with extension mailtodisc

you can configure C:\xampp\php\php.ini

in C:\xampp\php\php.ini
find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

in php.ini file find [mail function]

un comment following line if there is another
sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" in the php.ini file

Also remember to restart the server using the XAMMP control panel so the changes take effect.


问:
Fail to wire play dependancies with Macwire
i have a api service class that is dependent on play's Configuration and WSClient instancees.

and I dont want to use @Inject() anotation cause I want to use compile-time injection with Macwire so what i did is this:

```<code>// this is a trait that here im wiring all the dependencies that my api service needs
trait ApiDependencies {

  lazy val conf: Configuration = wire[Configuration]
  lazy val wsc: WSClient = wire[WSClient]

}

// this is the api service

class ApiService extends ApiDependencies {

  def getInfo (id: String): Future[Option[Info]] = {
    wsc.url("...").withHttpHeaders(("Content-Type", "application/json")).get.map { response =>
      response.status match {
        case Status.OK => ...
        case Status.NO_CONTENT => ...
        case _ => throw new Exception()
      }
    }
  }
}
</code>```

but I get a compiler error:

Error: Cannot find a value of type: [com.typesafe.config.Config]

  lazy val conf: Configuration = wire[Configuration]

Error: Cannot find a public constructor nor a companion object for
  [play.api.libs.ws.WSClient]   lazy val wsc: WSClient = wire[WSClient]

does someone knows how can I solve this issue...? and why is it happening :/

thanks!

答:
Configuration is a playframework Configuration, which internally uses Typesafe's Config library. Quoting Playframework docs:

The configuration file used by Play is based on the Typesafe config library

The exception you're getting tells you exactly this - macwire is unable to create an instance of Configuration as there are no Config instance in scope.

To fix it, you obviously need to provide such instace. Simplest way to do so would probably look like this:

```<code>import com.typesafe.config.{Config, ConfigFactory}
trait ApiDependencies {
    lazy val configuration: Config = ConfigFactory.load()
    lazy val conf: Configuration = wire[Configuration]
}
</code>```

Note that ConfigFactory.Load() basically uses default configuration file (application.conf) and it does take into account configuration overriding techniques described in Play's Configuration docs, as it is actually provided by typesafe config library (from Typesafe Config GitHub readme):

users can override the config with Java system properties, java -Dmyapp.foo.bar=10

Regarding WSClient: this is due to the fact that WSClient is not a class, but a trait. You need to wire actual implementation, i.e. NingWSClient, like this:

```<code>trait ApiDependencies {
  lazy val conf: Configuration = wire[Configuration]
  lazy val wsc: WSClient = wire[NingWSClient]
}
</code>```

See WSClient scaladoc for list of implementing classes (under "All Known Implementing Classes") - by the time of writing there are only NingWSClient and AhcWSClient. Which one is better is a different (and potentially opinion-based question).

Body:
i have a api service class that is dependent on play's Configuration and WSClient instancees.

and I dont want to use @Inject() anotation cause I want to use compile-time injection with Macwire so what i did is this:

// this is a trait that here im wiring all the dependencies that my api service needs
trait ApiDependencies {

  lazy val conf: Configuration = wire[Configuration]
  lazy val wsc: WSClient = wire[WSClient]

}

// this is the api service

class ApiService extends ApiDependencies {

  def getInfo (id: String): Future[Option[Info]] = {
    wsc.url("...").withHttpHeaders(("Content-Type", "application/json")).get.map { response =>
      response.status match {
        case Status.OK => ...
        case Status.NO_CONTENT => ...
        case _ => throw new Exception()
      }
    }
  }
}

but I get a compiler error:

Error: Cannot find a value of type: [com.typesafe.config.Config]

  lazy val conf: Configuration = wire[Configuration]

Error: Cannot find a public constructor nor a companion object for
  [play.api.libs.ws.WSClient]   lazy val wsc: WSClient = wire[WSClient]

does someone knows how can I solve this issue...? and why is it happening :/

thanks!

Body:
Configuration is a playframework Configuration, which internally uses Typesafe's Config library. Quoting Playframework docs:

The configuration file used by Play is based on the Typesafe config library

The exception you're getting tells you exactly this - macwire is unable to create an instance of Configuration as there are no Config instance in scope.

To fix it, you obviously need to provide such instace. Simplest way to do so would probably look like this:

import com.typesafe.config.{Config, ConfigFactory}
trait ApiDependencies {
    lazy val configuration: Config = ConfigFactory.load()
    lazy val conf: Configuration = wire[Configuration]
}

Note that ConfigFactory.Load() basically uses default configuration file (application.conf) and it does take into account configuration overriding techniques described in Play's Configuration docs, as it is actually provided by typesafe config library (from Typesafe Config GitHub readme):

users can override the config with Java system properties, java -Dmyapp.foo.bar=10

Regarding WSClient: this is due to the fact that WSClient is not a class, but a trait. You need to wire actual implementation, i.e. NingWSClient, like this:

trait ApiDependencies {
  lazy val conf: Configuration = wire[Configuration]
  lazy val wsc: WSClient = wire[NingWSClient]
}

See WSClient scaladoc for list of implementing classes (under "All Known Implementing Classes") - by the time of writing there are only NingWSClient and AhcWSClient. Which one is better is a different (and potentially opinion-based question).


问:
Can I get a reference to a pending transaction from a SqlConnection object?
Suppose someone (other than me) writes the following code and compiles it into an assembly:

```<code>using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();
    using (var transaction = conn.BeginTransaction())
    {
        /* Update something in the database */
        /* Then call any registered OnUpdate handlers */
        InvokeOnUpdate(conn);

        transaction.Commit();
    }
}
</code>```

The call to InvokeOnUpdate(IDbConnection conn) calls out to an event handler that I can implement and register. Thus, in this handler I will have a reference to the IDbConnection object, but I won't have a reference to the pending transaction. Is there any way in which I can get a hold of the transaction? In my OnUpdate handler I want to execute something similar to the following:

```<code>private void MyOnUpdateHandler(IDbConnection conn)
{
    var cmd = conn.CreateCommand();
    cmd.CommandText = someSQLString;
    cmd.CommandType = CommandType.Text;

    cmd.ExecuteNonQuery();
}
</code>```

However, the call to cmd.ExecuteNonQuery() throws an InvalidOperationException complaining that

"ExecuteNonQuery requires the command
  to have a transaction when the
  connection assigned to the command is
  in a pending local transaction.  The
  Transaction property of the command
  has not been initialized".

Can I in any way enlist my SqlCommand cmd with the pending transaction? Can I retrieve a reference to the pending transaction from the IDbConnection object (I'd be happy to use reflection if necessary)?

答:
I am a big proponent of simple so how about writing a wrapper over IDBConnection (DELEGATE PATTERN) that exposes Transaction. (Sorry for VB.NET code, I am writing this in VB.NET right now) Something like this:

```<code>  Public class MyConnection
      Implements IDbConnection

      Private itsConnection as IDbConnection
      Private itsTransaction as IDbTransaction

      Public Sub New(ByVal conn as IDbConnection)
         itsConnection = conn
      End Sub

      //...  'All the implementations would look like
      Public Sub Dispose() Implements IDbConnection.Dispose
         itsConnection.Dispose()
      End Sub
      //...

      //     'Except BeginTransaction which would look like
       Public Overridable Function BeginTransaction() As IDbTransaction Implements IDbConnection.BeginTransaction
         itsTransaction = itsConnection.BeginTransaction()
         Return itsTransaction
       End Function

      // 'Now you can create a property and use it everywhere without any hacks
       Public ReadOnly Property Transaction
          Get
              return itsTransaction
          End Get
       End Property

    End Class
</code>```

So you would instantiate this as:

```<code>Dim myConn as new MyConnection(new SqlConnection(...))
</code>```

and then you can get the transaction any time by using:

```<code> myConn.Transaction
</code>```

OwnerDisplayName: Rune

Body:
Suppose someone (other than me) writes the following code and compiles it into an assembly:

using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();
    using (var transaction = conn.BeginTransaction())
    {
        /* Update something in the database */
        /* Then call any registered OnUpdate handlers */
        InvokeOnUpdate(conn);

        transaction.Commit();
    }
}

The call to InvokeOnUpdate(IDbConnection conn) calls out to an event handler that I can implement and register. Thus, in this handler I will have a reference to the IDbConnection object, but I won't have a reference to the pending transaction. Is there any way in which I can get a hold of the transaction? In my OnUpdate handler I want to execute something similar to the following:

private void MyOnUpdateHandler(IDbConnection conn)
{
    var cmd = conn.CreateCommand();
    cmd.CommandText = someSQLString;
    cmd.CommandType = CommandType.Text;

    cmd.ExecuteNonQuery();
}

However, the call to cmd.ExecuteNonQuery() throws an InvalidOperationException complaining that

"ExecuteNonQuery requires the command
  to have a transaction when the
  connection assigned to the command is
  in a pending local transaction.  The
  Transaction property of the command
  has not been initialized".

Can I in any way enlist my SqlCommand cmd with the pending transaction? Can I retrieve a reference to the pending transaction from the IDbConnection object (I'd be happy to use reflection if necessary)?

Body:
I am a big proponent of simple so how about writing a wrapper over IDBConnection (DELEGATE PATTERN) that exposes Transaction. (Sorry for VB.NET code, I am writing this in VB.NET right now) Something like this:

  Public class MyConnection
      Implements IDbConnection

      Private itsConnection as IDbConnection
      Private itsTransaction as IDbTransaction

      Public Sub New(ByVal conn as IDbConnection)
         itsConnection = conn
      End Sub

      //...  'All the implementations would look like
      Public Sub Dispose() Implements IDbConnection.Dispose
         itsConnection.Dispose()
      End Sub
      //...

      //     'Except BeginTransaction which would look like
       Public Overridable Function BeginTransaction() As IDbTransaction Implements IDbConnection.BeginTransaction
         itsTransaction = itsConnection.BeginTransaction()
         Return itsTransaction
       End Function

      // 'Now you can create a property and use it everywhere without any hacks
       Public ReadOnly Property Transaction
          Get
              return itsTransaction
          End Get
       End Property

    End Class

So you would instantiate this as:

Dim myConn as new MyConnection(new SqlConnection(...))

and then you can get the transaction any time by using:

 myConn.Transaction


问:
Checking the user email and password
I want to validate if the email and password in the form are true, so that I can redirect it to a new page. But, when I reload the page it doesn't validate if the user email and the user password are correct it does actually nothing.  It only gives me back the user email and password it needs to check.

 var app = angular
    .module('formValidation', ['ui-router'])
    .controller("locationFormCtrl", locationFormCtrl);
    // .config('$stateprovider','$urlRouterProvider',routeFunc);

    function locationFormCtrl($http,config){
      $http.post(config.apiUrl+ "login", {email: "ehnk@gmail.nl", password: "welkom123"}).then(function(response){
          if(email == "audrey@appsharing.nl" & password == "welkom123"){
            header('Location : opdrachten.html');
          }else{
            alert('wrong password');
          }
      });
    }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><!DOCTYPE html>
 <html>
  <head>
   <title>Login Form</title>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>>
   <script type="text/javascript" src="main.js"></script>
  </head>
  <body ng-app="formValidation" ng-controller="locationFormCtrl" >
  <form novalidate class="simple-form" ng-submit="submit()">
   <label>Email: <input type="text" ng-model="user.email" name="email"/></label><br>
   <label>Password: <input type="password" ng-model="user.password" name="password" required/></label><br>
   <input type="submit"  value="login">
  </form>
  </body>
 </html></code>```

答:
```<code>//please update condition as below..

if(email == "audrey@appsharing.nl" && password == "welkom123"){
   header('Location : opdrachten.html');
}else{
   alert('wrong password');
}
</code>```

Title: Checking the user email and password

Body:
I want to validate if the email and password in the form are true, so that I can redirect it to a new page. But, when I reload the page it doesn't validate if the user email and the user password are correct it does actually nothing.  It only gives me back the user email and password it needs to check.

 var app = angular
    .module('formValidation', ['ui-router'])
    .controller("locationFormCtrl", locationFormCtrl);
    // .config('$stateprovider','$urlRouterProvider',routeFunc);

    function locationFormCtrl($http,config){
      $http.post(config.apiUrl+ "login", {email: "ehnk@gmail.nl", password: "welkom123"}).then(function(response){
          if(email == "audrey@appsharing.nl" & password == "welkom123"){
            header('Location : opdrachten.html');
          }else{
            alert('wrong password');
          }
      });
    }

<!DOCTYPE html>

   <title>Login Form</title>
   >

  <form novalidate class="simple-form" ng-submit="submit()">
   Email: <input type="text" ng-model="user.email" name="email"/>

   Password: <input type="password" ng-model="user.password" name="password" required/>

   <input type="submit"  value="login">
  </form>


问:
Update single column based on multiple 'priority' columns
Suppose you had a DataFrame with a number of columns / Series- say five for example. If the fifth column (named 'Updated Col') had values, in addition to nans, what would be the best way to insert values into 'Updated Col' from other columns in place of the nans based on a preferred column order?

e.g. my dataframe looks something like this;

```<code>Date                 1     2    3   4   Updated Col
12/03/2017 0:00     0.4                 0.9
12/03/2017 0:10     0.4                 0.1
12/03/2017 0:20     0.4                 0.6
12/03/2017 0:30     0.9   0.7           Nan
12/03/2017 0:40     0.1                 Nan
12/03/2017 0:50     0.6            0.5  Nan
12/03/2017 1:00     0.4        0.3      Nan
12/03/2017 1:10     0.3            0.2  Nan
12/03/2017 1:20     0.9                 0.8
12/03/2017 1:30     0.9                 0.8
12/03/2017 1:40     0.0                 0.9
</code>```

..and say for example I wanted the values from column 3 as a priority, followed by 2, then 1, i would expect the DataFrame to look like this;

```<code>                  1    2      3   4   Updated Col
12/03/2017 0:00  0.4                  0.9
12/03/2017 0:10  0.4                  0.1
12/03/2017 0:20  0.4                  0.6
12/03/2017 0:30  0.9  0.7             0.7
12/03/2017 0:40  0.1                  0.1
12/03/2017 0:50  0.6             0.5  0.5
12/03/2017 1:00  0.4        0.3       0.3
12/03/2017 1:10  0.3             0.2  0.2
12/03/2017 1:20  0.9                  0.8
12/03/2017 1:30  0.9                  0.8
12/03/2017 1:40  0.0                  0.9
</code>```

..values would be input from the lower priority columns only if the higher priority columns were empty / NaN.

What would be the best way to do this?

I've tried numerous np.where attempts but cant work out what the best way would be?

Many thanks in advance.

Body:
Suppose you had a DataFrame with a number of columns / Series- say five for example. If the fifth column (named 'Updated Col') had values, in addition to nans, what would be the best way to insert values into 'Updated Col' from other columns in place of the nans based on a preferred column order?

e.g. my dataframe looks something like this;

Date                 1     2    3   4   Updated Col
12/03/2017 0:00     0.4                 0.9
12/03/2017 0:10     0.4                 0.1
12/03/2017 0:20     0.4                 0.6
12/03/2017 0:30     0.9   0.7           Nan
12/03/2017 0:40     0.1                 Nan
12/03/2017 0:50     0.6            0.5  Nan
12/03/2017 1:00     0.4        0.3      Nan
12/03/2017 1:10     0.3            0.2  Nan
12/03/2017 1:20     0.9                 0.8
12/03/2017 1:30     0.9                 0.8
12/03/2017 1:40     0.0                 0.9

..and say for example I wanted the values from column 3 as a priority, followed by 2, then 1, i would expect the DataFrame to look like this;

                  1    2      3   4   Updated Col
12/03/2017 0:00  0.4                  0.9
12/03/2017 0:10  0.4                  0.1
12/03/2017 0:20  0.4                  0.6
12/03/2017 0:30  0.9  0.7             0.7
12/03/2017 0:40  0.1                  0.1
12/03/2017 0:50  0.6             0.5  0.5
12/03/2017 1:00  0.4        0.3       0.3
12/03/2017 1:10  0.3             0.2  0.2
12/03/2017 1:20  0.9                  0.8
12/03/2017 1:30  0.9                  0.8
12/03/2017 1:40  0.0                  0.9

..values would be input from the lower priority columns only if the higher priority columns were empty / NaN.

What would be the best way to do this?

I've tried numerous np.where attempts but cant work out what the best way would be?

Many thanks in advance.

Body:
You can use fillna with forward filling (ffill) and then select  column:

updated_col =  'Updated Col'
#define columns for check, maybe [1,2,3,4] if integer colum names
cols = ['1','2','3','4'] + [updated_col]

print (df[cols].ffill(axis=1))
      1    2    3    4  Updated Col
0   0.4  0.4  0.4  0.4          0.9
1   0.4  0.4  0.4  0.4          0.1
2   0.4  0.4  0.4  0.4          0.6
3   0.9  0.9  0.7  0.7          0.7
4   0.1  0.1  0.1  0.1          0.1
5   0.6  0.6  0.6  0.5          0.5
6   0.4  0.4  0.3  0.3          0.3
7   0.3  0.3  0.3  0.2          0.2
8   0.9  0.9  0.9  0.9          0.8
9   0.9  0.9  0.9  0.9          0.8
10  0.0  0.0  0.0  0.0          0.9

df[updated_col] = df[cols].ffill(axis=1)[updated_col]
print (df)
               Date    1   2    3    4  Updated Col
0   12/03/2017 0:00  0.4 NaN  NaN  NaN          0.9
1   12/03/2017 0:10  0.4 NaN  NaN  NaN          0.1
2   12/03/2017 0:20  0.4 NaN  NaN  NaN          0.6
3   12/03/2017 0:30  0.9 NaN  0.7  NaN          0.7
4   12/03/2017 0:40  0.1 NaN  NaN  NaN          0.1
5   12/03/2017 0:50  0.6 NaN  NaN  0.5          0.5
6   12/03/2017 1:00  0.4 NaN  0.3  NaN          0.3
7   12/03/2017 1:10  0.3 NaN  NaN  0.2          0.2
8   12/03/2017 1:20  0.9 NaN  NaN  NaN          0.8
9   12/03/2017 1:30  0.9 NaN  NaN  NaN          0.8
10  12/03/2017 1:40  0.0 NaN  NaN  NaN          0.9

EDIT:

Thank you shivsn for comments.

If have Nan (string values) in DataFrame what are not NaNs (missing values) or empty string values is necessary first replace:

updated_col =  'Updated Col'
cols = ['1','2','3','4'] + ['Updated Col']
d = {'Nan':np.nan, '': np.nan}
df = df.replace(d)
df[updated_col] = df[cols].ffill(axis=1)[updated_col]
print (df)
               Date    1   2    3    4 Updated Col
0   12/03/2017 0:00  0.4 NaN  NaN  NaN         0.9
1   12/03/2017 0:10  0.4 NaN  NaN  NaN         0.1
2   12/03/2017 0:20  0.4 NaN  NaN  NaN         0.6
3   12/03/2017 0:30  0.9 NaN  0.7  NaN         0.7
4   12/03/2017 0:40  0.1 NaN  NaN  NaN         0.1
5   12/03/2017 0:50  0.6 NaN  NaN  0.5         0.5
6   12/03/2017 1:00  0.4 NaN  0.3  NaN         0.3
7   12/03/2017 1:10  0.3 NaN  NaN  0.2         0.2
8   12/03/2017 1:20  0.9 NaN  NaN  NaN         0.8
9   12/03/2017 1:30  0.9 NaN  NaN  NaN         0.8
10  12/03/2017 1:40  0.0 NaN  NaN  NaN         0.9


问:
Amazon ec2 not working when accessing through public IP
I am new to Amazon EC2. The application is working fine on the Amazon EC2 Windows Server.
But when i am trying to access the same through the public ip, it is not accessible.
I have seen the inbound configurations and the ports(HTTP,HTTPS,SSH) are enabled.

I am unable to access the application through public ip.

```<code> http:ec2-XX-XX-XXX-XX.us-west-2.compute.amazonaws.com
</code>```

is accessible when inside the EC2 region, however

```<code>XX.XX.XXX.XX:8080/myapp
</code>```

is not accessible.

```<code>XX.XX.XXX.XX is the public ip.
</code>```

Yess.. This is VPC..!!!

答:
create security group with request you are wish to grant access. here I've created security group to any request

Then add the specific security group to network interface

Body:
I am new to Amazon EC2. The application is working fine on the Amazon EC2 Windows Server.
But when i am trying to access the same through the public ip, it is not accessible.
I have seen the inbound configurations and the ports(HTTP,HTTPS,SSH) are enabled.

I am unable to access the application through public ip.

 http:ec2-XX-XX-XXX-XX.us-west-2.compute.amazonaws.com

is accessible when inside the EC2 region, however

XX.XX.XXX.XX:8080/myapp

is not accessible.

XX.XX.XXX.XX is the public ip.

Yess.. This is VPC..!!!


问:
What simple guidelines would you give your developers for writing good SQL against Oracle?
I work in a group of about 25 developers.  I'm responsible for coming up with the database design (tables, views, etc) and am called apon for performance tuning when necessary.

There are a couple of different applications that connect.  Database access is via JDBC, hibernate, and iBatis SQL maps.  Developers with various levels of experience write SQL statements.

What guidelines would you give to developers to write good SQL?

By good I mean: correct, performs well, easy to understand and maintain.

These are just meant to be easy to follow guidelines - I want to get people onto the right track for the majority of situations.  We will break these guidelines when it makes sense.

EDIT: We have in place code reviews for all source commits (SQL, java, etc) enforced through a jira workflow.

答:
Pair-program. Any advantange it provides for agile development in general, at least doubles for SQL development.

Second choice, code reviews for all SQL.

OwnerDisplayName: WW

LastEditorDisplayName: WW

OwnerDisplayName: le dorfier