问:
how can i include files for all functions in php
i'm new in php as you know and i have some problems (like any new starts) and my problem is i cant include file like config.php to all functions in the files

i have just 3 files

in config.php i just connect to database in mysqli and the variable is $connect

in functions.php there is just a simple functions

```<code>include('config.php');
function rows($table){
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}
</code>```

in index.php

```<code>include('functions.php');
echo rows('books');
</code>```

and the problem is i cant get $connect variable from config.php to work in functions in functions.php file.

EDIT

when i include config.php inside the functions everything will be ok,
like this

```<code>function rows($table){
include('config.php');
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}
</code>```

but i don't want to do this because i want to include a config.php for all functions i have.

Thanks

答:
The problem is you declare a variable in the global scope, however whatever is available in the global scope is not automatically available in the function scope unless your force it available via global $connect which has its own caveats.

Some alternatives to global would be.

1) Use a singleton class:

```<code><?php
//File connect.php
class Connection {
    private static $connection = null;
    public static get() {
          if (self::$connection == null) { self::$connection = mysqli_connect(...); }
          return self::$connection;
    }
}
</code>```

And use it as

```<code>include_once('connect.php');
function rows($table){
    $gettables = mysqli_query(Connection::get(),"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}
</code>```

2) Constants (Not the best idea):

```<code> <?php
 //File config.php
 define('CONNECTION',mysqli_connect(...));
</code>```

Use as:

```<code>include_once('config.php');
function rows($table){
    $gettables = mysqli_query(CONNECTION,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}
</code>```

3) Pass it as a parameter to the function (config.php is as you have it now):

```<code>function rows($connection, $table){
    $gettables = mysqli_query($connection,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}
</code>```

Use as:

```<code>include('config.php');
include_once('functions.php');
echo rows($connection, 'books');
</code>```

4) Have the file work like a variable declaration

```<code><?php
//config.php
return mysqli_connect(...);
</code>```

Use as:

```<code>function rows($table){
     $connect = include('config.php');
     $gettables = mysqli_query($connect,"SELECT * FROM $table");
     $numrows = mysqli_num_rows($gettables);
     return($numrows);
}
</code>```

Body:
i'm new in php as you know and i have some problems (like any new starts) and my problem is i cant include file like config.php to all functions in the files

i have just 3 files

in config.php i just connect to database in mysqli and the variable is $connect

in functions.php there is just a simple functions

include('config.php');
function rows($table){
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

in index.php

include('functions.php');
echo rows('books');

and the problem is i cant get $connect variable from config.php to work in functions in functions.php file.

EDIT

when i include config.php inside the functions everything will be ok,
like this

function rows($table){
include('config.php');
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

but i don't want to do this because i want to include a config.php for all functions i have.

Thanks

Body:
The problem is you declare a variable in the global scope, however whatever is available in the global scope is not automatically available in the function scope unless your force it available via global $connect which has its own caveats.

Some alternatives to global would be.

1) Use a singleton class:

<?php
//File connect.php
class Connection {
    private static $connection = null;
    public static get() {
          if (self::$connection == null) { self::$connection = mysqli_connect(...); }
          return self::$connection;
    }
}

And use it as

include_once('connect.php');
function rows($table){
    $gettables = mysqli_query(Connection::get(),"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

2) Constants (Not the best idea):

 <?php
 //File config.php
 define('CONNECTION',mysqli_connect(...));

Use as:

include_once('config.php');
function rows($table){
    $gettables = mysqli_query(CONNECTION,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

3) Pass it as a parameter to the function (config.php is as you have it now):

function rows($connection, $table){
    $gettables = mysqli_query($connection,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

Use as:

include('config.php');
include_once('functions.php');
echo rows($connection, 'books');

4) Have the file work like a variable declaration

<?php
//config.php
return mysqli_connect(...);

Use as:

function rows($table){
     $connect = include('config.php');
     $gettables = mysqli_query($connect,"SELECT * FROM $table");
     $numrows = mysqli_num_rows($gettables);
     return($numrows);
}


来源: stackexchange

问:
How to combine webpack with ng-build?
I am using webpack.config to complile, bindle and minify my scss files

```<code> module: {
 rules: [{
        test: /\.scss$/,
         use: extractSass.extract({
            use: [{
                loader: "css-loader"
            }, {
                loader: "sass-loader"
            }],
            // use style-loader in development
            fallback: "style-loader"
        })
    }]
 },
</code>```

The problem now is ng-build doesn't run webpack.config auto, I have to run webpack command then ng-build, I need to include webpack.config in ng-build or create custom command to do both.

答: ng-cli is built on webpack. You can use ng eject to remove the ng-cli encapsulation and reveal the webpack configuration underneath.

ContentLicense: CC BY-SA 3.0

Title: How to combine webpack with ng-build?

Body:
I am using webpack.config to complile, bindle and minify my scss files

 module: {
 rules: [{
        test: /\.scss$/,
         use: extractSass.extract({
            use: [{
                loader: "css-loader"
            }, {
                loader: "sass-loader"
            }],
            // use style-loader in development
            fallback: "style-loader"
        })
    }]
 },

The problem now is ng-build doesn't run webpack.config auto, I have to run webpack command then ng-build, I need to include webpack.config in ng-build or create custom command to do both.


问:
Error in build for Xamarin Android when adding Google Map
I have an android xamarin project, and I only add Xamarin.GooglePlayServices.Maps nuget.

my build has this error :

error: package com.google.android.gms.maps does not exist
        com.google.android.gms.maps.OnMapReadyCallback

答:
I installed Google APIs , and Google play service, but still I got the error.

I check this path

obj\Debug\__library_projects__\Xamarin.GooglePlayServices.Maps\library_project_imports

and the folder was empty, this folder suppose to have .jar files for Google Map.

So I delete obj folder and now everything's working.


问:
Properly detect keyboard layout
I have a winforms application where I need to obtain the current keyboard layout of the user. To do that I'm using System.Windows.Forms.InputLanguage.CurrentInputLanguage.LayoutName.

This works fine as long as the user has the form as his active window, once he focuses something else and changes the language the former property wont return a proper value, it will return the last used language while the form was still the active window.

Is there a way I can get the users keyboard layout's name even if he is not focusing the form, there are no restrictions to what can be used.

答:
Couple of things to note here. Keyboard selection in Windows is done on a per-thread basis. This allows the user to select a different keyboard locale for any given application, which Windows will respect, while leaving other applications alone.

The user does this by enabling the language bar (for installed keyboards) in the Windows Taskbar. If they select a different keyboard while another application has the focus, then the selection only applies to that application. And besides, if your application doesn't have the focus, it can't do anything about it anyway. By using the language bar in this way, the user is plainly stating their intention to have the selected keyboard apply to the active application only. There is no way for your application to find out about it, since from Windows' point of view, it's none of your application's business.

Now, if you want to know if the user changes the keyboard for the entire system (using the control panel applet), that's feasible. If your application doesn't have the focus, there's no way to trap the notification message. Your form will still consider the current language to be the one it started with. However, the system-wide change does change the InputLanguage.DefaultInputLanguage. to the newly-selected keyboard. So, override the OnActivated handler and check the value of the default language, not the current one (which is per-thread).

Title: Properly detect keyboard layout


问:
Find a place using coordinates in google maps: differences between Google Maps web application ad Google Maps API v3?
I need to locate a point on a Google Maps using Google Maps API v3.

This is a sample: 42.62649134593722, 13.283811807632446

If I use Google Maps https://www.google.it/maps/place/42%C2%B037'35.4%22N+13%C2%B017'01.7%22E/@42.6264913,13.2816231,17z/data=!3m1!4b1!4m5!3m4!1s0x0:0x0!8m2!3d42.6264913!4d13.2838118 web application I obtain this

Using the Geocoding example ( https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple) on the same coordinates couple I've seen that the marker on the map is located in a different location

How may I have the same behaviour and location using Geocoding API? Alternatives? Examples?

答:
The value of 42.62649134593722, 13.283811807632446 is a latitude and longitude value. It is an exact point on the map.

In the second screenshot you are attempting to geocode the value of 42.62649134593722, 13.283811807632446, despite it being an exact location already.

Geocoding is:

Geocoding is the process of converting addresses (like "1600
  Amphitheatre Parkway, Mountain View, CA") into geographic coordinates
  (like latitude 37.423021 and longitude -122.083739), which you can use
  to place markers on a map or position the map.
  - Source: https://developers.google.com/maps/documentation/geocoding/intro

If you already have the lat/lng I am unsure why you would need to utilise the geocoding API...

JSFiddle: http://jsfiddle.net/4mtyu/2763/


问:
Removing characters in a java string
I want to remove charecters from a string. Example: I have an application that as the users check off the check mark its building the sql script, so if they check female it adds to the String And Gender = 'Female'. and if they select Male it adds And Where gender = 'Male'. The script is working well when adding to string as they check off but when you uncheck a item I am not sure how to remove the char that were added when it was checked. I tried .Replace .ReplaceAll and all the other replace. I know the string I want to remove, why won't it replace it with "".

```<code>String SQL;
SQL += " AND Gender = 'Female'";
SQL.Replace("AND Gender = 'Female'","");
</code>```

This won't work!

答:
String is immutable class. Here is another solution : convert to StringBuffer or StringBuilder and
delete from the startIndex to endIndex </p>

```<code>String s = "test tes te t ";
String str = new StringBuffer(s).delete(start, end).toString();

for example :
String str = new StringBuffer(s).delete(0, 8).toString();
System.out.println(str); // s te t
</code>```

ContentLicense: CC BY-SA 2.5

Title: Removing characters in a java string

Body:
I want to remove charecters from a string. Example: I have an application that as the users check off the check mark its building the sql script, so if they check female it adds to the String And Gender = 'Female'. and if they select Male it adds And Where gender = 'Male'. The script is working well when adding to string as they check off but when you uncheck a item I am not sure how to remove the char that were added when it was checked. I tried .Replace .ReplaceAll and all the other replace. I know the string I want to remove, why won't it replace it with "".

String SQL;
SQL += " AND Gender = 'Female'";
SQL.Replace("AND Gender = 'Female'","");

This won't work!

Body:
String is immutable class. Here is another solution : convert to StringBuffer or StringBuilder and
delete from the startIndex to endIndex

String s = "test tes te t ";
String str = new StringBuffer(s).delete(start, end).toString();

for example :
String str = new StringBuffer(s).delete(0, 8).toString();
System.out.println(str); // s te t


问:
How to Change one controller value into another Control
```<code>var getAgentBalanceAmount = function (ClientId, UserId) {
    $http({
        method: 'GET',
        url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
    })
    .then(function success(response) {
        $scope.getBalAmount = response.data[0].BalanceAmount;
    }, function error(response) {
        alert('Error');
    });
};
</code>```

In this method  $scope.getBalAmount have some value and bind into index.html page.here index page act as a master page.i want to change this value on other pages(in content page.) is it possible.Thanks in advance.

答:
set in rootscope.</p>

```<code>  var getAgentBalanceAmount = function (ClientId, UserId) {
        $http({
            method: 'GET',
            url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
        })
        .then(function success(response) {
            $rootscope.getBalAmount = response.data[0].BalanceAmount;
        }, function error(response) {
            alert('Error');
        });
    };
</code>```

Body:
var getAgentBalanceAmount = function (ClientId, UserId) {
    $http({
        method: 'GET',
        url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
    })
    .then(function success(response) {
        $scope.getBalAmount = response.data[0].BalanceAmount;
    }, function error(response) {
        alert('Error');
    });
};

In this method  $scope.getBalAmount have some value and bind into index.html page.here index page act as a master page.i want to change this value on other pages(in content page.) is it possible.Thanks in advance.

Body:
set in rootscope.

  var getAgentBalanceAmount = function (ClientId, UserId) {
        $http({
            method: 'GET',
            url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
        })
        .then(function success(response) {
            $rootscope.getBalAmount = response.data[0].BalanceAmount;
        }, function error(response) {
            alert('Error');
        });
    };


问:
Gradle loaded Serenity libs to project libraries but can't see them further
I have this build.gradle file

```<code>apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'net.serenity-bdd.aggregator'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath("net.serenity-bdd:serenity-gradle-plugin:1.4.0")
    }
}

dependencies {
    testCompile('net.serenity-bdd:serenity-core:1.4.0')
    testCompile('net.serenity-bdd:serenity-junit:1.4.0')
    testCompile('junit:junit:4.12')
    testCompile('org.assertj:assertj-core:1.7.0')
    testCompile('org.slf4j:slf4j-simple:1.7.7')

}

gradle.startParameter.continueOnFailure = true
</code>```

It loads serenity libraries to the project BUT some classes are not visible when compile:
PageObject class is not visible

but it truly exists in External libs:

Class in the libs

What should I do to make it be visible in test classes and page object classes?
Also when I import manually by entering full path to the library then gradle runner does not see those classes anyway.

答: Issue is resolved. Wrong compile scope was chosen. Need to be compile not testCompile.</p>

Body:
I have this build.gradle file

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'net.serenity-bdd.aggregator'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath("net.serenity-bdd:serenity-gradle-plugin:1.4.0")
    }
}

dependencies {
    testCompile('net.serenity-bdd:serenity-core:1.4.0')
    testCompile('net.serenity-bdd:serenity-junit:1.4.0')
    testCompile('junit:junit:4.12')
    testCompile('org.assertj:assertj-core:1.7.0')
    testCompile('org.slf4j:slf4j-simple:1.7.7')

}

gradle.startParameter.continueOnFailure = true

It loads serenity libraries to the project BUT some classes are not visible when compile:
PageObject class is not visible

but it truly exists in External libs:

Class in the libs

What should I do to make it be visible in test classes and page object classes?
Also when I import manually by entering full path to the library then gradle runner does not see those classes anyway.


问:
Change color for saturday and sunday date field in tree view odoo 9
Is it possible change color line for saturday and sunday date field in tree view?</p>

答:
You can define a color field in your model and assign it to red when it's Sunday or Saturday.

class YourClass(models.Model):

    @api.one
    def _get_color(self):
        #if it's saturday or sunday
             self.color = "red"

    color = fields.Char("Color", compute=_get_color)
```

<p>Then, you have to modify a little your tree view.</p>

<pre class="lang-xml prettyprint-override"><code><tree string="Your tree view" colors="red:color=='red'">
    <field name="color" invisible="True"/>
    ...
</tree>
</code>```

After that, the row in a tree view would be printed as red.

Hope, that will help.

Body:
You can define a color field in your model and assign it to red when it's Sunday or Saturday.

class YourClass(models.Model):

    @api.one
    def _get_color(self):
        #if it's saturday or sunday
             self.color = "red"

    color = fields.Char("Color", compute=_get_color)

Then, you have to modify a little your tree view.

<tree string="Your tree view" colors="red:color=='red'">
    <field name="color" invisible="True"/>
    ...
</tree>

After that, the row in a tree view would be printed as red.

Hope, that will help.

ContentLicense: CC BY-SA 4.0


问:
How to find who created a AWS RDS DB instance?
I want to find who created a particular AWS RDS DB instance. I tried two ways:

Using aws rds describe-db-instances command line

Using AWS console https://console.aws.amazon.com/rds/

Is there a way to find out this or not?
If yes, Can anyone help to find out the createdBy flag?

答:
If it was created in the last 7 days and if you have CloudTrail enabled, you can get the information you want (who created the RDS instance) in CloudTrail dashboard.

If it was created more than 7 days ago and if have CloudTrail logs (S3 bucket) enabled, then you can download the logs (compressed json files) and look for the event that creates the DB instance which will also have the user who created it.


问:
RX Java with Retrofit-2 VS Normal Retrofit-2
I am currently using Retrofit2 for API parsing. As I was asked to change it with RxJava + Retrofit for my new application. How can I achieve this. What is the benefits of using RxJava along with Retrofit.

Any help should be a greatly appreciated.

Below is the code I am using for normal Retrofit parsing

```<code>Retrofit.Builder builder =new Retrofit.Builder().baseUrl(API_BASE_URL).client(httpClient).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
</code>```

答:
As I was asked to change it with RxJava + Retrofit for my new application. How can I achieve this.

Add com.squareup.retrofit2:adapter-rxjava2 as a dependency in your project and configure it on your Retrofit.Builder:

```<code>addCallAdapterFactory(
    RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
</code>```

Then you can change your Retrofit interfaces from

```<code>Call<ReturnType> op(...)
</code>```

to

```<code>Observable<ReturnType> op(...)
</code>```

and instead of enqueue()ing the Call, subscribe the observable to get your requests flying.

What is the benefits of using RxJava along with Retrofit

Retrofit service API calls integrate nicely with other rxjava code in your application. If you're not using rxjava elsewhere in your application, there's little benefit.

Body:
I am currently using Retrofit2 for API parsing. As I was asked to change it with RxJava + Retrofit for my new application. How can I achieve this. What is the benefits of using RxJava along with Retrofit.

Any help should be a greatly appreciated.

Below is the code I am using for normal Retrofit parsing

Retrofit.Builder builder =new Retrofit.Builder().baseUrl(API_BASE_URL).client(httpClient).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);

Body:
As I was asked to change it with RxJava + Retrofit for my new application. How can I achieve this.

Add com.squareup.retrofit2:adapter-rxjava2 as a dependency in your project and configure it on your Retrofit.Builder:

addCallAdapterFactory(
    RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))

Then you can change your Retrofit interfaces from

Call<ReturnType> op(...)

to

Observable<ReturnType> op(...)

and instead of enqueue()ing the Call, subscribe the observable to get your requests flying.

What is the benefits of using RxJava along with Retrofit

Retrofit service API calls integrate nicely with other rxjava code in your application. If you're not using rxjava elsewhere in your application, there's little benefit.


问:
Navbar link scroll to top
What I want to do is that when "Home" is clicked on the navbar, it should scroll to the top of the page, but I can't seem to get anything working. I'm to get it to work on the same .html so I'm using  on the same page</p>

```<code><nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container text-center">
        <div id="navbar" class="navbar-collapse">
            <ul class="nav navbar-nav">
                <li><a href="#home">Home</a></li>
                <li><a href="#feedback">Feedback</a></li>
                <li><a href="#about">About me</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </div>
    </div>
</nav>

<script>
    $("#home").on("click", function() {
        $("body").scrollTop(0);
    });
</script>
</code>```

答:
Yes this code won't work unfortunately, try:

```<code>$("a[href='#home']").click(function(){
    $('html, body').animate({scrollTop : 0},800);
    return false;
});
</code>```

Best wishes

Title: Navbar link scroll to top

Body:
What I want to do is that when "Home" is clicked on the navbar, it should scroll to the top of the page, but I can't seem to get anything working. I'm to get it to work on the same .html so I'm using  on the same page

Home

Feedback

About me

Contact

Body:
Yes this code won't work unfortunately, try:

$("a[href='#home']").click(function(){
    $('html, body').animate({scrollTop : 0},800);
    return false;
});

Best wishes


答: I dont think body have scroll bar. You need to find the div containts the scroll bar. Just simple right click the scroll bar and Inspec it

Title: Navbar link scroll to top


问:
OpenMP slow private function
I want to use OpenMP to parallelize a for-loop inside a function that is called from the main in c++.

My code runs much slower than in sequential mode: The for-loop takes about 6.1s (wall-clock) without OpenMP (just commenting out the #pragma... command), and 11.8s with OpenMP.

My machine has 8 CPUs and 8183Mb of physical storage and is equipped with a 64 bit windows 7 Operating System. I use the visual studio compiler for a 64x bit system in debug mode.

I have read that performance degradation might be due to variables that should be declared as private, but I am unsure how to do this correctly and which of the variables need to be declared as private.

This is the relevant for-loop:

```<code>vec DecenterOffsetParallel(const real_1d_array &x22, vec vDistance, double dOffsetXp, double dOffsetYp, double dOffsetXm, double dOffsetYm, double dOffsetXpY, double dOffsetYpX, double dOffsetXmY, double dOffsetYmX, double* dDeltaXp, double* dDeltaYp, double* dDeltaXm, double* dDeltaYm, double* dDeltaXpY, double* dDeltaYpX, double* dDeltaXmY, double* dDeltaYmX, double* delta0, /*local variables for the parallel code: */ const int nRaysn, double PupilDian, mat mRxNn, mat mRyNn, mat mRzNn, mat mRxN1n, mat mRyN1n, mat mRzN1n, mat mRxN2n, mat mRyN2n, mat mRzN2n, mat mRxN3n, mat mRyN3n, mat mRzN3n, mat mRcxNn, mat mRcyNn, mat mRczNn, mat mRcxN1n, mat mRcyN1n, mat mRczN1n, mat mRcxN2n, mat mRcyN2n, mat mRczN2n, mat mRcxN3n, mat mRcyN3n, mat mRczN3n, mat mPathNn, mat mPath1Nn, mat mPath00Nn, mat mPathN1n, mat mPath1N1n, mat mPath00N1n, mat mPathN2n, mat mPath1N2n, mat mPath00N2n, mat mPathN3n, mat mPath1N3n, mat mPath00N3n)
{
 #pragma omp parallel for
for (int xy = 0; xy < nRaysn * nRaysn; xy++){
    mat temp = mat(nRaysn, nRaysn);
    mat mScxn(nRaysn, nRaysn);
    mat mScyn(nRaysn, nRaysn);
    mat mSczn(nRaysn, nRaysn);

    int i = xy / nRaysn;
    int j = xy % nRaysn;
    // only rays inside entrance pupil:t
    if (sqrt(((10.0 / nRaysn) * i - 5.0)*((10.0 / nRaysn) * i - 5.0) + ((10.0 / nRaysn)*j - 5.0) *((10.0 / nRaysn)*j - 5.0)) <= PupilDian / 2.0){
        // Initialize the matrices
        mRxNn(i, j) = (10.0 / nRaysn) * i - 5.0;
        mRyNn(i, j) = (10.0 / nRaysn) * j - 5.0;
        mRzNn(i, j) = 0.0;
        //... everything is repeated 3 more times to simulate all in all 4 cases...: mRxNn1(i,j) = (10.0/nRaysn)*i-5.0; and so on...

        mRcxNn(i, j) = sign(vDistance(0)) *(mRxNn(i, j) - dOffsetYmX) / (sqrt(vDistance(0)*vDistance(0) + (mRxNn(i, j) - dOffsetYmX) * (mRxNn(i, j) - dOffsetYmX) + (mRyNn(i, j) - dOffsetYm) *(mRyNn(i, j) - dOffsetYm)));
        mRcyNn(i, j) = sign(vDistance(0)) *(mRyNn(i, j) - dOffsetYm) / (sqrt(vDistance(0)*vDistance(0) + (mRxNn(i, j) - dOffsetYmX) * (mRxNn(i, j) - dOffsetYmX) + (mRyNn(i, j) - dOffsetYm) *(mRyNn(i, j) - dOffsetYm)));
        mRczNn(i, j) = sqrt(1 - mRcxNn(i, j)*mRcxNn(i, j) - mRcyNn(i, j)*mRcyNn(i, j));
        mPathNn(i, j) = 0.0;
        mPath1Nn(i, j) = sign(vDistance(0)) *nAir * vDistance(0) / mRczNn(i, j);
        mPath00Nn(i, j) = mPath1Nn(i, j);
        //... everything is repeated 3 more times to simulate 4 different cases...

        //trace rays through cornea
        temp(i, j) = RayIntersect(ZernAnt, ZernRadAnt, &mRxNn(i, j), &mRyNn(i, j), P2DAnt, UAnt, VAnt, &mRzNn(i, j), mRcxNn(i, j), mRcyNn(i, j), mRczNn(i, j), &mPathNn(i, j), xNullAnt, yNullAnt, NknotsUAnt, NknotsVAnt); // find the intersection (modifies mRz, mRy, mRx, mPath)
        mPathNn(i, j) = mPath1Nn(i, j) + nAir*mPathNn(i, j);
        temp(i, j) = Surface(P2DAnt, UAnt, VAnt, ZernAnt, ZernRadAnt, mRxNn(i, j), mRyNn(i, j), mRzNn(i, j), &mScxn(i, j), &mScyn(i, j), &mSczn(i, j), KnotIntervallSizeAnt, xNullAnt, yNullAnt);
        // *Ant are identical for all four cases!
        temp(i, j) = Refract(nAir, nCornea, &mRcxNn(i, j), &mRcyNn(i, j), &mRczNn(i, j), mScxn(i, j), mScyn(i, j), mSczn(i, j));
        //... everything is repeated 3 more times to simulate all in all 4 cases...
    }
    else{
        mRxNn(i, j) = mRyNn(i, j) = mRzNn(i, j) = mRcxNn(i, j) = mRcyNn(i, j) = mRczNn(i, j) = mPathNn(i, j) = mPath1Nn(i, j) = NAN;
        //... everything is repeated 3 more times to simulate all in all 4 cases...
    }
}
// some other stuff, that is not relevant to the questions...
</code>```

}

Can anyone give me a hint, what might cause the performance degradation?
Thank you!

PS: Armadillo library is used for the matrices and vectors.

答:
You don't need to declare any private variables in the parallel construct because all the variables are either read-only (nS, Dia, etc.) or need to be shared (mX, mY, etc.).

The question about performance degradation, you should provide more information as @Zulan explained in the comments. A very useful metric here would be how much times your function gets called and which portion of the total time of the application it takes (to check that this is actually a hotspot). Having application execution time and accumulated function time would be great.

You can do this with many tools such as gprof's call graph, but run it without OpenMP.

Title: OpenMP slow private function

Body:
I want to use OpenMP to parallelize a for-loop inside a function that is called from the main in c++.

My code runs much slower than in sequential mode: The for-loop takes about 6.1s (wall-clock) without OpenMP (just commenting out the #pragma... command), and 11.8s with OpenMP.

My machine has 8 CPUs and 8183Mb of physical storage and is equipped with a 64 bit windows 7 Operating System. I use the visual studio compiler for a 64x bit system in debug mode.

I have read that performance degradation might be due to variables that should be declared as private, but I am unsure how to do this correctly and which of the variables need to be declared as private.

This is the relevant for-loop:

vec DecenterOffsetParallel(const real_1d_array &x22, vec vDistance, double dOffsetXp, double dOffsetYp, double dOffsetXm, double dOffsetYm, double dOffsetXpY, double dOffsetYpX, double dOffsetXmY, double dOffsetYmX, double* dDeltaXp, double* dDeltaYp, double* dDeltaXm, double* dDeltaYm, double* dDeltaXpY, double* dDeltaYpX, double* dDeltaXmY, double* dDeltaYmX, double* delta0, /*local variables for the parallel code: */ const int nRaysn, double PupilDian, mat mRxNn, mat mRyNn, mat mRzNn, mat mRxN1n, mat mRyN1n, mat mRzN1n, mat mRxN2n, mat mRyN2n, mat mRzN2n, mat mRxN3n, mat mRyN3n, mat mRzN3n, mat mRcxNn, mat mRcyNn, mat mRczNn, mat mRcxN1n, mat mRcyN1n, mat mRczN1n, mat mRcxN2n, mat mRcyN2n, mat mRczN2n, mat mRcxN3n, mat mRcyN3n, mat mRczN3n, mat mPathNn, mat mPath1Nn, mat mPath00Nn, mat mPathN1n, mat mPath1N1n, mat mPath00N1n, mat mPathN2n, mat mPath1N2n, mat mPath00N2n, mat mPathN3n, mat mPath1N3n, mat mPath00N3n)
{
 #pragma omp parallel for
for (int xy = 0; xy < nRaysn * nRaysn; xy++){
    mat temp = mat(nRaysn, nRaysn);
    mat mScxn(nRaysn, nRaysn);
    mat mScyn(nRaysn, nRaysn);
    mat mSczn(nRaysn, nRaysn);

    int i = xy / nRaysn;
    int j = xy % nRaysn;
    // only rays inside entrance pupil:t
    if (sqrt(((10.0 / nRaysn) * i - 5.0)*((10.0 / nRaysn) * i - 5.0) + ((10.0 / nRaysn)*j - 5.0) *((10.0 / nRaysn)*j - 5.0)) <= PupilDian / 2.0){
        // Initialize the matrices
        mRxNn(i, j) = (10.0 / nRaysn) * i - 5.0;
        mRyNn(i, j) = (10.0 / nRaysn) * j - 5.0;
        mRzNn(i, j) = 0.0;
        //... everything is repeated 3 more times to simulate all in all 4 cases...: mRxNn1(i,j) = (10.0/nRaysn)*i-5.0; and so on...

        mRcxNn(i, j) = sign(vDistance(0)) *(mRxNn(i, j) - dOffsetYmX) / (sqrt(vDistance(0)*vDistance(0) + (mRxNn(i, j) - dOffsetYmX) * (mRxNn(i, j) - dOffsetYmX) + (mRyNn(i, j) - dOffsetYm) *(mRyNn(i, j) - dOffsetYm)));
        mRcyNn(i, j) = sign(vDistance(0)) *(mRyNn(i, j) - dOffsetYm) / (sqrt(vDistance(0)*vDistance(0) + (mRxNn(i, j) - dOffsetYmX) * (mRxNn(i, j) - dOffsetYmX) + (mRyNn(i, j) - dOffsetYm) *(mRyNn(i, j) - dOffsetYm)));
        mRczNn(i, j) = sqrt(1 - mRcxNn(i, j)*mRcxNn(i, j) - mRcyNn(i, j)*mRcyNn(i, j));
        mPathNn(i, j) = 0.0;
        mPath1Nn(i, j) = sign(vDistance(0)) *nAir * vDistance(0) / mRczNn(i, j);
        mPath00Nn(i, j) = mPath1Nn(i, j);
        //... everything is repeated 3 more times to simulate 4 different cases...

        //trace rays through cornea
        temp(i, j) = RayIntersect(ZernAnt, ZernRadAnt, &mRxNn(i, j), &mRyNn(i, j), P2DAnt, UAnt, VAnt, &mRzNn(i, j), mRcxNn(i, j), mRcyNn(i, j), mRczNn(i, j), &mPathNn(i, j), xNullAnt, yNullAnt, NknotsUAnt, NknotsVAnt); // find the intersection (modifies mRz, mRy, mRx, mPath)
        mPathNn(i, j) = mPath1Nn(i, j) + nAir*mPathNn(i, j);
        temp(i, j) = Surface(P2DAnt, UAnt, VAnt, ZernAnt, ZernRadAnt, mRxNn(i, j), mRyNn(i, j), mRzNn(i, j), &mScxn(i, j), &mScyn(i, j), &mSczn(i, j), KnotIntervallSizeAnt, xNullAnt, yNullAnt);
        // *Ant are identical for all four cases!
        temp(i, j) = Refract(nAir, nCornea, &mRcxNn(i, j), &mRcyNn(i, j), &mRczNn(i, j), mScxn(i, j), mScyn(i, j), mSczn(i, j));
        //... everything is repeated 3 more times to simulate all in all 4 cases...
    }
    else{
        mRxNn(i, j) = mRyNn(i, j) = mRzNn(i, j) = mRcxNn(i, j) = mRcyNn(i, j) = mRczNn(i, j) = mPathNn(i, j) = mPath1Nn(i, j) = NAN;
        //... everything is repeated 3 more times to simulate all in all 4 cases...
    }
}
// some other stuff, that is not relevant to the questions...

}

Can anyone give me a hint, what might cause the performance degradation?
Thank you!

PS: Armadillo library is used for the matrices and vectors.


问:
How to convert number to string - VBA
I'm trying to conver a number value to a strin ex. convert 10 to "10"

I tried

```<code>    With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .NumberFormat = "0" ' Or .NumberFormat = "@"
   End With
</code>```

But it us not converting correctly. Any ideas?

答:
Try selecting your range first. So:

```<code>   With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .Select
      .Selection.NumberFormat = "@"
   End With
</code>```

Hope this helps.

Title: How to convert number to string - VBA

Body:
I'm trying to conver a number value to a strin ex. convert 10 to "10"

I tried

    With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .NumberFormat = "0" ' Or .NumberFormat = "@"
   End With

But it us not converting correctly. Any ideas?

Body:
Try selecting your range first. So:

   With Range("B2:B" & NewResizeRange)
      .Value = Range("B2:B" & NewResizeRange).Value
      .Select
      .Selection.NumberFormat = "@"
   End With

Hope this helps.


问:
Show view fails silently when its prop actions is populated with a class defined component
The following code makes the Show view to appear empty (completely white), with no errors in the browser's console nor in the webpack development server console:

```<code>const actionStyle = {
    zIndex: 2,
    display: 'inline-block',
    float: 'right',
};

class ActionButtons extends React.Component{
  render(){
    const {basePath, data, refresh} = this.props;
    return (
      <CardActions style={actionStyle}>
        <FlatButton secondary label="Process" icon={<ProcessIcon />} />
        <ListButton basePath={basePath} record={data} />
        <FlatButton primary label="Refresh" onClick={refresh} icon={<RefreshIcon />} />
      </CardActions>
    );
  }
}

const ShowContactRequest = (props) => (
    <Show title={<RecordTitle />} actions={<ActionButtons />} {...props}>
      <SomeOtherComponentImportedFromSomeWhere />
    </Show>
);
</code>```

When the component ActionButtons is provided via flat function, the Show view works with no issues.

I need to create the actions component as a class so I can access the redux store.

Any clue what I'm doing wrong?

Thanks.

答: I found the problem, I wasn't exporting the component properly. Fixed now, thanks.</p>

Body:
The following code makes the Show view to appear empty (completely white), with no errors in the browser's console nor in the webpack development server console:

const actionStyle = {
    zIndex: 2,
    display: 'inline-block',
    float: 'right',
};

class ActionButtons extends React.Component{
  render(){
    const {basePath, data, refresh} = this.props;
    return (
      <CardActions style={actionStyle}>
        <FlatButton secondary label="Process" icon={<ProcessIcon />} />
        <ListButton basePath={basePath} record={data} />
        <FlatButton primary label="Refresh" onClick={refresh} icon={<RefreshIcon />} />
      </CardActions>
    );
  }
}

const ShowContactRequest = (props) => (
    <Show title={<RecordTitle />} actions={<ActionButtons />} {...props}>
      <SomeOtherComponentImportedFromSomeWhere />
    </Show>
);

When the component ActionButtons is provided via flat function, the Show view works with no issues.

I need to create the actions component as a class so I can access the redux store.

Any clue what I'm doing wrong?

Thanks.


问:
Large text is going back of Imageview

```<code> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:id="@+id/Chartimg"
        android:layout_margin="5dp"
        android:padding="5dp" />
     <TextView
        android:text="Medium Textcbvvvccgcvcvcbcvbvvbcvbvcbvcb"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Chartimg"
        android:id="@+id/InfoView_ItemName"
        android:layout_centerVertical="true"
        android:ellipsize="end"
        android:maxLines="1" />        <TextView
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Chartimg"
        android:id="@+id/IvItemID"
        android:layout_below="@id/InfoView_ItemName" />
    <ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_next_item"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scaleType="centerInside" />
</RelativeLayout>
 </FrameLayout>
</code>```

I am creating litview when the listview item have large text it is going back to the image view here I am posting my code. please give me any solution but

答:
you should align text left of that image view

i.e
firstly provide id to image view

```<code><ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_next_item"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:id="@+id/iv_arrow"
        android:scaleType="centerInside" />
</code>```

and then add this id to text view and add make text view to left of image view i.e

```<code><TextView
        android:text="Medium Textcbvvvccgcvcvcbcvbvvbcvbvcbvcb"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Chartimg"
        android:layout_toLeftOf="@id/iv_arrow"
        android:id="@+id/InfoView_ItemName"
        android:layout_centerVertical="true"
        android:ellipsize="end"
        android:maxLines="1" />
</code>```

if you have any doubt please ask

Title: Large text is going back of Imageview

Body:
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:id="@+id/Chartimg"
        android:layout_margin="5dp"
        android:padding="5dp" />
     <TextView
        android:text="Medium Textcbvvvccgcvcvcbcvbvvbcvbvcbvcb"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Chartimg"
        android:id="@+id/InfoView_ItemName"
        android:layout_centerVertical="true"
        android:ellipsize="end"
        android:maxLines="1" />        <TextView
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Chartimg"
        android:id="@+id/IvItemID"
        android:layout_below="@id/InfoView_ItemName" />
    <ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_next_item"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scaleType="centerInside" />
</RelativeLayout>
 </FrameLayout>

I am creating litview when the listview item have large text it is going back to the image view here I am posting my code. please give me any solution but

Body:
you should align text left of that image view

i.e
firstly provide id to image view

<ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_next_item"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:id="@+id/iv_arrow"
        android:scaleType="centerInside" />

and then add this id to text view and add make text view to left of image view i.e

<TextView
        android:text="Medium Textcbvvvccgcvcvcbcvbvvbcvbvcbvcb"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/Chartimg"
        android:layout_toLeftOf="@id/iv_arrow"
        android:id="@+id/InfoView_ItemName"
        android:layout_centerVertical="true"
        android:ellipsize="end"
        android:maxLines="1" />

if you have any doubt please ask


问:
Pyspark - ValueError: could not convert string to float / invalid literal for float()
I am trying to use data from a spark dataframe as the input for my k-means model. However I keep getting errors. (Check section after code)

My spark dataframe and looks like this (and has around 1M rows):

```<code>ID            col1           col2        Latitude         Longitude
13            ...            ...           22.2             13.5
62            ...            ...           21.4             13.8
24            ...            ...           21.8             14.1
71            ...            ...           28.9             18.0
...           ...            ...           ....             ....
</code>```

Here is my code:

```<code>from pyspark.ml.clustering import KMeans
from pyspark.ml.linalg import Vectors

df = spark.read.csv("file.csv")

spark_rdd = df.rdd.map(lambda row: (row["ID"], Vectors.dense(row["Latitude"],row["Longitude"])))
feature_df = spark_rdd.toDF(["ID", "features"])

kmeans = KMeans().setK(2).setSeed(1)
model = kmeans.fit(feature_df)

sum_of_square_error = model.computeCost(feature_df)
    print str(sum_of_square_error)

centers = model.clusterCenters()

for center in centers:
    print(center)
</code>```

However, I get the error:

```<code>Py4JJavaError                             Traceback (most recent call last)
<ipython-input-145-f50a6cbe7243> in <module>()
      7
      8 kmeans = KMeans().setK(2).setSeed(1)
----> 9 model = kmeans.fit(feature_df)
     10
     11

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/base.py in fit(self, dataset, params)
     62                 return self.copy(params)._fit(dataset)
     63             else:
---> 64                 return self._fit(dataset)
     65         else:
     66             raise ValueError("Params must be either a param map or a list/tuple of param maps, "

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/wrapper.py in _fit(self, dataset)
    234
    235     def _fit(self, dataset):
--> 236         java_model = self._fit_java(dataset)
    237         return self._create_model(java_model)
    238

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/wrapper.py in _fit_java(self, dataset)
    231         """
    232         self._transfer_params_to_java()
--> 233         return self._java_obj.fit(dataset._jdf)
    234
    235     def _fit(self, dataset):

/usr/local/lib/python2.7/dist-packages/py4j/java_gateway.pyc in __call__(self, *args)
   1131         answer = self.gateway_client.send_command(command)
   1132         return_value = get_return_value(
-> 1133             answer, self.gateway_client, self.target_id, self.name)
   1134
   1135         for temp_arg in temp_args:

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/sql/utils.pyc in deco(*a, **kw)
     61     def deco(*a, **kw):
     62         try:
---> 63             return f(*a, **kw)
     64         except py4j.protocol.Py4JJavaError as e:
     65             s = e.java_exception.toString()

/usr/local/lib/python2.7/dist-packages/py4j/protocol.pyc in get_return_value(answer, gateway_client, target_id, name)
    317                 raise Py4JJavaError(
    318                     "An error occurred while calling {0}{1}{2}.\n".
--> 319                     format(target_id, ".", name), value)
    320             else:
    321                 raise Py4JError(

Py4JJavaError: An error occurred while calling o3552.fit.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 5 in stage 457.0 failed 4 times, most recent failure: Lost task 5.3 in stage 457.0 (TID 2308, 10.3.1.31, executor 1): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 174, in main
    process()
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 169, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/serializers.py", line 268, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-145-f50a6cbe7243>", line 4, in <lambda>
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 790, in dense
    return DenseVector(elements)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 275, in __init__
    ar = np.array(ar, dtype=np.float64)
ValueError: could not convert string to float: GOLF

    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:193)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:156)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:152)
    at org.apache.spark.InterruptibleIterator.next(InterruptibleIterator.scala:43)
    at scala.collection.Iterator$$anon$12.nextCur(Iterator.scala:434)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:440)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
    at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
    at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:377)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.storage.memory.MemoryStore.putIteratorAsValues(MemoryStore.scala:215)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:957)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.doPut(BlockManager.scala:888)
    at org.apache.spark.storage.BlockManager.doPutIterator(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.getOrElseUpdate(BlockManager.scala:694)
    at org.apache.spark.rdd.RDD.getOrCompute(RDD.scala:334)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:285)
    at org.apache.spark.rdd.ZippedPartitionsRDD2.compute(ZippedPartitionsRDD.scala:89)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1435)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1423)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1422)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1422)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at scala.Option.foreach(Option.scala:257)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1650)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1605)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1594)
    at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:628)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1918)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1931)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1944)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1958)
    at org.apache.spark.rdd.RDD.count(RDD.scala:1157)
    at org.apache.spark.rdd.RDD$$anonfun$takeSample$1.apply(RDD.scala:567)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
    at org.apache.spark.rdd.RDD.takeSample(RDD.scala:556)
    at org.apache.spark.mllib.clustering.KMeans.initKMeansParallel(KMeans.scala:353)
    at org.apache.spark.mllib.clustering.KMeans.runAlgorithm(KMeans.scala:256)
    at org.apache.spark.mllib.clustering.KMeans.run(KMeans.scala:227)
    at org.apache.spark.ml.clustering.KMeans.fit(KMeans.scala:319)
    at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 174, in main
    process()
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 169, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/serializers.py", line 268, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-145-f50a6cbe7243>", line 4, in <lambda>
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 790, in dense
    return DenseVector(elements)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 275, in __init__
    ar = np.array(ar, dtype=np.float64)
ValueError: could not convert string to float: GOLFE

    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:193)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:156)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:152)
    at org.apache.spark.InterruptibleIterator.next(InterruptibleIterator.scala:43)
    at scala.collection.Iterator$$anon$12.nextCur(Iterator.scala:434)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:440)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
    at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
    at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:377)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.storage.memory.MemoryStore.putIteratorAsValues(MemoryStore.scala:215)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:957)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.doPut(BlockManager.scala:888)
    at org.apache.spark.storage.BlockManager.doPutIterator(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.getOrElseUpdate(BlockManager.scala:694)
    at org.apache.spark.rdd.RDD.getOrCompute(RDD.scala:334)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:285)
    at org.apache.spark.rdd.ZippedPartitionsRDD2.compute(ZippedPartitionsRDD.scala:89)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more
</code>```

The strange thing is that the error is different every time I run it. The 3 types of errors I get are:

UnicodeEncodeError: 'decimal' codec can't encode characters in position 3-5: invalid decimal Unicode string

invalid literal for float(): 2017-04

ValueError: could not convert string to float: GOLF

Correct me if I am wrong, but I think some value of data in the columns may be incorrect (eg. occasional strings inside latitude and longitude column)

Is there a way to check if the value in each row of 'Latitude' is in fact a float? Is there a way to check if the value in each row of 'ID' is an integer?
I would like to discard the rows which contain values of the incorrect data type. Perhaps there a way of doing this using df.filter()?

I would greatly appreciate any help. Thanks.

UPDATE: I even tried df.describe('ID', 'Latitude', 'Longitude').show() and it returns numeric values for count, mean, stddev, min, max values for each column, indicating to me that they must all be numbers..?

答:
you should maybe have continued on the same thread since it's the same problem. For reference : Preprocessing data in pyspark

Here you need to convert Latitude / Longitude to float and remove null values with dropna before injecting the data in Kmean, because it seems these columns contain some strings that cannot be cast to a numeric value, so preprocess df with something like :

```<code>df2 = (df
       .withColumn("Latitude", col("Latitude").cast("float"))
       .withColumn("Longitude", col("Longitude").cast("float"))
       .dropna()
       )

spark_rdd = df2.rdd ...
</code>```

Body:
I am trying to use data from a spark dataframe as the input for my k-means model. However I keep getting errors. (Check section after code)

My spark dataframe and looks like this (and has around 1M rows):

ID            col1           col2        Latitude         Longitude
13            ...            ...           22.2             13.5
62            ...            ...           21.4             13.8
24            ...            ...           21.8             14.1
71            ...            ...           28.9             18.0
...           ...            ...           ....             ....

Here is my code:

from pyspark.ml.clustering import KMeans
from pyspark.ml.linalg import Vectors

df = spark.read.csv("file.csv")

spark_rdd = df.rdd.map(lambda row: (row["ID"], Vectors.dense(row["Latitude"],row["Longitude"])))
feature_df = spark_rdd.toDF(["ID", "features"])

kmeans = KMeans().setK(2).setSeed(1)
model = kmeans.fit(feature_df)

sum_of_square_error = model.computeCost(feature_df)
    print str(sum_of_square_error)

centers = model.clusterCenters()

for center in centers:
    print(center)

However, I get the error:

Py4JJavaError                             Traceback (most recent call last)
<ipython-input-145-f50a6cbe7243> in <module>()
      7
      8 kmeans = KMeans().setK(2).setSeed(1)
----> 9 model = kmeans.fit(feature_df)
     10
     11

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/base.py in fit(self, dataset, params)
     62                 return self.copy(params)._fit(dataset)
     63             else:
---> 64                 return self._fit(dataset)
     65         else:
     66             raise ValueError("Params must be either a param map or a list/tuple of param maps, "

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/wrapper.py in _fit(self, dataset)
    234
    235     def _fit(self, dataset):
--> 236         java_model = self._fit_java(dataset)
    237         return self._create_model(java_model)
    238

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/wrapper.py in _fit_java(self, dataset)
    231         """
    232         self._transfer_params_to_java()
--> 233         return self._java_obj.fit(dataset._jdf)
    234
    235     def _fit(self, dataset):

/usr/local/lib/python2.7/dist-packages/py4j/java_gateway.pyc in __call__(self, *args)
   1131         answer = self.gateway_client.send_command(command)
   1132         return_value = get_return_value(
-> 1133             answer, self.gateway_client, self.target_id, self.name)
   1134
   1135         for temp_arg in temp_args:

~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/sql/utils.pyc in deco(*a, **kw)
     61     def deco(*a, **kw):
     62         try:
---> 63             return f(*a, **kw)
     64         except py4j.protocol.Py4JJavaError as e:
     65             s = e.java_exception.toString()

/usr/local/lib/python2.7/dist-packages/py4j/protocol.pyc in get_return_value(answer, gateway_client, target_id, name)
    317                 raise Py4JJavaError(
    318                     "An error occurred while calling {0}{1}{2}.\n".
--> 319                     format(target_id, ".", name), value)
    320             else:
    321                 raise Py4JError(

Py4JJavaError: An error occurred while calling o3552.fit.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 5 in stage 457.0 failed 4 times, most recent failure: Lost task 5.3 in stage 457.0 (TID 2308, 10.3.1.31, executor 1): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 174, in main
    process()
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 169, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/serializers.py", line 268, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-145-f50a6cbe7243>", line 4, in <lambda>
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 790, in dense
    return DenseVector(elements)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 275, in __init__
    ar = np.array(ar, dtype=np.float64)
ValueError: could not convert string to float: GOLF

    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:193)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:156)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:152)
    at org.apache.spark.InterruptibleIterator.next(InterruptibleIterator.scala:43)
    at scala.collection.Iterator$$anon$12.nextCur(Iterator.scala:434)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:440)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
    at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
    at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:377)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.storage.memory.MemoryStore.putIteratorAsValues(MemoryStore.scala:215)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:957)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.doPut(BlockManager.scala:888)
    at org.apache.spark.storage.BlockManager.doPutIterator(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.getOrElseUpdate(BlockManager.scala:694)
    at org.apache.spark.rdd.RDD.getOrCompute(RDD.scala:334)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:285)
    at org.apache.spark.rdd.ZippedPartitionsRDD2.compute(ZippedPartitionsRDD.scala:89)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1435)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1423)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1422)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1422)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at scala.Option.foreach(Option.scala:257)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1650)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1605)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1594)
    at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:628)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1918)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1931)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1944)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1958)
    at org.apache.spark.rdd.RDD.count(RDD.scala:1157)
    at org.apache.spark.rdd.RDD$$anonfun$takeSample$1.apply(RDD.scala:567)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
    at org.apache.spark.rdd.RDD.takeSample(RDD.scala:556)
    at org.apache.spark.mllib.clustering.KMeans.initKMeansParallel(KMeans.scala:353)
    at org.apache.spark.mllib.clustering.KMeans.runAlgorithm(KMeans.scala:256)
    at org.apache.spark.mllib.clustering.KMeans.run(KMeans.scala:227)
    at org.apache.spark.ml.clustering.KMeans.fit(KMeans.scala:319)
    at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 174, in main
    process()
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/worker.py", line 169, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/serializers.py", line 268, in dump_stream
    vs = list(itertools.islice(iterator, batch))
  File "<ipython-input-145-f50a6cbe7243>", line 4, in <lambda>
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 790, in dense
    return DenseVector(elements)
  File "~/Downloads/spark-2.1.0-bin-hadoop2.7/python/pyspark/ml/linalg/__init__.py", line 275, in __init__
    ar = np.array(ar, dtype=np.float64)
ValueError: could not convert string to float: GOLFE

    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:193)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:156)
    at org.apache.spark.api.python.PythonRunner$$anon$1.next(PythonRDD.scala:152)
    at org.apache.spark.InterruptibleIterator.next(InterruptibleIterator.scala:43)
    at scala.collection.Iterator$$anon$12.nextCur(Iterator.scala:434)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:440)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
    at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
    at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:377)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.storage.memory.MemoryStore.putIteratorAsValues(MemoryStore.scala:215)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:957)
    at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.doPut(BlockManager.scala:888)
    at org.apache.spark.storage.BlockManager.doPutIterator(BlockManager.scala:948)
    at org.apache.spark.storage.BlockManager.getOrElseUpdate(BlockManager.scala:694)
    at org.apache.spark.rdd.RDD.getOrCompute(RDD.scala:334)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:285)
    at org.apache.spark.rdd.ZippedPartitionsRDD2.compute(ZippedPartitionsRDD.scala:89)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more

The strange thing is that the error is different every time I run it. The 3 types of errors I get are:

UnicodeEncodeError: 'decimal' codec can't encode characters in position 3-5: invalid decimal Unicode string

invalid literal for float(): 2017-04

ValueError: could not convert string to float: GOLF

Correct me if I am wrong, but I think some value of data in the columns may be incorrect (eg. occasional strings inside latitude and longitude column)

Is there a way to check if the value in each row of 'Latitude' is in fact a float? Is there a way to check if the value in each row of 'ID' is an integer?
I would like to discard the rows which contain values of the incorrect data type. Perhaps there a way of doing this using df.filter()?

I would greatly appreciate any help. Thanks.

UPDATE: I even tried df.describe('ID', 'Latitude', 'Longitude').show() and it returns numeric values for count, mean, stddev, min, max values for each column, indicating to me that they must all be numbers..?

Body:
you should maybe have continued on the same thread since it's the same problem. For reference : Preprocessing data in pyspark

Here you need to convert Latitude / Longitude to float and remove null values with dropna before injecting the data in Kmean, because it seems these columns contain some strings that cannot be cast to a numeric value, so preprocess df with something like :

df2 = (df
       .withColumn("Latitude", col("Latitude").cast("float"))
       .withColumn("Longitude", col("Longitude").cast("float"))
       .dropna()
       )

spark_rdd = df2.rdd ...


问:
How to add my MFT at the end of the topology resolved by the media session?
I need to insert my MFT before the EVR and after the very last MFT that the media session resolved. I want to take uncompressed/decoded input and apply a blur filter to it and send it to the EVR/window to be displayed. I need to take advantage of the job media session already did, I don't want to resolve dependencies myself. I need to know what options I have in order to further investigate on google?</p>

答:
You can create a partial topology. After that you need to implement a custom topology loader. In Load method after loading the topology you can insert your MFT and insert it between the last MFT and EVR. Disconnect tem first.
Check here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb250372(v=vs.85).aspx</p>


问:
Tableview cell play video when double clicked then it will be full screen mode
I am trying to achieve play a video which is in the table view cell and it is working fine . Now i want  to play that video in full screen mode when double clicked . I am not sure, how to achieve that. And tried with  UITapGestureRecognizer when double clicked then it will be full screen mode, but I am getting the following error.

Value of type 'AVPlayer?' has no member 'view'

```<code>   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

           let videoURL = URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(url: videoURL!)

    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = cell.bounds

    cell.layer.addSublayer(playerLayer)
    player.play()

    //handle full screen mode
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
    let view = UIView(frame: cell.contentView.frame)
    view.addGestureRecognizer(tapGesture)
    //player.view.addSubview(view) // /// Value of type 'AVPlayer?' has no member 'view'

    return cell
}

 func handleTap(_ recognizer: UITapGestureRecognizer) {
      print("tap tap")
    }
</code>```

答:
Error it self clearly telling you that

Value of type 'AVPlayer?' has no member 'view'

Because AVPlayer is not subclass of UIView. And it does not and any property called view.

And the line of code player.view.addSubview(view) can not execute.

Body:
I am trying to achieve play a video which is in the table view cell and it is working fine . Now i want  to play that video in full screen mode when double clicked . I am not sure, how to achieve that. And tried with  UITapGestureRecognizer when double clicked then it will be full screen mode, but I am getting the following error.

Value of type 'AVPlayer?' has no member 'view'

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

           let videoURL = URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(url: videoURL!)

    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = cell.bounds

    cell.layer.addSublayer(playerLayer)
    player.play()

    //handle full screen mode
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
    let view = UIView(frame: cell.contentView.frame)
    view.addGestureRecognizer(tapGesture)
    //player.view.addSubview(view) // /// Value of type 'AVPlayer?' has no member 'view'

    return cell
}

 func handleTap(_ recognizer: UITapGestureRecognizer) {
      print("tap tap")
    }


问:
PHP get dimensions of SWF file
I'm trying to load an swf file and dynamically determine its height and width.  Is there something anyone knows about that can read the dimensions of the swf file using PHP?

Here's what worked for anyone who needs it:

```<code>$aInfo = getimagesize($sFile);
list($iWidth, $iHeight) = $aInfo;
</code>```

答:
use getimagesize

example:

```<code>php -r "print_r(getimagesize('http://adsatt.espn.go.com/ad/sponsors/ibm/Oct_2010/ibm0-728x90-0160.swf'));"
</code>```

```
Array
(
    [0] => 728
    [1] => 90
    [2] => 13
    [3] => width="728" height="90"
    [mime] => application/x-shockwave-flash
)
```

PS: module gd is required

Title: PHP get dimensions of SWF file

Body:
I'm trying to load an swf file and dynamically determine its height and width.  Is there something anyone knows about that can read the dimensions of the swf file using PHP?

Here's what worked for anyone who needs it:

$aInfo = getimagesize($sFile);
list($iWidth, $iHeight) = $aInfo;

Body:
use getimagesize

example:

php -r "print_r(getimagesize('http://adsatt.espn.go.com/ad/sponsors/ibm/Oct_2010/ibm0-728x90-0160.swf'));"

Array
(
    [0] => 728
    [1] => 90
    [2] => 13
    [3] => width="728" height="90"
    [mime] => application/x-shockwave-flash
)

PS: module gd is required


问:
willHideViewController not calld when application is in landscape mode
I am creating an iPhone universal application and implemented the  willHideViewController method. This method is called on the iPad portrait mode but not called on the iPad landscape mode.
Can anyone give the solutions?</p>

答:
willHideViewController is deprecated, maybe that’s the case you face that issue.

use splitViewController:willChangeToDisplayMode: beside of willHideViewController


问:
How to add a schema to a Dataset in Spark?
I am trying to load a file into spark.
If I load a normal textFile into Spark like below:

```<code>val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")
</code>```

The outcome is:

```<code>partFile: org.apache.spark.sql.Dataset[String] = [value: string]
</code>```

I can see a dataset in the output. But if I load a Json file:

```<code>val pfile = spark.read.json("hdfs://quickstart:8020/user/cloudera/pjson")
</code>```

The outcome is a dataframe with a readymade schema:

```<code>pfile: org.apache.spark.sql.DataFrame = [address: struct<city: string, state: string>, age: bigint ... 1 more field]
</code>```

The Json/parquet/orc files have schema. So I can understand that this is a feature from Spark version:2x, which made things easier as we directly get a DataFrame in this case and for a normal textFile you get a dataset where there is no schema which makes sense.
What I'd like to know is how can I add a schema to a dataset that is a resultant of loading a textFile into spark. For an RDD, there is case class/StructType option to add the schema and convert it to a DataFrame.
Could anyone let me know how can I do it ?

答:
When you use textFile, each line of the file will be a string row in your Dataset. To convert to DataFrame with a schema, you can use toDF:

val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")

import sqlContext.implicits._
val df = partFile.toDF("string_column")
```

<p>In this case, the DataFrame will have a schema of a single column of type StringType.</p>

<p>If your file contains a more complex schema, you can either use the csv reader (if the file is in a structured csv format):</p>

<pre class="lang-scala prettyprint-override"><code>val partFile = spark.read.option("header", "true").option("delimiter", ";").csv("hdfs://quickstart:8020/user/cloudera/partfile")
</code>```

Or you can process your Dataset using map, then using toDF to convert to DataFrame. For example, suppose you want one column to be the first character of the line (as an Int) and the other column to be the fourth character (also as an Int):

val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")

val processedDataset: Dataset[(Int, Int)] = partFile.map {
  line: String => (line(0).toInt, line(3).toInt)
}

import sqlContext.implicits._
val df = processedDataset.toDF("value0", "value3")
```

<p>Also, you can define a case class, which will represent the final schema for your DataFrame:</p>

<pre class="lang-scala prettyprint-override"><code>case class MyRow(value0: Int, value3: Int)

val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")

val processedDataset: Dataset[MyRow] = partFile.map {
  line: String => MyRow(line(0).toInt, line(3).toInt)
}

import sqlContext.implicits._
val df = processedDataset.toDF
</code>```

In both cases above, calling df.printSchema would show:

root
 |-- value0: integer (nullable = true)
 |-- value3: integer (nullable = true)
```

Body:
I am trying to load a file into spark.
If I load a normal textFile into Spark like below:

val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")

The outcome is:

partFile: org.apache.spark.sql.Dataset[String] = [value: string]

I can see a dataset in the output. But if I load a Json file:

val pfile = spark.read.json("hdfs://quickstart:8020/user/cloudera/pjson")

The outcome is a dataframe with a readymade schema:

pfile: org.apache.spark.sql.DataFrame = [address: struct<city: string, state: string>, age: bigint ... 1 more field]

The Json/parquet/orc files have schema. So I can understand that this is a feature from Spark version:2x, which made things easier as we directly get a DataFrame in this case and for a normal textFile you get a dataset where there is no schema which makes sense.
What I'd like to know is how can I add a schema to a dataset that is a resultant of loading a textFile into spark. For an RDD, there is case class/StructType option to add the schema and convert it to a DataFrame.
Could anyone let me know how can I do it ?

Body:
When you use textFile, each line of the file will be a string row in your Dataset. To convert to DataFrame with a schema, you can use toDF:

val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")

import sqlContext.implicits._
val df = partFile.toDF("string_column")

In this case, the DataFrame will have a schema of a single column of type StringType.

If your file contains a more complex schema, you can either use the csv reader (if the file is in a structured csv format):

val partFile = spark.read.option("header", "true").option("delimiter", ";").csv("hdfs://quickstart:8020/user/cloudera/partfile")

Or you can process your Dataset using map, then using toDF to convert to DataFrame. For example, suppose you want one column to be the first character of the line (as an Int) and the other column to be the fourth character (also as an Int):

val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")

val processedDataset: Dataset[(Int, Int)] = partFile.map {
  line: String => (line(0).toInt, line(3).toInt)
}

import sqlContext.implicits._
val df = processedDataset.toDF("value0", "value3")

Also, you can define a case class, which will represent the final schema for your DataFrame:

case class MyRow(value0: Int, value3: Int)

val partFile = spark.read.textFile("hdfs://quickstart:8020/user/cloudera/partfile")

val processedDataset: Dataset[MyRow] = partFile.map {
  line: String => MyRow(line(0).toInt, line(3).toInt)
}

import sqlContext.implicits._
val df = processedDataset.toDF

In both cases above, calling df.printSchema would show:

root
 |-- value0: integer (nullable = true)
 |-- value3: integer (nullable = true)


问:
How to insert a row in a nested table in Oracle with PreparedStatement
I created 2 types as OBJECT and a type of table and a nested table in my Oracle database. I know how to inset a row in table in cmd but I have no idea that how I can insert a row in this table with PreparedStatement in Java.
This is my table and type:

```<code> CREATE Or REPLACE TYPE item AS OBJECT
(
  id        number,
  counter   number
);/
CREATE Or REPLACE TYPE time AS OBJECT
(
  year        number,
  month number,
  second number
);
CREATE Or REPLACE TYPE Shoping_list AS TABLE OF item;
create table invoice(
id number NOT NULL PRIMARY KEY ,
list shoping_list,
dateIn date,
timeIn time,
totalCost number )
nested table list store as list_of_product;
</code>```

I can insert with this code in cmd

```<code>insert into invoice(id,list,dateIn,timeIn,totalCost) values(seq_ID_invoice.nextval,
shoping_list(item(1,2),item(26,1)),TO_DATE('1396-04-11','YYYY-MM-DD'),time(11,25,40),7000);
</code>```

and this my java code

```<code>preparedStatement = connection.prepareStatement("insert into invoice (id,list,dateIn,timeIn,totalCost) values ("+id+",?,?,?,?)");
</code>```

答:
seq_ID_invoice is Oracle Sequence. So oracle takes its value automatically. For rest you can use setInt, setString, setFloat, etc. methods available in PreparedStatement based on the types of columns defined inside your table.

Following sample code may help you.

```<code>PreparedStatement ps = connection.prepareStatement(
    "insert into invoice(id, list, dateIn, timeIn, totalCost)"+
    " values (seq_ID_invoice.nextval, shoping_list(item(?,?), item(?,?)), TO_DATE(?,'YYYY-MM-DD'), time(?, ?, ?), ?)");

ps.setInt(1, int_value_for_id_of_item_1);
ps.setInt(2, int_value_for_counter_of_item_1);
ps.setInt(3, int_value_for_id_of_item_2);
ps.setInt(4, int_value_for_counter_of_item_2);
ps.setString(5, string_value_for_date);
ps.setInt(6, int_value_for_year_of_time);
ps.setInt(7, int_value_for_month_of_time);
ps.setInt(8, int_value_for_second_of_time);
ps.setInt(9, int_bvalue_for_total_cost);

int rowCnt = ps.executeUpdate();
System.out.println(rowCnt+" rows affected.");
</code>```

int_value_for_id_of_item_1, int_value_for_counter_of_item_1, ... these are java variables. You can defined variables of your choice or you can directly mention values in place of these variables.

Body:
I created 2 types as OBJECT and a type of table and a nested table in my Oracle database. I know how to inset a row in table in cmd but I have no idea that how I can insert a row in this table with PreparedStatement in Java.
This is my table and type:

 CREATE Or REPLACE TYPE item AS OBJECT
(
  id        number,
  counter   number
);/
CREATE Or REPLACE TYPE time AS OBJECT
(
  year        number,
  month number,
  second number
);
CREATE Or REPLACE TYPE Shoping_list AS TABLE OF item;
create table invoice(
id number NOT NULL PRIMARY KEY ,
list shoping_list,
dateIn date,
timeIn time,
totalCost number )
nested table list store as list_of_product;

I can insert with this code in cmd

insert into invoice(id,list,dateIn,timeIn,totalCost) values(seq_ID_invoice.nextval,
shoping_list(item(1,2),item(26,1)),TO_DATE('1396-04-11','YYYY-MM-DD'),time(11,25,40),7000);

and this my java code

preparedStatement = connection.prepareStatement("insert into invoice (id,list,dateIn,timeIn,totalCost) values ("+id+",?,?,?,?)");

Body:
seq_ID_invoice is Oracle Sequence. So oracle takes its value automatically. For rest you can use setInt, setString, setFloat, etc. methods available in PreparedStatement based on the types of columns defined inside your table.

Following sample code may help you.

PreparedStatement ps = connection.prepareStatement(
    "insert into invoice(id, list, dateIn, timeIn, totalCost)"+
    " values (seq_ID_invoice.nextval, shoping_list(item(?,?), item(?,?)), TO_DATE(?,'YYYY-MM-DD'), time(?, ?, ?), ?)");

ps.setInt(1, int_value_for_id_of_item_1);
ps.setInt(2, int_value_for_counter_of_item_1);
ps.setInt(3, int_value_for_id_of_item_2);
ps.setInt(4, int_value_for_counter_of_item_2);
ps.setString(5, string_value_for_date);
ps.setInt(6, int_value_for_year_of_time);
ps.setInt(7, int_value_for_month_of_time);
ps.setInt(8, int_value_for_second_of_time);
ps.setInt(9, int_bvalue_for_total_cost);

int rowCnt = ps.executeUpdate();
System.out.println(rowCnt+" rows affected.");

int_value_for_id_of_item_1, int_value_for_counter_of_item_1, ... these are java variables. You can defined variables of your choice or you can directly mention values in place of these variables.


问:
Multiple array to test with mocha/chai/expect
I'm doing several test with mocha/chai, but in this case I have no ideia what I have to do.

In this array (for instance):

```<code>[ { rectangles: '1',
    rows: '1',
    cols: '1' },
  { rectangles: '1',
    rows: '2',
    cols: '1' },
  { rectangles: '4',
    rows: '2',
    cols: '2' } ]
</code>```

And I would like to test if, for instance ...

```<code>array1 : rectangles = 1;
array2 : rectangles = 1;
array3 : rectangles = 4;
</code>```

Is possible? How can I do it?

答:
I found a solution: </p>

```<code>        it('Should an for array data-rectangles 1,1,4', function(){
            var el = code.listGrids(gridSelector);
            expect(el[0]).to.own.include({rectangles: '1'})
            expect(el[1]).to.own.include({rectangles: '1'})
            expect(el[2]).to.own.include({rectangles: '4'})
        });
</code>```

<p>Super easy! =) </p>

Body:
I'm doing several test with mocha/chai, but in this case I have no ideia what I have to do.

In this array (for instance):

[ { rectangles: '1',
    rows: '1',
    cols: '1' },
  { rectangles: '1',
    rows: '2',
    cols: '1' },
  { rectangles: '4',
    rows: '2',
    cols: '2' } ]

And I would like to test if, for instance ...

array1 : rectangles = 1;
array2 : rectangles = 1;
array3 : rectangles = 4;

Is possible? How can I do it?

Body:
I found a solution:

        it('Should an for array data-rectangles 1,1,4', function(){
            var el = code.listGrids(gridSelector);
            expect(el[0]).to.own.include({rectangles: '1'})
            expect(el[1]).to.own.include({rectangles: '1'})
            expect(el[2]).to.own.include({rectangles: '4'})
        });

Super easy! =)


问:
Change foreach loop by for loop
I am creating a Fingerprint Verification System in which I have to match fingerprints using records in database.  I have used foreach loop to to so but it is taking almost 40 seconds for only 350 records.  I want to speed it up.  I want my foreach loop to convert into for loop but I am facing some difficulties in initializing the for loop.  Here is my code.

foreach (KeyValuePair<decimal, MemoryStream> tt in profileDic)
{
    this.Invoke(new Function(delegate()
    {
        textBox1.Text += "\n" + tt.Key.ToString();
    }));
    temp = new DPFP.Template(tt.Value);

    Verificator = new DPFP.Verification.Verification();
    featuresForVerification = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Verification);
    if (featuresForVerification != null)
    {
        DPFP.Verification.Verification.Result result = new DPFP.Verification.Verification.Result();
        Verificator.Verify(featuresForVerification, temp, ref result);

        #region UI Envoke
        this.Invoke(new Function(delegate()
        {
            if (result.Verified)
            {
                MessageBox.Show("FAR " + result.FARAchieved + "\n" + tt.Key.ToString());
            }
            else
            {
                //MessageBox.Show("No Match");
            }
        }));
        #endregion
    }
    this.Invoke(new Function(delegate()
    {
        progressBar1.Value += 1;
    }));
    Thread.CurrentThread.Priority = ThreadPriority.Highest;
}
```

I am confused at the first line foreach (KeyValuePair<decimal, MemoryStream> tt in profileDic).  Can someone tell me how I can iterate through every item in Dictionary object profileDic using a for loop.  I am not sure how to get KeyValuePair<decimal, MemoryStream> tt in profileDic when using a for loop.

答:
What biometric system are you using? This kind of work should be done in biometric device. But if you really need to find a person directly in database, you should not use C# collections, but database itself.

Every fingerprint has its minutiaes. There are unique features of fingerprint. There are some algorithms that transform this into storable data for example in database. This could look like md5 hash.

Next, when you have records in database with minutiaes, you can just ask database for this value.

It should work like that: you get minutias (or complete data that can be stored directly in database) and then ask database for this value, something like:

```<code>SELECT *
FROM users
WHERE fingerprint = :your_data
</code>```

Database operations are far more faster than iterating throug any collection in any way.

Title: Change foreach loop by for loop

Body:
I am creating a Fingerprint Verification System in which I have to match fingerprints using records in database.  I have used foreach loop to to so but it is taking almost 40 seconds for only 350 records.  I want to speed it up.  I want my foreach loop to convert into for loop but I am facing some difficulties in initializing the for loop.  Here is my code.

foreach (KeyValuePair<decimal, MemoryStream> tt in profileDic)
{
    this.Invoke(new Function(delegate()
    {
        textBox1.Text += "\n" + tt.Key.ToString();
    }));
    temp = new DPFP.Template(tt.Value);

    Verificator = new DPFP.Verification.Verification();
    featuresForVerification = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Verification);
    if (featuresForVerification != null)
    {
        DPFP.Verification.Verification.Result result = new DPFP.Verification.Verification.Result();
        Verificator.Verify(featuresForVerification, temp, ref result);

        #region UI Envoke
        this.Invoke(new Function(delegate()
        {
            if (result.Verified)
            {
                MessageBox.Show("FAR " + result.FARAchieved + "\n" + tt.Key.ToString());
            }
            else
            {
                //MessageBox.Show("No Match");
            }
        }));
        #endregion
    }
    this.Invoke(new Function(delegate()
    {
        progressBar1.Value += 1;
    }));
    Thread.CurrentThread.Priority = ThreadPriority.Highest;
}

I am confused at the first line foreach (KeyValuePair<decimal, MemoryStream> tt in profileDic).  Can someone tell me how I can iterate through every item in Dictionary object profileDic using a for loop.  I am not sure how to get KeyValuePair<decimal, MemoryStream> tt in profileDic when using a for loop.

Body:
What biometric system are you using? This kind of work should be done in biometric device. But if you really need to find a person directly in database, you should not use C# collections, but database itself.

Every fingerprint has its minutiaes. There are unique features of fingerprint. There are some algorithms that transform this into storable data for example in database. This could look like md5 hash.

Next, when you have records in database with minutiaes, you can just ask database for this value.

It should work like that: you get minutias (or complete data that can be stored directly in database) and then ask database for this value, something like:

SELECT *
FROM users
WHERE fingerprint = :your_data

Database operations are far more faster than iterating throug any collection in any way.