问:
Get response from API with jQuery
I'd like to type some stuff into an input field and then get a response from the API if the value exist or not.

The API will response with a true/ false value if the zipcode (12345) exist. http://192.168.100.100:8887/public?zipcode=12345

And here's my code so far:

```<code>$(function() {

    $('.positiveMessage, .negativeMessage').hide()
    $('#submitBtn').on('click', function() {
        var $zipcode = document.getElementById('searchZipCode').value
        $.getJSON({
            type: 'GET',
            url: 'http://192.168.100.100:8887/public?zipcode=' + $zipCode,
            success: result => {
                //Goes through the array
                $.each(result, function(i, info) {
                    $('body').append(info + '')
                })
            }
        })
    })
})
</code>```

As you can see I'd like to manipulate the API by adding a variable in the link.

答:
You've got some ES6 in there (the left arrow in your success function) that might not work as you're expecting. You're also using getElementById when you've already got jQuery.

It's hard to answer without the API feed but this should help you at least get a response. Use console.log to print what returns from the API, then you should be able to traverse to the data you need.

$(function() {
    $('.positiveMessage, .negativeMessage').hide()
    $('#submitBtn').on('click', function() {
        var zipCode = $('#searchZipCode').val();
        $.getJSON({
            type: 'GET',
            url: 'http://192.168.100.100:8887/public?zipcode=' + zipCode,
            success: function(result){
                if (result) {
                    $('.positiveMessage').show()
                    $('.negativeMessage').hide()
                } else {
                    $('.positiveMessage').hide()
                    $('.negativeMessage').show()
                }
            }
        })
    })
})```

Title: Get response from API with jQuery

Body:
I'd like to type some stuff into an input field and then get a response from the API if the value exist or not.

The API will response with a true/ false value if the zipcode (12345) exist. http://192.168.100.100:8887/public?zipcode=12345

And here's my code so far:

$(function() {

    $('.positiveMessage, .negativeMessage').hide()
    $('#submitBtn').on('click', function() {
        var $zipcode = document.getElementById('searchZipCode').value
        $.getJSON({
            type: 'GET',
            url: 'http://192.168.100.100:8887/public?zipcode=' + $zipCode,
            success: result => {
                //Goes through the array
                $.each(result, function(i, info) {
                    $('body').append(info + '')
                })
            }
        })
    })
})

As you can see I'd like to manipulate the API by adding a variable in the link.


来源: stackexchange

问:
How can I use listview inside a scrollview in android?
This is the image I want in my application:

I tried using listview inside a scrollview but the problem is that I am not able to stretch listview to its full size. Can anyone tell me how can I do this??

答:
You can use your recyclerview as below example :

```<code>    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_collapseMode="pin">

                <!-- Some views-->

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_bubhub_comment_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</code>```

You can use below properties in recyclerview if you need both layout to scroll. Otherwise it would work as single scrolling view. These properties allows Recyclerview to scroll inside NestedScrollView

```<code>app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
</code>```

ContentLicense: CC BY-SA 3.0

Body:
You can use your recyclerview as below example :

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_collapseMode="pin">

                <!-- Some views-->

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_bubhub_comment_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

You can use below properties in recyclerview if you need both layout to scroll. Otherwise it would work as single scrolling view. These properties allows Recyclerview to scroll inside NestedScrollView

app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"


问:
How to add a NOT NULLABLE column with a foreign key to an existing table that already contains data?
An example for this problem:

Imagine you have a table projects. The table projects already
  contains data. Now you want to add a project_type_id column to the
  table, that can't be NULL and has a foreign key to the project_types
  table.

The database sees the project_type_id can't be NULL, so it inserts a
  0 in the column for all the existing rows. The 0 does not exist in
  the project_types table, so the foreign key constraint fails. This
  means you can't add the foreign key.

I thought about adding a first "default" row to the project_types table and link all the projects to that type. This isn't a good solution, because the user has to change all the types from "default" to the correct type.

What is your solution for this problem?

答:
I think one workaround here would be to first add the new column without a foreign key reference.  Then update the values of that column, and afterwards add the foreign key constraint.  Something like this:

```<code>ALTER TABLE projects ADD COLUMN project_type_id INT;

-- now update with correct values

ALTER TABLE projects ADD CONSTRAINT fk_pt_id FOREIGN KEY (project_type_id)
REFERENCES yourTable(parent_id);
</code>```

Body:
I think one workaround here would be to first add the new column without a foreign key reference.  Then update the values of that column, and afterwards add the foreign key constraint.  Something like this:

ALTER TABLE projects ADD COLUMN project_type_id INT;

-- now update with correct values

ALTER TABLE projects ADD CONSTRAINT fk_pt_id FOREIGN KEY (project_type_id)
REFERENCES yourTable(parent_id);


问:
How to make a button not show up but still be used by javascript?
I am using a button that has to be invible and should be used by a javascript function.

```<code><asp:Button ID ="btnDummy1" runat="server" Visible ="true" OnClick="btnSubmit1_Click" width="0px" height="0px"/
</code>```

I cannot keep visible = false as it the javascript will not use invible content in the poage. I havetried to give width=0 and height=0, still it showws up in Chrome. What do you guys think i should do?

Thanks in advance :)

答:
A pretty clean approach in ASP.Net it give it a "hidden" class:

```<code><asp:Button ID ="btnDummy1" runat="server" CssClass="hidden" />
</code>```

Then in your stylesheet:

```<code>.hidden { display: none; }
</code>```

ContentLicense: CC BY-SA 2.5

Body:
I am using a button that has to be invible and should be used by a javascript function.

<asp:Button ID ="btnDummy1" runat="server" Visible ="true" OnClick="btnSubmit1_Click" width="0px" height="0px"/

I cannot keep visible = false as it the javascript will not use invible content in the poage. I havetried to give width=0 and height=0, still it showws up in Chrome. What do you guys think i should do?

Thanks in advance :)

Body:
A pretty clean approach in ASP.Net it give it a "hidden" class:

<asp:Button ID ="btnDummy1" runat="server" CssClass="hidden" />

Then in your stylesheet:

.hidden { display: none; }


问:
IONIC Application showing no internet connection
I have successfully installed cordova network plugin and no errors so far from the code side.

When I use ionic serve to get the app to run on my browser, it detects my wifi and the plugin recognizes a connection, and if i disable wifi on my computer, it again recognizes that there is no internet connection.

The problem now comes when I install the app onto an android phone after building. I even have Bluestacks on my computer but the app does not get the network from there too. Has anyone run into this problem? How did you go through it?

I do not think it is an issue with my code, so I am not posting code, unless someone will need it for clarification.

I hope someone is able to help.

Permission in androidManifest.xml

```<code><uses-permission android:name="android.permission.INTERNET" />
</code>```

config.xml do include:

```<code><access origin="*" />
</code>```

答:
Problem Solved : As Permission Issue Held Due to Permission Was Listed in Dangerous Permission.

I Lowered my TargetSDKVersion

Body:
I have successfully installed cordova network plugin and no errors so far from the code side.

When I use ionic serve to get the app to run on my browser, it detects my wifi and the plugin recognizes a connection, and if i disable wifi on my computer, it again recognizes that there is no internet connection.

The problem now comes when I install the app onto an android phone after building. I even have Bluestacks on my computer but the app does not get the network from there too. Has anyone run into this problem? How did you go through it?

I do not think it is an issue with my code, so I am not posting code, unless someone will need it for clarification.

I hope someone is able to help.

Permission in androidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

config.xml do include:

<access origin="*" />


问:
python: pd.rolling_std function result is different from the standard deviation calculator
There is dataframe as following, I want to compute the close column: volatility, for exaple window=2, namely two rows's volatility. I

```<code>   Date      close
2010-06-09    3160.0
2010-06-10    3180.0
2010-06-11    3215.0
2010-06-14    3255.0
</code>```

I  used the following code which use the function:

```<code>stdDeviation = pd.rolling_std(df['Close'],window=2)
stdDeviation.head(4)
</code>```

The result is:

```<code>      Date
2010-06-09          NaN
2010-06-10    14.142136
2010-06-11    24.748737
2010-06-14    28.284271
Name: Close, dtype: float64
</code>```

but when calculate the standard deviation by the calculator https://en.wikipedia.org/wiki/Standard_deviation

I found the first two numbers: 3160, 3180, the standard deviation of these two number is 10 which is different from 14.142136 which calculated by the function.pd.rolling_std.

Could you guy tell me more about the function rolling_std, what's the clacualtor of this function in detail. why is different, is there something wrong in my question . Thanks!

答:
pandas by default calculates the sample standard deviation. The denominator for the sample standard deviation has a correction factor so we divide the sum of squares by n-1 instead of n. 1 here is the lost degrees of freedom due to the sample mean. If you want to get the same result, you can pass ddof=0 to rolling_std which will give you 10 as the result.

```<code>stdDeviation = pd.rolling_std(df['Close'], window=2, ddof=0)
</code>```

Body:
There is dataframe as following, I want to compute the close column: volatility, for exaple window=2, namely two rows's volatility. I

   Date      close
2010-06-09    3160.0
2010-06-10    3180.0
2010-06-11    3215.0
2010-06-14    3255.0

I  used the following code which use the function:

stdDeviation = pd.rolling_std(df['Close'],window=2)
stdDeviation.head(4)

The result is:

      Date
2010-06-09          NaN
2010-06-10    14.142136
2010-06-11    24.748737
2010-06-14    28.284271
Name: Close, dtype: float64

but when calculate the standard deviation by the calculator https://en.wikipedia.org/wiki/Standard_deviation

I found the first two numbers: 3160, 3180, the standard deviation of these two number is 10 which is different from 14.142136 which calculated by the function.pd.rolling_std.

Could you guy tell me more about the function rolling_std, what's the clacualtor of this function in detail. why is different, is there something wrong in my question . Thanks!

Body:
pandas by default calculates the sample standard deviation. The denominator for the sample standard deviation has a correction factor so we divide the sum of squares by n-1 instead of n. 1 here is the lost degrees of freedom due to the sample mean. If you want to get the same result, you can pass ddof=0 to rolling_std which will give you 10 as the result.

stdDeviation = pd.rolling_std(df['Close'], window=2, ddof=0)


问:
Custom Keyboard InputAccessoryView not visible in iOS 11
I have implemented Custom input accessory view it was working fine till iOS 10.3.1. But it's not visible in iOS 11 beta.

Have anyone experience this issue?

答:
The question you ask does not have much detail. But I had the same problem when using an inputAccessoryView and a custom inputView for the textfield.

And resolved this on iOS11 by setting the custom inputView's autoresizingMask to .flexibleHeight.

```<code>yourCustomInputView.autoresizingMask = .flexibleHeight
</code>```

Hope this resolves the issue. If not maybe provide some more information?

Here is how I add the input accessory, incase this is of more help (as extension of textfield):

```<code>public extension UITextField {

public func addToolbarInputAccessoryView(barButtonItems: [UIBarButtonItem],
                                         textColour: UIColor,
                                         toolbarHeight: CGFloat = 44,
                                         backgroundColour: UIColor = .white) {

    let toolbar = UIToolbar()

    toolbar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: toolbarHeight)
    toolbar.items = barButtonItems
    toolbar.isTranslucent = false
    toolbar.barTintColor = backgroundColour
    toolbar.tintColor = textColour

    inputAccessoryView = toolbar
}
</code>```

}

And then on the inputView (not the inputAccessoryView), I was using a date picker for example - just make sure that the date picker's autoresizing mask is set to flexible height.

Body:
The question you ask does not have much detail. But I had the same problem when using an inputAccessoryView and a custom inputView for the textfield.

And resolved this on iOS11 by setting the custom inputView's autoresizingMask to .flexibleHeight.

yourCustomInputView.autoresizingMask = .flexibleHeight

Hope this resolves the issue. If not maybe provide some more information?

Here is how I add the input accessory, incase this is of more help (as extension of textfield):

public extension UITextField {

public func addToolbarInputAccessoryView(barButtonItems: [UIBarButtonItem],
                                         textColour: UIColor,
                                         toolbarHeight: CGFloat = 44,
                                         backgroundColour: UIColor = .white) {

    let toolbar = UIToolbar()

    toolbar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: toolbarHeight)
    toolbar.items = barButtonItems
    toolbar.isTranslucent = false
    toolbar.barTintColor = backgroundColour
    toolbar.tintColor = textColour

    inputAccessoryView = toolbar
}

}

And then on the inputView (not the inputAccessoryView), I was using a date picker for example - just make sure that the date picker's autoresizing mask is set to flexible height.


问:
WPF how to put separator below each item in horizontal stackpanel
I've got a horizontal stackpanel on which I add items. I would like to add a separator below each project name, rather than it being to the right of the name, and that separator should span the whole page. These 2 questions haven't helped me up to now: How to add a vertical Separator?    How can a separator be added between items in an ItemsControl

The View code is:

```<code><ListView ItemsSource="{Binding Projects}" Margin="49,188,54,0">
  <ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
      <EventSetter Event="PreviewMouseLeftButtonDown" Handler="EventSetter_OnHandler"/>
    </Style>
  </ListView.ItemContainerStyle>
  <ListView.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <ContentControl Content="{StaticResource  Appbar_Suitcase}"/>
        <Label Content="{Binding Name}"/>
        <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
      </StackPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
</code>```

答:
make the first stackpanel vertical and put the content control and the label inside of a horizontal stackpanel. something like this:</p>

```<code><StackPanel>
    <StackPanel Orientation="Horizontal">
        <ContentControl  Content="{StaticResource  Appbar_Suitcase}" />
        <Label Content="{Binding Name}"/>
    </StackPanel>

    <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
</StackPanel>
</code>```

Body:
I've got a horizontal stackpanel on which I add items. I would like to add a separator below each project name, rather than it being to the right of the name, and that separator should span the whole page. These 2 questions haven't helped me up to now: How to add a vertical Separator?    How can a separator be added between items in an ItemsControl

The View code is:

<ListView ItemsSource="{Binding Projects}" Margin="49,188,54,0">
  <ListView.ItemContainerStyle>

  </ListView.ItemContainerStyle>
  <ListView.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <ContentControl Content="{StaticResource  Appbar_Suitcase}"/>

        <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
      </StackPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

Body:
make the first stackpanel vertical and put the content control and the label inside of a horizontal stackpanel. something like this:

<StackPanel>
    <StackPanel Orientation="Horizontal">
        <ContentControl  Content="{StaticResource  Appbar_Suitcase}" />
        <Label Content="{Binding Name}"/>
    </StackPanel>

    <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
</StackPanel>


问:
Android two way binding with Integer type causes databinding does not exist
I'm having some issue with implementing two way binding with an Integer data type.

```<code>public class User {

    private String firstName;
    private String lastName;
    private int age;

    public User() {}

    public void setFirstName(String firstName) {
       this.firstName = firstName;
    }

    public String getFirstName() {
       return this.firstName;
    }

    public void setLastName(String lastName) {
       this.lastName = lastName;
    }

    public String getLastName() {
       return this.lastName;
    }

    public void setAge(int age) {
       this.age = age;
    }

    public int getAge() {
       return this.age;
    }

}
</code>```

XML:

```<code><?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="UserDataBinding">
        <variable
            name="user"
            type="com.databinding.model.User" />
    </data>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="@dimen/activity_horizontal_margin">

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.firstName}" />

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.lastName}" />

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.age}" />

    </LinearLayout>
</layout>
</code>```

Unfortunately, it gives me the error

"Error:(52, 17) Cannot find the getter for attribute 'android:text'
  with value type java.lang.Integer on
  android.support.design.widget.TextInputEditText. "

If I change the attribute text to

```<code>       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={Integer.toString(user.age)}" />
</code>```

then I get the error

"Error:cannot generate view binders java.lang.NullPointerException"

Appreciate any help on this.

UPDATE: It seems there was another error right after the error mentioned above.

cannot generate view binders java.lang.NullPointerException

Not sure why its giving me NPE even though the app hasn't started yet.

答:
Here is my solution. It's clean and simple. Simply if layout needs String, give it a String instead of int. All you have to do is create a setter and getter with String type and use them to bind to ui while normal setter and getter doing the normal thing!

A complete code !

My POJO class(Mydata.java). getAgeString and setAgeString are the ui methods, doing the conversion. Note that I put @Bindable on getAgeString. so ui will use ageString

```<code>package com.databindingnumber;

import android.databinding.BaseObservable;
import android.databinding.Bindable;

public class MyData extends BaseObservable{
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(this.age != age) {
            this.age = age;
            notifyPropertyChanged(BR.ageString);//NOTE: ui is using ageString !
        }
    }

    @Bindable
    public String getAgeString() {
        return Integer.toString(age);
    }

    public void setAgeString(String ageString) {
        try {
            int val = Integer.parseInt(ageString);
            this.setAge(val);
        }catch(NumberFormatException ex){
            this.setAge(0);//default value
        }
    }
}
</code>```

The Layout File(activity_main.xml). use normal two-way binding with @= but use ageString instead of age

```<code><?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="foo" type="com.databindingnumber.MyData"/>
    </data>

    <EditText
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:inputType="numberSigned"
        android:text="@={foo.ageString}" />
</layout>
</code>```

MainActivity.java file

```<code>public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setFoo(new MyData());
    }
}
</code>```

Hope this will help to someone!

Body:
I'm having some issue with implementing two way binding with an Integer data type.

public class User {

    private String firstName;
    private String lastName;
    private int age;

    public User() {}

    public void setFirstName(String firstName) {
       this.firstName = firstName;
    }

    public String getFirstName() {
       return this.firstName;
    }

    public void setLastName(String lastName) {
       this.lastName = lastName;
    }

    public String getLastName() {
       return this.lastName;
    }

    public void setAge(int age) {
       this.age = age;
    }

    public int getAge() {
       return this.age;
    }

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="UserDataBinding">
        <variable
            name="user"
            type="com.databinding.model.User" />
    </data>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="@dimen/activity_horizontal_margin">

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.firstName}" />

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.lastName}" />

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={user.age}" />

    </LinearLayout>
</layout>

Unfortunately, it gives me the error

"Error:(52, 17) Cannot find the getter for attribute 'android:text'
  with value type java.lang.Integer on
  android.support.design.widget.TextInputEditText. "

If I change the attribute text to

       <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={Integer.toString(user.age)}" />

then I get the error

"Error:cannot generate view binders java.lang.NullPointerException"

Appreciate any help on this.

UPDATE: It seems there was another error right after the error mentioned above.

cannot generate view binders java.lang.NullPointerException

Not sure why its giving me NPE even though the app hasn't started yet.

Body:
Here is my solution. It's clean and simple. Simply if layout needs String, give it a String instead of int. All you have to do is create a setter and getter with String type and use them to bind to ui while normal setter and getter doing the normal thing!

A complete code !

My POJO class(Mydata.java). getAgeString and setAgeString are the ui methods, doing the conversion. Note that I put @Bindable on getAgeString. so ui will use ageString

package com.databindingnumber;

import android.databinding.BaseObservable;
import android.databinding.Bindable;

public class MyData extends BaseObservable{
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(this.age != age) {
            this.age = age;
            notifyPropertyChanged(BR.ageString);//NOTE: ui is using ageString !
        }
    }

    @Bindable
    public String getAgeString() {
        return Integer.toString(age);
    }

    public void setAgeString(String ageString) {
        try {
            int val = Integer.parseInt(ageString);
            this.setAge(val);
        }catch(NumberFormatException ex){
            this.setAge(0);//default value
        }
    }
}

The Layout File(activity_main.xml). use normal two-way binding with @= but use ageString instead of age

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="foo" type="com.databindingnumber.MyData"/>
    </data>

    <EditText
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:inputType="numberSigned"
        android:text="@={foo.ageString}" />
</layout>

MainActivity.java file

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setFoo(new MyData());
    }
}

Hope this will help to someone!


Title: Converting data from wide to long for two variables

Body:
I have a dataset with two variables in a wide format that I want to convert to long. I've seen the examples from the cook for R but they are for just one variable.

My dataset (graph.dat) looks like this:

 PH25   PH50    PH75    PH100   P25      P50    P75    P100      r_b    c
3.96    5.94    5.94    5.94    1,694   1,736   1,329   800     0.029   7
2.86    2.86    2.86    2.86    1,665   1,792   1,639   1,335   0.027   7
3.96    5.72    5.72    5.72    1,580   1,560   1,156   663     0.023   7

I want to plot PH (x-axis) versus P(y-axis). So for a PH25 value corresponds a value of P25 (and a value of r_b and c); so the long format will be:

PH       P      r_b         c
3.96    1,694   0.029       7
2.86    1,665   0.027       7
3.96    1,580   0.023       7
5.94    1,736   0.029       7
2.86    1,792   0.027       7
5.72    1,560   0.023       7
5.94    1,329   0.029       7
2.86    1,639   0.027       7
5.72    1,156   0.023       7
5.94    800     0.029       7
2.86    1,335   0.027       7
5.72    663     0.023       7

I try to "melt" it twice but it doesn't work.
Here is what I did:

graph.dat1<- melt(graph.dat, id.vars=c("PH25","PH50","PH75",
                                        "PH100","r_b", "c"),
                             variable.name="P",
                             value.name="Pval")

and the new dataset (graph.dat1) is fine because I've only three values for each "P" (giving me a total of 12 observations). It looks like this:

    PH25    PH50    PH75    PH100   r_b     c    P     Pval
1   3.96    5.94    5.94    5.94    0.029   7   P25    1694
2   2.86    2.86    2.86    2.86    0.027   7   P25    1665
3   3.96    5.72    5.72    5.72    0.023   7   P25    1580
4   3.96    5.94    5.94    5.94    0.029   7   P50    1736
5   2.86    2.86    2.86    2.86    0.027   7   P50     1792
6   3.96    5.72    5.72    5.72    0.023   7   P50     1560
7   3.96    5.94    5.94    5.94    0.029   7   P75     1329
8   2.86    2.86    2.86    2.86    0.027   7   P75     1639
9   3.96    5.72    5.72    5.72    0.023   7   P75     1156
10  3.96    5.94    5.94    5.94    0.029   7   P100    800
11  2.86    2.86    2.86    2.86    0.027   7   P100    1335
12  3.96    5.72    5.72    5.72    0.023   7   P100    663

but when I do the second "melt" it doesn't work and I don't know how to solve it. This is the second step I did:

graph.dat2<- melt (graph.dat1,id.vars=c("r_b", "c", "P", "Pval"),
                   variable.name="PdH",
                   value.name="PH")

but then I get 4 times the number of observations (so instead of 12 observations I get 48 observations). So, my new dataset (graph.dat2) looks like:

    r_b     c   P     Pval  PdH      PH
1   0.029   7   P25   1694  PH25    3.96
2   0.027   7   P25   1665  PH25    2.86
3   0.023   7   P25   1580  PH25    3.96
4   0.029   7   P50   1736  PH25    3.96
5   0.027   7   P50   1792  PH25    2.86
6   0.023   7   P50   1560  PH25    3.96
7   0.029   7   P75   1329  PH25    3.96
8   0.027   7   P75   1639  PH25    2.86
9   0.023   7   P75   1156  PH25    3.96
10  0.029   7   P100   800  PH25    3.96
11  0.027   7   P100  1335  PH25    2.86
12  0.023   7   P100   663  PH25    3.96
13  0.029   7   P25   1694  PH50    5.94
14  0.027   7   P25   1665  PH50    2.86
15  0.023   7   P25   1580  PH50    5.72
...

Body:
An alternative solution in base R would be:

df <- read.table(header=TRUE, text="PH25   PH50    PH75    PH100   P25          P50    P75    P100      r_b    c
3.96    5.94    5.94    5.94    1,694   1,736   1,329   800     0.029   7
2.86    2.86    2.86    2.86    1,665   1,792   1,639   1,335   0.027   7
3.96    5.72    5.72    5.72    1,580   1,560   1,156   663     0.023   7")

(want<-do.call(rbind,
               Map(
                 function(x,y) data.frame(PH=df[[x]],P=df[[y]],r_b=df$r_b,c=df$c),
                 c("PH25","PH50","PH75","PH100"),
                 c("P25","P50","P75","P100")
                 )))


问:
How to build a layout of single line and multi line labels in iOS autolayout?
I have an application in swift,and have encountered an autolayout issue. I've come up with the solution to it, but it doesn't seem to work as expected.

So, here's the issue:
I have two labels in a table view cell. The first one should only have one line, and the second one must have as many lines as needed and take up as much vertical space as needed. The first label should always be shown fully and, therefore, determine the position, the size, and the number of lines of the second label. Texts for both are set programmatically when drawing the cell, of course.

The solution seems to be pretty simple: Anchor the first label to the left, center it vertically, anchor the second label to the parent top, right, bottom and the right side of the first one. Set the number of lines to 0 for the second label. The editor shows no warnings or errors.

This is how it looks like:

In most cases it looks good, like on the picture below, but sometimes not (red arrows at the picture below):

The first label should have absolute priority and never be truncated, wrapped, or cut, but this happens. What am I doing wrong?

UPD: For the question to be more clear, the first label is so called 'margin label', it represents the margin and the indicator of the cell. This is done to imitate a tree structure. The second is, of course, the text itself. '>' is for the closed comment, 'v' is for the opened.

答: set height constraint for first label and set 1 for 'lines' attribute</p>


问:
Async Azure File Storage - Upload Tasks Wait For Other Tasks
I have this code that runs a delegate asynchronously:

```<code>public delegate void FileHandler(string path);
....
FileHandler fileHandler = HandleFile;
foreach (FileInfo file in files)
{
    string filePath = file.FullName;
    if (IO.FileAvailable(filePath))
    {
        fileHandler.BeginInvoke(filePath, null, null); // i call EndInvoke later
    }
}
</code>```

Here is the delegate, that just uploads stuff into Azure File Storage:

```<code>private static void HandleFile(string path)
{
    AzureStorage.Instance.UploadFile("some-key", path);
}
</code>```

Also, this is the Upload functions:

```<code>public async Task UploadFileAsync(string storageKey, string filePath)
{
    CloudFile loc = Navigate(storageKey);
    await TransferManager.UploadAsync(filePath, loc);
}

public void UploadFile(string storageKey, string filePath)
{
    Task uploadTask = UploadFileAsync(storageKey, filePath);
    Console.WriteLine("Will wait for " + storageKey + " path: " + filePath + " with thread: " + Thread.CurrentThread.ManagedThreadId);
    uploadTask.Wait();
    Console.WriteLine(">>> Done waiting for " + storageKey + " path: " + filePath);
    uploadTask.Dispose();
}
</code>```

Here is my output:

```<code>Will wait for /bills/test/SomePdfName_1.pdf path: C:\test\SomePdfName_1.pdf with thread: 4
Will wait for /bills/test/SomePdfName_6.pdf path: C:\test\SomePdfName_6.pdf with thread: 7
Will wait for /bills/test/SomePdfName_10.pdf path: C:\test\SomePdfName_10.pdf with thread: 5
Will wait for /bills/test/SomePdfName_5.pdf path: C:\test\SomePdfName_5.pdf with thread: 9
Will wait for /bills/test/SomePdfName_3.pdf path: C:\test\SomePdfName_3.pdf with thread: 8
Will wait for /bills/test/SomePdfName_7.pdf path: C:\test\SomePdfName_7.pdf with thread: 11
Will wait for /bills/test/SomePdfName_4.pdf path: C:\test\SomePdfName_4.pdf with thread: 10
Will wait for /bills/test/SomePdfName_2.pdf path: C:\test\SomePdfName_2.pdf with thread: 6
Will wait for /bills/test/SomePdfName_8.pdf path: C:\test\SomePdfName_8.pdf with thread: 12
Will wait for /bills/test/SomePdfName_9.pdf path: C:\test\SomePdfName_9.pdf with thread: 13
.... 10 seconds later
>>> Done waiting for /bills/test/SomePdfName_9.pdf path: C:\test\SomePdfName_9.pdf
>>> Done waiting for /bills/test/SomePdfName_10.pdf path: C:\test\SomePdfName_10.pdf
>>> Done waiting for /bills/test/SomePdfName_1.pdf path: C:\test\SomePdfName_1.pdf
>>> Done waiting for /bills/test/SomePdfName_7.pdf path: C:\test\SomePdfName_7.pdf
>>> Done waiting for /bills/test/SomePdfName_2.pdf path: C:\test\SomePdfName_2.pdf
>>> Done waiting for /bills/test/SomePdfName_5.pdf path: C:\test\SomePdfName_5.pdf
>>> Done waiting for /bills/test/SomePdfName_6.pdf path: C:\test\SomePdfName_6.pdf
>>> Done waiting for /bills/test/SomePdfName_8.pdf path: C:\test\SomePdfName_8.pdf
>>> Done waiting for /bills/test/SomePdfName_3.pdf path: C:\test\SomePdfName_3.pdf
>>> Done waiting for /bills/test/SomePdfName_4.pdf path: C:\test\SomePdfName_4.pdf
</code>```

As you see, all 10 tasks have to enter into Wait first, before any of the upload tasks finish? My question why is that? This is the same either with 5, 10, 100 or 1000 files.

答:
As you see, all 10 tasks have to enter into Wait first, before any of the upload tasks finish? My question why is that? This is the same either with 5, 10, 100 or 1000 files.

In my opinion, I think this operation should divided into two cases.

First one. The file you upload doesn't exceed the thread pool number.

Since each of the BeginInvoke operations will choose one thread in the thread pool to execute the UploadFile method(All of the operation is async) and await TransferManager.UploadAsync will takes a lot of time to upload the file, so you will find, it looks like wait all the task to finish then start uploading.

In fact one thread will not affect another thread, just the thread is execute very fast.

Second, if the application use the thread number exceed the current thread pool number.

The default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. The number of the threads exceed the current thread pool's thread. It doesn't immediately create new threads in all situations.It will create one thread every 0.5 seconds if there are outstanding tasks, up to the maximum number of threads.

So you will find the application firstly create enough thread to run the UploadFile looks like it wait first.In my opinion, there are not enough threads to execute the await TransferManager.UploadAsync method(this will get another thread from the thread pool to run it). Since the running upload thread has high priority than the await TransferManager.UploadAsync thread. So the computer will firstly create the new thread to run the UploadFile method. If all the UploadFile method run completely, then it will assign another thread to run the TransferManager.UploadAsync method.

If you want to your application doesn't wait all the task, you could try to use Parallel class.

More details, you could refer to this codes:

```<code>        Parallel.ForEach(dirInfo.GetFiles(), new ParallelOptions() { MaxDegreeOfParallelism = 108 }, (file) =>
        {
            string filePath = file.FullName;

            Console.WriteLine("start BeginInvoke for file: " + file + " with thread: " + Thread.CurrentThread.ManagedThreadId);
            fileHandler.Invoke(filePath); // i call EndInvoke later

            Console.WriteLine("end BeginInvoke for file: " + file + " with thread: " + Thread.CurrentThread.ManagedThreadId);
        });
</code>```

Body:
I have this code that runs a delegate asynchronously:

public delegate void FileHandler(string path);
....
FileHandler fileHandler = HandleFile;
foreach (FileInfo file in files)
{
    string filePath = file.FullName;
    if (IO.FileAvailable(filePath))
    {
        fileHandler.BeginInvoke(filePath, null, null); // i call EndInvoke later
    }
}

Here is the delegate, that just uploads stuff into Azure File Storage:

private static void HandleFile(string path)
{
    AzureStorage.Instance.UploadFile("some-key", path);
}

Also, this is the Upload functions:

public async Task UploadFileAsync(string storageKey, string filePath)
{
    CloudFile loc = Navigate(storageKey);
    await TransferManager.UploadAsync(filePath, loc);
}

public void UploadFile(string storageKey, string filePath)
{
    Task uploadTask = UploadFileAsync(storageKey, filePath);
    Console.WriteLine("Will wait for " + storageKey + " path: " + filePath + " with thread: " + Thread.CurrentThread.ManagedThreadId);
    uploadTask.Wait();
    Console.WriteLine(">>> Done waiting for " + storageKey + " path: " + filePath);
    uploadTask.Dispose();
}

Here is my output:

Will wait for /bills/test/SomePdfName_1.pdf path: C:\test\SomePdfName_1.pdf with thread: 4
Will wait for /bills/test/SomePdfName_6.pdf path: C:\test\SomePdfName_6.pdf with thread: 7
Will wait for /bills/test/SomePdfName_10.pdf path: C:\test\SomePdfName_10.pdf with thread: 5
Will wait for /bills/test/SomePdfName_5.pdf path: C:\test\SomePdfName_5.pdf with thread: 9
Will wait for /bills/test/SomePdfName_3.pdf path: C:\test\SomePdfName_3.pdf with thread: 8
Will wait for /bills/test/SomePdfName_7.pdf path: C:\test\SomePdfName_7.pdf with thread: 11
Will wait for /bills/test/SomePdfName_4.pdf path: C:\test\SomePdfName_4.pdf with thread: 10
Will wait for /bills/test/SomePdfName_2.pdf path: C:\test\SomePdfName_2.pdf with thread: 6
Will wait for /bills/test/SomePdfName_8.pdf path: C:\test\SomePdfName_8.pdf with thread: 12
Will wait for /bills/test/SomePdfName_9.pdf path: C:\test\SomePdfName_9.pdf with thread: 13
.... 10 seconds later
>>> Done waiting for /bills/test/SomePdfName_9.pdf path: C:\test\SomePdfName_9.pdf
>>> Done waiting for /bills/test/SomePdfName_10.pdf path: C:\test\SomePdfName_10.pdf
>>> Done waiting for /bills/test/SomePdfName_1.pdf path: C:\test\SomePdfName_1.pdf
>>> Done waiting for /bills/test/SomePdfName_7.pdf path: C:\test\SomePdfName_7.pdf
>>> Done waiting for /bills/test/SomePdfName_2.pdf path: C:\test\SomePdfName_2.pdf
>>> Done waiting for /bills/test/SomePdfName_5.pdf path: C:\test\SomePdfName_5.pdf
>>> Done waiting for /bills/test/SomePdfName_6.pdf path: C:\test\SomePdfName_6.pdf
>>> Done waiting for /bills/test/SomePdfName_8.pdf path: C:\test\SomePdfName_8.pdf
>>> Done waiting for /bills/test/SomePdfName_3.pdf path: C:\test\SomePdfName_3.pdf
>>> Done waiting for /bills/test/SomePdfName_4.pdf path: C:\test\SomePdfName_4.pdf

As you see, all 10 tasks have to enter into Wait first, before any of the upload tasks finish? My question why is that? This is the same either with 5, 10, 100 or 1000 files.

Body:
As you see, all 10 tasks have to enter into Wait first, before any of the upload tasks finish? My question why is that? This is the same either with 5, 10, 100 or 1000 files.

In my opinion, I think this operation should divided into two cases.

First one. The file you upload doesn't exceed the thread pool number.

Since each of the BeginInvoke operations will choose one thread in the thread pool to execute the UploadFile method(All of the operation is async) and await TransferManager.UploadAsync will takes a lot of time to upload the file, so you will find, it looks like wait all the task to finish then start uploading.

In fact one thread will not affect another thread, just the thread is execute very fast.

Second, if the application use the thread number exceed the current thread pool number.

The default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. The number of the threads exceed the current thread pool's thread. It doesn't immediately create new threads in all situations.It will create one thread every 0.5 seconds if there are outstanding tasks, up to the maximum number of threads.

So you will find the application firstly create enough thread to run the UploadFile looks like it wait first.In my opinion, there are not enough threads to execute the await TransferManager.UploadAsync method(this will get another thread from the thread pool to run it). Since the running upload thread has high priority than the await TransferManager.UploadAsync thread. So the computer will firstly create the new thread to run the UploadFile method. If all the UploadFile method run completely, then it will assign another thread to run the TransferManager.UploadAsync method.

If you want to your application doesn't wait all the task, you could try to use Parallel class.

More details, you could refer to this codes:

        Parallel.ForEach(dirInfo.GetFiles(), new ParallelOptions() { MaxDegreeOfParallelism = 108 }, (file) =>
        {
            string filePath = file.FullName;

            Console.WriteLine("start BeginInvoke for file: " + file + " with thread: " + Thread.CurrentThread.ManagedThreadId);
            fileHandler.Invoke(filePath); // i call EndInvoke later

            Console.WriteLine("end BeginInvoke for file: " + file + " with thread: " + Thread.CurrentThread.ManagedThreadId);
        });


问:
How to write unit test for the helper in Meteor Mocha [ Closed ]
How to test the following checkUser helper with two parameters currentUser.username and username using Mocha (A JavaScript Test Framework)

```<code><div class="col-sm-1">
      {{#if checkUser currentUser.username username }}
        <button class="btn btn-small btn-danger my-delete-btn pull-right"><span class="glyphicons glyphicon glyphicon-trash"></span></button>
      {{/if}}
</div>
</code>```

答:
Welcome to SO. Before you ask a (way too general) question on how to solve a problem, you should first do your research and write code. If you get stuck in a specific part of your coding (like an unexpected error is thrown and can't be traced back) you may ask a question.

Also read about when, when not and how to ask a question on SO:

https://stackoverflow.com/help/on-topic

https://stackoverflow.com/help/dont-ask

https://stackoverflow.com/help/mcve

Regarding your question, you may read "How to write a simple Blaze Unit Test" as part of the Meteor Test Guide.

Body:
How to test the following checkUser helper with two parameters currentUser.username and username using Mocha (A JavaScript Test Framework)

      {{#if checkUser currentUser.username username }}
        <button class="btn btn-small btn-danger my-delete-btn pull-right"></button>
      {{/if}}


问:
Django queryset custom manager - refresh caching
In my userprofile model, I have a method (turned into a property) that returns a queryset based on another model's custom manager.

Concretely, a user can sell non-perishable and perishable items, and on the Item model level, several custom managers live that contain the logic (and return the querysets) for determining whether an item is perished or not. Within the userprofile, a method lives that returns something similar to:

```<code>Item.live_objects.filter(seller=self.user),
</code>```

where non_perished_objects is one of the said custom managers.

If however an item is added, it is never reflected through these userprofile methods. Only when restarting the server (and the queryset caches being refilled) are the results correct.

Is there a way to force Django to reload the data and drop the cached data?

Thanks in advance!

Update:

```<code>class LiveItemsManager(models.Manager):

    kwargs = {'perished': False,
              'schedule__start_date__lte': datetime.datetime.now(),
              'schedule__end_date__gt': datetime.datetime.now()}

    def get_query_set(self):
        return super(LiveItemsManager, self).get_query_set().filter(**self.kwargs)

class Item(models.Model):
    live_objects = LiveItemsManager()
    perished = models.BooleanField(default=False)
    seller = models.ForeignKey(User)
</code>```

As you see, there's also a Schedule model, containing a start_date, an end_data and an item_id field.

In the UserProfile model, I have:

```<code>def _get_live_items(self):
    results = Item.live_objects.filter(seller=self.user)
    return results

live_items = property(_get_live_items)
</code>```

The problem is that when calling the live_items property, the results returned are only the cached results.

(PS: Don't mind the setup of the models; there's a reason why the models are what they are :))

答:
The issue is that the kwargs are evaluated when the Manager is first defined - which is when models.py is first imported. So the values to be used against schedule__start_date and schedule__end_date are calculated then, and will not change. You can fix this by moving the kwargs declaration inside the method:

```<code>def get_query_set(self):
    kwargs = {'perished': False,
              'schedule__start_date__lte': datetime.datetime.now(),
              'schedule__end_date__gt': datetime.datetime.now()}
    return super(LiveItemsManager, self).get_query_set().filter(**kwargs)
</code>```

(Putting the definition into __init__() won't help, as it will have the same effect: the definition will be evaluated at instantiation of the manager, rather than definition, but since the manager is instantiated when the model is defined, this is pretty much the same time.)

Body:
In my userprofile model, I have a method (turned into a property) that returns a queryset based on another model's custom manager.

Concretely, a user can sell non-perishable and perishable items, and on the Item model level, several custom managers live that contain the logic (and return the querysets) for determining whether an item is perished or not. Within the userprofile, a method lives that returns something similar to:

Item.live_objects.filter(seller=self.user),

where non_perished_objects is one of the said custom managers.

If however an item is added, it is never reflected through these userprofile methods. Only when restarting the server (and the queryset caches being refilled) are the results correct.

Is there a way to force Django to reload the data and drop the cached data?

Thanks in advance!

Update:

class LiveItemsManager(models.Manager):

    kwargs = {'perished': False,
              'schedule__start_date__lte': datetime.datetime.now(),
              'schedule__end_date__gt': datetime.datetime.now()}

    def get_query_set(self):
        return super(LiveItemsManager, self).get_query_set().filter(**self.kwargs)

class Item(models.Model):
    live_objects = LiveItemsManager()
    perished = models.BooleanField(default=False)
    seller = models.ForeignKey(User)

As you see, there's also a Schedule model, containing a start_date, an end_data and an item_id field.

In the UserProfile model, I have:

def _get_live_items(self):
    results = Item.live_objects.filter(seller=self.user)
    return results

live_items = property(_get_live_items)

The problem is that when calling the live_items property, the results returned are only the cached results.

(PS: Don't mind the setup of the models; there's a reason why the models are what they are :))

Body:
The issue is that the kwargs are evaluated when the Manager is first defined - which is when models.py is first imported. So the values to be used against schedule__start_date and schedule__end_date are calculated then, and will not change. You can fix this by moving the kwargs declaration inside the method:

def get_query_set(self):
    kwargs = {'perished': False,
              'schedule__start_date__lte': datetime.datetime.now(),
              'schedule__end_date__gt': datetime.datetime.now()}
    return super(LiveItemsManager, self).get_query_set().filter(**kwargs)

(Putting the definition into __init__() won't help, as it will have the same effect: the definition will be evaluated at instantiation of the manager, rather than definition, but since the manager is instantiated when the model is defined, this is pretty much the same time.)


答:
You can use this trick : </p>

```<code>$('.positiveMessage').show();
var time_hide = 1000; // in ms
setTimeout(function(){
    $('.positiveMessage').hide();
}, time_hide);
</code>```

Title: Get response from API with jQuery

Body:
I'd like to type some stuff into an input field and then get a response from the API if the value exist or not.

The API will response with a true/ false value if the zipcode (12345) exist. http://192.168.100.100:8887/public?zipcode=12345

And here's my code so far:

$(function() {

    $('.positiveMessage, .negativeMessage').hide()
    $('#submitBtn').on('click', function() {
        var $zipcode = document.getElementById('searchZipCode').value
        $.getJSON({
            type: 'GET',
            url: 'http://192.168.100.100:8887/public?zipcode=' + $zipCode,
            success: result => {
                //Goes through the array
                $.each(result, function(i, info) {
                    $('body').append(info + '')
                })
            }
        })
    })
})

As you can see I'd like to manipulate the API by adding a variable in the link.

Body:
You can use this trick :

$('.positiveMessage').show();
var time_hide = 1000; // in ms
setTimeout(function(){
    $('.positiveMessage').hide();
}, time_hide);


问:
Key permanently invalidated Exception after adding/removing fingerprint
When user adds new fingerprint or removes any existing fingerprint and then tries to launch the application it throws KeyPermanentlyInvalidatedException

Here's my fingerprint code:

```<code> public Boolean auth(FingerprintManager.AuthenticationCallback callback) {
    try {
        KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
        if (store == null) {
            return null;
        }

        Cipher cipher = accessCipher();
        if (cipher == null) {
            return null;
        }

        store.load(null);
        SecretKey key = (SecretKey) store.getKey(DEFAULT_KEY_NAME, DEFAULT_STORE_PASS.toCharArray());
        cipher.init(Cipher.ENCRYPT_MODE, key);

        FingerprintManager manager = initManager();
        if (manager == null) {
            return null;
        }

        manager.authenticate(
                generateCryptoObject(cipher),
                generateCancellationSignal(),
                0,
                callback,
                null
        );

        return true;
    } catch (Throwable exc) {
        Logger.error(TAG, exc.getLocalizedMessage(), exc);
        return null;
    }
}

private Cipher accessCipher() {
    try {
        return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        // Was not available.
        return null;
    }
}
</code>```

Called auth method in onResume().

Entire FingerPrintUtils class:

`

```<code>public class FingerPrintUtils {

    private static final String TAG = FingerPrintUtils.class.getSimpleName();
    private static final String DEFAULT_KEYSTORE = "AndroidKeyStore";
    private static final String DEFAULT_KEY_NAME = "myApplication";
    private static final String DEFAULT_STORE_PASS = "csdgh@jkbvj@";
    private static FingerPrintUtils fingerPrintUtils;

    private Boolean isCancelled;
    private CancellationSignal cancellationSignal;

    @TargetApi(23)
    public static FingerPrintUtils getInstance() {
        if (fingerPrintUtils == null) {
            fingerPrintUtils = new FingerPrintUtils();
        }
        return fingerPrintUtils;
    }

    @TargetApi(23)
    public Boolean isFingerAuthAvailable() {
        Boolean hasHarware = hasHardware();
        if (hasHarware == null || !hasHarware) {
            return false;
        }

        Boolean hasPrint = hasRegisteredPrint();
        if (hasPrint == null || !hasPrint) {
            return false;
        }
        return true;
    }

    @TargetApi(23)
    public Boolean hasHardware() {
        FingerprintManager manager = initManager();
        if (manager == null) {
            return null;
        }

        return manager.isHardwareDetected();
    }

    @TargetApi(23)
    public Boolean hasRegisteredPrint() {
        FingerprintManager manager = initManager();
        if (manager == null) {
            return null;
        }

        return manager.hasEnrolledFingerprints();
    }

    @TargetApi(23)
    public Boolean createKey() {
        try {
            KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
            if (store == null) {
                return null;
            }

            KeyGenerator generator = accessKeyGen(KeyProperties.KEY_ALGORITHM_AES, DEFAULT_KEYSTORE);
            if (generator == null) {
                return null;
            }

            generator.init(new KeyGenParameterSpec.Builder(
                    DEFAULT_KEY_NAME,
                    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
            )
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                            // Require the user to authenticate with a fingerprint to authorize every use
                            // of the key
                    .setUserAuthenticationRequired(true)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                    .build());
            generator.generateKey();
            return true;
        } catch (Throwable exc) {
            Logger.error(TAG, exc.getLocalizedMessage(), exc);
            return false;
        }
    }

    @TargetApi(23)
    public Boolean keyExist() {
        try {
            KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
            if (store == null) {
                return null;
            }

            store.load(null);
            SecretKey key = (SecretKey) store.getKey(DEFAULT_KEY_NAME, DEFAULT_STORE_PASS.toCharArray());
            if (key != null) {
                return true;
            }

        } catch (Throwable exc) {
            Logger.error(TAG, exc.getLocalizedMessage(), exc);
            return null;
        }
        return false;
    }

    @TargetApi(23)
    public Boolean auth(FingerprintManager.AuthenticationCallback callback) {
        try {
            KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
            if (store == null) {
                return null;
            }

            Cipher cipher = accessCipher();
            if (cipher == null) {
                return null;
            }

            store.load(null);
            SecretKey key = (SecretKey) store.getKey(DEFAULT_KEY_NAME, DEFAULT_STORE_PASS.toCharArray());
            cipher.init(Cipher.ENCRYPT_MODE, key);

            FingerprintManager manager = initManager();
            if (manager == null) {
                return null;
            }

            manager.authenticate(
                    generateCryptoObject(cipher),
                    generateCancellationSignal(),
                    0,
                    callback,
                    null
            );

            return true;
        } catch (Throwable exc) {
            Logger.error(TAG, exc.getLocalizedMessage(), exc);
            return null;
        }
    }

    @TargetApi(23)
    public Boolean stop() {
        if (isCancelled != null && !isCancelled) {
            isCancelled = true;
            cancellationSignal.cancel();
            cancellationSignal = null;
            return true;
        }
        return false;
    }

    @TargetApi(23)
    private FingerprintManager initManager() {
        Context context = BasePreferenceHelper.getCurrentContext();
        if (context == null) {
            return null;
        }

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return null;
        }

        FingerprintManager manager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
        if (manager == null) {
            return null;
        }

        return manager;
    }

    @TargetApi(23)
    private Cipher accessCipher() {
        try {
            return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                    + KeyProperties.BLOCK_MODE_CBC + "/"
                    + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            // Was not available.
            return null;
        }
    }

    @TargetApi(23)
    private KeyStore accessKeyStore(String storeName) {
        try {
            return KeyStore.getInstance(storeName);
        } catch (Throwable exc) {
            // Was not available.
            return null;
        }
    }

    @TargetApi(23)
    private FingerprintManager.CryptoObject generateCryptoObject(Cipher cipher) {
        if (cipher == null) {
            throw new IllegalArgumentException("Cipher is required.");
        }
        return new FingerprintManager.CryptoObject(cipher);
    }

    @TargetApi(23)
    private CancellationSignal generateCancellationSignal() {
        cancellationSignal = new CancellationSignal();
        isCancelled = false;
        return cancellationSignal;
    }

    @TargetApi(23)
    private KeyGenerator accessKeyGen(String algo, String storeName) {
        try {
            return KeyGenerator.getInstance(algo, storeName);
        } catch (Throwable exc) {
            // Was not available.
            return null;
        }
    }
}
</code>```

`

答:
In your FingerPrintUtils#Auth() method, while initializing cipher.init(), put it into try catch block. Handle KeyInvalidatedException by deleting key from keystore and again create key using createKey() method. It will definitely work.

use below code for the same.

```<code>try
{
    cipher.init(Cipher.ENCRYPT_MODE, key);
}
catch (KeyPermanentlyInvalidatedException e)
{
    store.deleteEntry(DEFAULT_KEY_NAME);
    createKey();
    auth(callback);
    return false;
}
</code>```

Body:
When user adds new fingerprint or removes any existing fingerprint and then tries to launch the application it throws KeyPermanentlyInvalidatedException

Here's my fingerprint code:

 public Boolean auth(FingerprintManager.AuthenticationCallback callback) {
    try {
        KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
        if (store == null) {
            return null;
        }

        Cipher cipher = accessCipher();
        if (cipher == null) {
            return null;
        }

        store.load(null);
        SecretKey key = (SecretKey) store.getKey(DEFAULT_KEY_NAME, DEFAULT_STORE_PASS.toCharArray());
        cipher.init(Cipher.ENCRYPT_MODE, key);

        FingerprintManager manager = initManager();
        if (manager == null) {
            return null;
        }

        manager.authenticate(
                generateCryptoObject(cipher),
                generateCancellationSignal(),
                0,
                callback,
                null
        );

        return true;
    } catch (Throwable exc) {
        Logger.error(TAG, exc.getLocalizedMessage(), exc);
        return null;
    }
}

private Cipher accessCipher() {
    try {
        return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        // Was not available.
        return null;
    }
}

Called auth method in onResume().

Entire FingerPrintUtils class:

`

public class FingerPrintUtils {

    private static final String TAG = FingerPrintUtils.class.getSimpleName();
    private static final String DEFAULT_KEYSTORE = "AndroidKeyStore";
    private static final String DEFAULT_KEY_NAME = "myApplication";
    private static final String DEFAULT_STORE_PASS = "csdgh@jkbvj@";
    private static FingerPrintUtils fingerPrintUtils;

    private Boolean isCancelled;
    private CancellationSignal cancellationSignal;

    @TargetApi(23)
    public static FingerPrintUtils getInstance() {
        if (fingerPrintUtils == null) {
            fingerPrintUtils = new FingerPrintUtils();
        }
        return fingerPrintUtils;
    }

    @TargetApi(23)
    public Boolean isFingerAuthAvailable() {
        Boolean hasHarware = hasHardware();
        if (hasHarware == null || !hasHarware) {
            return false;
        }

        Boolean hasPrint = hasRegisteredPrint();
        if (hasPrint == null || !hasPrint) {
            return false;
        }
        return true;
    }

    @TargetApi(23)
    public Boolean hasHardware() {
        FingerprintManager manager = initManager();
        if (manager == null) {
            return null;
        }

        return manager.isHardwareDetected();
    }

    @TargetApi(23)
    public Boolean hasRegisteredPrint() {
        FingerprintManager manager = initManager();
        if (manager == null) {
            return null;
        }

        return manager.hasEnrolledFingerprints();
    }

    @TargetApi(23)
    public Boolean createKey() {
        try {
            KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
            if (store == null) {
                return null;
            }

            KeyGenerator generator = accessKeyGen(KeyProperties.KEY_ALGORITHM_AES, DEFAULT_KEYSTORE);
            if (generator == null) {
                return null;
            }

            generator.init(new KeyGenParameterSpec.Builder(
                    DEFAULT_KEY_NAME,
                    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
            )
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                            // Require the user to authenticate with a fingerprint to authorize every use
                            // of the key
                    .setUserAuthenticationRequired(true)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                    .build());
            generator.generateKey();
            return true;
        } catch (Throwable exc) {
            Logger.error(TAG, exc.getLocalizedMessage(), exc);
            return false;
        }
    }

    @TargetApi(23)
    public Boolean keyExist() {
        try {
            KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
            if (store == null) {
                return null;
            }

            store.load(null);
            SecretKey key = (SecretKey) store.getKey(DEFAULT_KEY_NAME, DEFAULT_STORE_PASS.toCharArray());
            if (key != null) {
                return true;
            }

        } catch (Throwable exc) {
            Logger.error(TAG, exc.getLocalizedMessage(), exc);
            return null;
        }
        return false;
    }

    @TargetApi(23)
    public Boolean auth(FingerprintManager.AuthenticationCallback callback) {
        try {
            KeyStore store = accessKeyStore(DEFAULT_KEYSTORE);
            if (store == null) {
                return null;
            }

            Cipher cipher = accessCipher();
            if (cipher == null) {
                return null;
            }

            store.load(null);
            SecretKey key = (SecretKey) store.getKey(DEFAULT_KEY_NAME, DEFAULT_STORE_PASS.toCharArray());
            cipher.init(Cipher.ENCRYPT_MODE, key);

            FingerprintManager manager = initManager();
            if (manager == null) {
                return null;
            }

            manager.authenticate(
                    generateCryptoObject(cipher),
                    generateCancellationSignal(),
                    0,
                    callback,
                    null
            );

            return true;
        } catch (Throwable exc) {
            Logger.error(TAG, exc.getLocalizedMessage(), exc);
            return null;
        }
    }

    @TargetApi(23)
    public Boolean stop() {
        if (isCancelled != null && !isCancelled) {
            isCancelled = true;
            cancellationSignal.cancel();
            cancellationSignal = null;
            return true;
        }
        return false;
    }

    @TargetApi(23)
    private FingerprintManager initManager() {
        Context context = BasePreferenceHelper.getCurrentContext();
        if (context == null) {
            return null;
        }

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return null;
        }

        FingerprintManager manager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
        if (manager == null) {
            return null;
        }

        return manager;
    }

    @TargetApi(23)
    private Cipher accessCipher() {
        try {
            return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                    + KeyProperties.BLOCK_MODE_CBC + "/"
                    + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            // Was not available.
            return null;
        }
    }

    @TargetApi(23)
    private KeyStore accessKeyStore(String storeName) {
        try {
            return KeyStore.getInstance(storeName);
        } catch (Throwable exc) {
            // Was not available.
            return null;
        }
    }

    @TargetApi(23)
    private FingerprintManager.CryptoObject generateCryptoObject(Cipher cipher) {
        if (cipher == null) {
            throw new IllegalArgumentException("Cipher is required.");
        }
        return new FingerprintManager.CryptoObject(cipher);
    }

    @TargetApi(23)
    private CancellationSignal generateCancellationSignal() {
        cancellationSignal = new CancellationSignal();
        isCancelled = false;
        return cancellationSignal;
    }

    @TargetApi(23)
    private KeyGenerator accessKeyGen(String algo, String storeName) {
        try {
            return KeyGenerator.getInstance(algo, storeName);
        } catch (Throwable exc) {
            // Was not available.
            return null;
        }
    }
}

`

Body:
In your FingerPrintUtils#Auth() method, while initializing cipher.init(), put it into try catch block. Handle KeyInvalidatedException by deleting key from keystore and again create key using createKey() method. It will definitely work.

use below code for the same.

try
{
    cipher.init(Cipher.ENCRYPT_MODE, key);
}
catch (KeyPermanentlyInvalidatedException e)
{
    store.deleteEntry(DEFAULT_KEY_NAME);
    createKey();
    auth(callback);
    return false;
}


问:
Split string based on spaces and read that in angular2
I am creating a pipe in angular2 where I want to split the string on white spaces and later on read it as an array.

```<code>let stringToSplit = "abc def ghi";
StringToSplit.split(" ");
console.log(stringToSplit[0]);
</code>```

When I log this, I always get "a" as output. Where I am going wrong?

答:
Made a few changes:

let stringToSplit = "abc def ghi";
let x = stringToSplit.split(" ");
console.log(x[0]);

The split method returns an array. Instead of using its result, you are getting the first element of the original string.

ContentLicense: CC BY-SA 4.0

Body:
I am creating a pipe in angular2 where I want to split the string on white spaces and later on read it as an array.

let stringToSplit = "abc def ghi";
StringToSplit.split(" ");
console.log(stringToSplit[0]);

When I log this, I always get "a" as output. Where I am going wrong?


问:
Mongo DB Path setting with homebrew
I am new to mongodb, I am install Mongodb with homebrew on OS Mac and it has been successfully installed,

Now I am writing,

```<code>mongod --dbpath mypath
</code>```

What I want to achieve is, not to write every time dbpath --dbpath /path but just mongod

Any extra configuration I need to do for mac system ?

Thanks in advance.

答:
You could just fork it into the background:

```<code>$ mongod --dbpath=yourPath --syslog --fork
</code>```

Mongod will bo runing in the background. To stop it use kill

```<code>$ pkill mongod
</code>```

This is the prefered way, so you don't have to start a new terminal session.

But if you do not care about that you can just make an alias:

```<code> $ alias mongod='mongod --dbpath yourPath'
</code>```

To make it permanent you have to add it to you shell config file. I'll presume you are runing bash and you config file is .bashrc, but any other shell or config file would be similar

```<code>$ echo "alias mongod='mongod --dbpath yourPath'" >> ~/.bashrc
$ source ~/.bashrc # reloads the shell
</code>```

Title: Mongo DB Path setting with homebrew

Body:
I am new to mongodb, I am install Mongodb with homebrew on OS Mac and it has been successfully installed,

Now I am writing,

mongod --dbpath mypath

What I want to achieve is, not to write every time dbpath --dbpath /path but just mongod

Any extra configuration I need to do for mac system ?

Thanks in advance.

Body:
You could just fork it into the background:

$ mongod --dbpath=yourPath --syslog --fork

Mongod will bo runing in the background. To stop it use kill

$ pkill mongod

This is the prefered way, so you don't have to start a new terminal session.

But if you do not care about that you can just make an alias:

 $ alias mongod='mongod --dbpath yourPath'

To make it permanent you have to add it to you shell config file. I'll presume you are runing bash and you config file is .bashrc, but any other shell or config file would be similar

$ echo "alias mongod='mongod --dbpath yourPath'" >> ~/.bashrc
$ source ~/.bashrc # reloads the shell


问:
Spark SQL: Specify column name resulting from UDF in WHERE clause
I have written a UDF function which will return a column (0 or 1) after processing 2 columns. I need to have my select query such that it returns those records for which this value is 1.
I wrote the query as below:

```<code>SELECT number, myUDF(col1, col2) as result
    FROM mytable
    WHERE result is not null
</code>```

However it doesn't recognize the column name 'result'. Is there any special syntax needed so that it recognizes this new output column? Thanks.

答:
CASE statement should solve the problem here:

SELECT number,
CASE when myUDF(col1, col2) = 1 then myUDF(col1, col2) END as result
FROM mytable

Body:
I have written a UDF function which will return a column (0 or 1) after processing 2 columns. I need to have my select query such that it returns those records for which this value is 1.
I wrote the query as below:

SELECT number, myUDF(col1, col2) as result
    FROM mytable
    WHERE result is not null

However it doesn't recognize the column name 'result'. Is there any special syntax needed so that it recognizes this new output column? Thanks.


问:
HBase・PySpark table load Error
I try to read a table from HBase at PySpark.

This is my code.

```<code>from pyspark.sql.types import *
host = 'localhost'

keyConv = "org.apache.spark.examples.pythonconverters.ImmutableBytesWritableToStringConverter"

valueConv = "org.apache.spark.examples.pythonconverters.HBaseResultToStringConverter"

testdata_conf = {"hbase.zookeeper.quorum": host, "hbase.mapreduce.inputtable": "test", "hbase.mapreduce.scan.columns": "cf:a"}

testdata_rdd = sc.newAPIHadoopRDD("org.apache.hadoop.hbase.mapreduce.TableInputFormat","org.apache.hadoop.hbase.io.ImmutableBytesWritable","org.apache.hadoop.hbase.client.Result",keyConverter=keyConv,valueConverter=valueConv,conf=testdata_conf)

output = cmdata_rdd.collect()
output
</code>```

but, I get the error.

```<code>An error occurred while calling z:org.apache.spark.api.python.PythonRDD.newAPIHadoopRDD.
: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.io.ImmutableBytesWritable
</code>```

I refer to this link RDD is having only first column value : Hbase, PySpark for table loading.

I do not use Java and Scala ever. So, I can not understand why the error occured.

If anyone have advise, please tell me.

Thank you.

答:
Since I just started with (py)Spak I can feel with you.
You have to add the jars to your pySpark command with the --jars option.
Here is a example (we are using Cloudera, so please make sure the jar is in a place that you can reach):

```<code>pyspark --jars /opt/cloudera/parcels/CDH/jars/spark-examples-1.6.0-cdh5.10.0-hadoop2.6.0-cdh5.10.0.jar,/opt/cloudera/parcels/CDH/jars/hbase-examples-1.2.0-cdh5.10.0.jar
</code>```

Your exception was a "ClassNotFoundException" so that means that the jar is not on your classpath.

To check whether your additional jars are appended correctly to the classpath look at the info message you see when pySpark starts up. There must be a message like this:

INFO ui.SparkUI: Started SparkUI at http://xx.xx.xx.xx:4040
  Where x is the IP of your cluster.

Open a browser and navigate to this address.
You will see the cluster status. Click on Enviroment and browse to the very end of the page. You will see your jars followed by "Added by User"

This is my script that worked for me without any problems:
    from pyspark import SparkContext, HiveContext
    from pyspark.streaming import StreamingContext

```<code>def main():
    # The SparkContext might be initialized by the spark Shell
    sc = SparkContext("local[2]", appName='SparkHBaseWriter')
    # Config to write to a hBaseFile
    conf = {"hbase.zookeeper.qourum": "quickstart.cloudera:2181",\
                "zookeeper.znode.parent": "/hbase",\
                "hbase.mapred.outputtable": "test",\
                "mapreduce.outputformat.class": "org.apache.hadoop.hbase.mapreduce.TableOutputFormat",\
                "mapreduce.job.output.key.class": "org.apache.hadoop.hbase.io.ImmutableBytesWritable",\
                "mapreduce.job.output.value.class": "org.apache.hadoop.io.Writable"}
    keyConv = "org.apache.spark.examples.pythonconverters.StringToImmutableBytesWritableConverter"
    valueConv = "org.apache.spark.examples.pythonconverters.StringListToPutConverter"
    rdd = sc.parallelize((("row1", ["row1", "cf1", "cel1", "value from PySpark"]),))
    rdd.saveAsNewAPIHadoopDataset(conf=conf,keyConverter=keyConv,valueConverter=valueConv)

    # read data from hBase
    conf = {"hbase.zookeeper.qourum": "sbglboclomd0002.santanderde.dev.corp:2181",\
                "zookeeper.znode.parent": "/hbase",\
                "hbase.mapred.outputtable": "test",\
                "mapreduce.outputformat.class": "org.apache.hadoop.hbase.mapreduce.TableOutputFormat",\
                "mapreduce.job.output.key.class": "org.apache.hadoop.hbase.io.ImmutableBytesWritable",\
                "mapreduce.job.output.value.class": "org.apache.hadoop.io.Writable"}

    host = 'localhost'

    keyConv = "org.apache.spark.examples.pythonconverters.ImmutableBytesWritableToStringConverter"

    valueConv = "org.apache.spark.examples.pythonconverters.HBaseResultToStringConverter"

    testdata_conf = {
        "hbase.zookeeper.quorum": "quickstart.cloudera:2181",
        "hbase.mapreduce.inputtable": "test",
        "hbase.mapreduce.scan.columns": "cf1:cel1"
        }

    testdata_rdd = sc.newAPIHadoopRDD(
        "org.apache.hadoop.hbase.mapreduce.TableInputFormat",
        "org.apache.hadoop.hbase.io.ImmutableBytesWritable",
        "org.apache.hadoop.hbase.client.Result",
        keyConverter=keyConv,
        valueConverter=valueConv,
        conf=testdata_conf)

    output = testdata_rdd.collect()
    print(output[0])
    # Output in console:
    # >>> testdata_rdd.collect()[0]
    # (u'row1', u'{"qualifier" : "cel1", "timestamp" : "1499151230221", "columnFamily" : "cf1", "row" : "row1", "type" : "Put", "value" : "value from PySpark"}')

if __name__ == '__main__':
    main()
</code>```

I know your question is very old, but I hope this gonna help others.

Title: HBase・PySpark table load Error

Body:
I try to read a table from HBase at PySpark.

This is my code.

from pyspark.sql.types import *
host = 'localhost'

keyConv = "org.apache.spark.examples.pythonconverters.ImmutableBytesWritableToStringConverter"

valueConv = "org.apache.spark.examples.pythonconverters.HBaseResultToStringConverter"

testdata_conf = {"hbase.zookeeper.quorum": host, "hbase.mapreduce.inputtable": "test", "hbase.mapreduce.scan.columns": "cf:a"}

testdata_rdd = sc.newAPIHadoopRDD("org.apache.hadoop.hbase.mapreduce.TableInputFormat","org.apache.hadoop.hbase.io.ImmutableBytesWritable","org.apache.hadoop.hbase.client.Result",keyConverter=keyConv,valueConverter=valueConv,conf=testdata_conf)

output = cmdata_rdd.collect()
output

but, I get the error.

An error occurred while calling z:org.apache.spark.api.python.PythonRDD.newAPIHadoopRDD.
: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.io.ImmutableBytesWritable

I refer to this link RDD is having only first column value : Hbase, PySpark for table loading.

I do not use Java and Scala ever. So, I can not understand why the error occured.

If anyone have advise, please tell me.

Thank you.

Body:
Since I just started with (py)Spak I can feel with you.
You have to add the jars to your pySpark command with the --jars option.
Here is a example (we are using Cloudera, so please make sure the jar is in a place that you can reach):

pyspark --jars /opt/cloudera/parcels/CDH/jars/spark-examples-1.6.0-cdh5.10.0-hadoop2.6.0-cdh5.10.0.jar,/opt/cloudera/parcels/CDH/jars/hbase-examples-1.2.0-cdh5.10.0.jar

Your exception was a "ClassNotFoundException" so that means that the jar is not on your classpath.

To check whether your additional jars are appended correctly to the classpath look at the info message you see when pySpark starts up. There must be a message like this:

INFO ui.SparkUI: Started SparkUI at http://xx.xx.xx.xx:4040
  Where x is the IP of your cluster.

Open a browser and navigate to this address.
You will see the cluster status. Click on Enviroment and browse to the very end of the page. You will see your jars followed by "Added by User"

This is my script that worked for me without any problems:
    from pyspark import SparkContext, HiveContext
    from pyspark.streaming import StreamingContext

def main():
    # The SparkContext might be initialized by the spark Shell
    sc = SparkContext("local[2]", appName='SparkHBaseWriter')
    # Config to write to a hBaseFile
    conf = {"hbase.zookeeper.qourum": "quickstart.cloudera:2181",\
                "zookeeper.znode.parent": "/hbase",\
                "hbase.mapred.outputtable": "test",\
                "mapreduce.outputformat.class": "org.apache.hadoop.hbase.mapreduce.TableOutputFormat",\
                "mapreduce.job.output.key.class": "org.apache.hadoop.hbase.io.ImmutableBytesWritable",\
                "mapreduce.job.output.value.class": "org.apache.hadoop.io.Writable"}
    keyConv = "org.apache.spark.examples.pythonconverters.StringToImmutableBytesWritableConverter"
    valueConv = "org.apache.spark.examples.pythonconverters.StringListToPutConverter"
    rdd = sc.parallelize((("row1", ["row1", "cf1", "cel1", "value from PySpark"]),))
    rdd.saveAsNewAPIHadoopDataset(conf=conf,keyConverter=keyConv,valueConverter=valueConv)

    # read data from hBase
    conf = {"hbase.zookeeper.qourum": "sbglboclomd0002.santanderde.dev.corp:2181",\
                "zookeeper.znode.parent": "/hbase",\
                "hbase.mapred.outputtable": "test",\
                "mapreduce.outputformat.class": "org.apache.hadoop.hbase.mapreduce.TableOutputFormat",\
                "mapreduce.job.output.key.class": "org.apache.hadoop.hbase.io.ImmutableBytesWritable",\
                "mapreduce.job.output.value.class": "org.apache.hadoop.io.Writable"}

    host = 'localhost'

    keyConv = "org.apache.spark.examples.pythonconverters.ImmutableBytesWritableToStringConverter"

    valueConv = "org.apache.spark.examples.pythonconverters.HBaseResultToStringConverter"

    testdata_conf = {
        "hbase.zookeeper.quorum": "quickstart.cloudera:2181",
        "hbase.mapreduce.inputtable": "test",
        "hbase.mapreduce.scan.columns": "cf1:cel1"
        }

    testdata_rdd = sc.newAPIHadoopRDD(
        "org.apache.hadoop.hbase.mapreduce.TableInputFormat",
        "org.apache.hadoop.hbase.io.ImmutableBytesWritable",
        "org.apache.hadoop.hbase.client.Result",
        keyConverter=keyConv,
        valueConverter=valueConv,
        conf=testdata_conf)

    output = testdata_rdd.collect()
    print(output[0])
    # Output in console:
    # >>> testdata_rdd.collect()[0]
    # (u'row1', u'{"qualifier" : "cel1", "timestamp" : "1499151230221", "columnFamily" : "cf1", "row" : "row1", "type" : "Put", "value" : "value from PySpark"}')

if __name__ == '__main__':
    main()

I know your question is very old, but I hope this gonna help others.


问:
Add a resizing layer to a keras sequential model
How can I add a resizing layer to

```<code>model = Sequential()
</code>```

using

```<code>model.add(...)
</code>```

To resize an image from shape (160, 320, 3) to (224,224,3) ?

答:
I think you should consider using tensorflow's resize_images layer.

https://www.tensorflow.org/api_docs/python/tf/image/resize_images

It appears keras does not include this, and perhaps because the feature does not exist in theano. I have written a custom keras layer that does the same. It's a quick hack, so it might not work well in your case.

```<code>import keras
import keras.backend as K
from keras.utils import conv_utils
from keras.engine import InputSpec
from keras.engine import Layer
from tensorflow import image as tfi

class ResizeImages(Layer):
    """Resize Images to a specified size

    # Arguments
        output_size: Size of output layer width and height
        data_format: A string,
            one of `channels_last` (default) or `channels_first`.
            The ordering of the dimensions in the inputs.
            `channels_last` corresponds to inputs with shape
            `(batch, height, width, channels)` while `channels_first`
            corresponds to inputs with shape
            `(batch, channels, height, width)`.
            It defaults to the `image_data_format` value found in your
            Keras config file at `~/.keras/keras.json`.
            If you never set it, then it will be "channels_last".

    # Input shape
        - If `data_format='channels_last'`:
            4D tensor with shape:
            `(batch_size, rows, cols, channels)`
        - If `data_format='channels_first'`:
            4D tensor with shape:
            `(batch_size, channels, rows, cols)`

    # Output shape
        - If `data_format='channels_last'`:
            4D tensor with shape:
            `(batch_size, pooled_rows, pooled_cols, channels)`
        - If `data_format='channels_first'`:
            4D tensor with shape:
            `(batch_size, channels, pooled_rows, pooled_cols)`
    """
    def __init__(self, output_dim=(1, 1), data_format=None, **kwargs):
        super(ResizeImages, self).__init__(**kwargs)
        data_format = conv_utils.normalize_data_format(data_format)
        self.output_dim = conv_utils.normalize_tuple(output_dim, 2, 'output_dim')
        self.data_format = conv_utils.normalize_data_format(data_format)
        self.input_spec = InputSpec(ndim=4)

    def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]

    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_first':
            return (input_shape[0], input_shape[1], self.output_dim[0], self.output_dim[1])
        elif self.data_format == 'channels_last':
            return (input_shape[0], self.output_dim[0], self.output_dim[1], input_shape[3])

    def _resize_fun(self, inputs, data_format):
        try:
            assert keras.backend.backend() == 'tensorflow'
            assert self.data_format == 'channels_last'
        except AssertionError:
            print "Only tensorflow backend is supported for the resize layer and accordingly 'channels_last' ordering"
        output = tfi.resize_images(inputs, self.output_dim)
        return output

    def call(self, inputs):
        output = self._resize_fun(inputs=inputs, data_format=self.data_format)
        return output

    def get_config(self):
        config = {'output_dim': self.output_dim,
                  'padding': self.padding,
                  'data_format': self.data_format}
        base_config = super(ResizeImages, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))
</code>```

Body:
How can I add a resizing layer to

model = Sequential()

using

model.add(...)

To resize an image from shape (160, 320, 3) to (224,224,3) ?

Body:
I think you should consider using tensorflow's resize_images layer.

https://www.tensorflow.org/api_docs/python/tf/image/resize_images

It appears keras does not include this, and perhaps because the feature does not exist in theano. I have written a custom keras layer that does the same. It's a quick hack, so it might not work well in your case.

import keras
import keras.backend as K
from keras.utils import conv_utils
from keras.engine import InputSpec
from keras.engine import Layer
from tensorflow import image as tfi

class ResizeImages(Layer):
    """Resize Images to a specified size

    # Arguments
        output_size: Size of output layer width and height
        data_format: A string,
            one of `channels_last` (default) or `channels_first`.
            The ordering of the dimensions in the inputs.
            `channels_last` corresponds to inputs with shape
            `(batch, height, width, channels)` while `channels_first`
            corresponds to inputs with shape
            `(batch, channels, height, width)`.
            It defaults to the `image_data_format` value found in your
            Keras config file at `~/.keras/keras.json`.
            If you never set it, then it will be "channels_last".

    # Input shape
        - If `data_format='channels_last'`:
            4D tensor with shape:
            `(batch_size, rows, cols, channels)`
        - If `data_format='channels_first'`:
            4D tensor with shape:
            `(batch_size, channels, rows, cols)`

    # Output shape
        - If `data_format='channels_last'`:
            4D tensor with shape:
            `(batch_size, pooled_rows, pooled_cols, channels)`
        - If `data_format='channels_first'`:
            4D tensor with shape:
            `(batch_size, channels, pooled_rows, pooled_cols)`
    """
    def __init__(self, output_dim=(1, 1), data_format=None, **kwargs):
        super(ResizeImages, self).__init__(**kwargs)
        data_format = conv_utils.normalize_data_format(data_format)
        self.output_dim = conv_utils.normalize_tuple(output_dim, 2, 'output_dim')
        self.data_format = conv_utils.normalize_data_format(data_format)
        self.input_spec = InputSpec(ndim=4)

    def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]

    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_first':
            return (input_shape[0], input_shape[1], self.output_dim[0], self.output_dim[1])
        elif self.data_format == 'channels_last':
            return (input_shape[0], self.output_dim[0], self.output_dim[1], input_shape[3])

    def _resize_fun(self, inputs, data_format):
        try:
            assert keras.backend.backend() == 'tensorflow'
            assert self.data_format == 'channels_last'
        except AssertionError:
            print "Only tensorflow backend is supported for the resize layer and accordingly 'channels_last' ordering"
        output = tfi.resize_images(inputs, self.output_dim)
        return output

    def call(self, inputs):
        output = self._resize_fun(inputs=inputs, data_format=self.data_format)
        return output

    def get_config(self):
        config = {'output_dim': self.output_dim,
                  'padding': self.padding,
                  'data_format': self.data_format}
        base_config = super(ResizeImages, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))


问:
Active Azure Sql Connections are over the connection pool limit
We fight the issue in production when once in a while our Azure SQL database performance significantly degrades. We know we have locks on one of the tables, but these locks are not deadlocks, they are long locks and in an hour or so the performance returns to normal. We are trying to find all the possible scenarios on how we get these long locks (every query is super fast and all performance analyzers could show us what causes long locks). The reason for this question is the picture below:

Out connection pool settings allow only 200 connections to be pooled. And most of the times we have about 10-20 open/pooled connections with the database. Then suddenly a number of active connections start to grow and the pool is completely taken. While a number of pooled connections stay below 200, we see a number of active connections using sp_who2 reach 1.5k-2k connections (sometimes 4k-5k).

I've built the same chart using Azure Portal monitoring tools. It has different aggregation period but shows the same issue:

the connection string we use:

Data Source=[server].database.windows.net;initial
  catalog=[database];persist security info=True;user
  id=[user];password=[password];MultipleActiveResultSets=True;Connection
  Timeout=30;Max Pool Size=200;Pooling=True;App=[AppName]

How is that possible taking into account connection pool limitation of 200 connections?

ps: there is no periodic task, long running query or other tool doing anything, we checked with sp_who2 all the active connections to the database.

答:
There are 2 things you can check on you dbcontext objects to see if you are using them correctly and dispose object to return the connection to the connection pool.

First, you are creating the dbcontext from code. Check if there is a using statement around each creation scope of the dbcontext object. Something like:

```<code>using (var context = new xxxContext()) {
    ...
}
</code>```

This will dispose the context when it goes out of scope automatically.

Second you are using dependency injection to inject the dbcontext object. Make sure you are using scoped:

```<code>services.AddScoped<xxxContext>(
</code>```

Then the DI will take care of disposing your context objects.

The next thing you can check is if you have uncommitted transactions. Check if all you transactions are within using blocks, so they will commit or rollback when you are out of scope.

Body:
There are 2 things you can check on you dbcontext objects to see if you are using them correctly and dispose object to return the connection to the connection pool.

First, you are creating the dbcontext from code. Check if there is a using statement around each creation scope of the dbcontext object. Something like:

using (var context = new xxxContext()) {
    ...
}

This will dispose the context when it goes out of scope automatically.

Second you are using dependency injection to inject the dbcontext object. Make sure you are using scoped:

services.AddScoped<xxxContext>(

Then the DI will take care of disposing your context objects.

The next thing you can check is if you have uncommitted transactions. Check if all you transactions are within using blocks, so they will commit or rollback when you are out of scope.


问:
R 3.4.1 "Single Candle" Personal Library Path Error: unable to create ‘NA’
I just updated to R (3.4.1 "Single Candle") on my Linux Mint 18.1 Cinnamon machine and I attempted to install a package. R returned the following:

```<code>> install.packages('ggplot2')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("ggplot2") :
  'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead?  (y/n) y
Would you like to create a personal library
NA
to install packages into?  (y/n) y
Error in install.packages("ggplot2") : unable to create ‘NA’
</code>```

I have encountered the 'lib not writable' output before but typically it offers a solution like this one:

```<code>Would you like to create a personal library
~/R/x86_64-pc-linux-gnu-library/3.4
to install packages into?  (y/n) y
</code>```

Any ideas why the personal library is suggesting NA? Is there a way to manually override this?

答:
The same happened to me while running the installation procedure for some Bioconductor packages.

Then I realized also I could write this (or similar) on the bash command line:

export R_LIBS_USER=$HOME/R/x86_64-pc-linux-gnu-library/3.4 && R

or

export R_LIBS_USER=$HOME/R/x86_64-pc-linux-gnu-library/3.4 && rstudio

and then run upgrade.packages() (or install.packages(), or biocLite()) inside R.

This way the change is temporary, and you don't have to update any config files.

This shell command is useless if subsequently a command in .Renvironor.Rprofile`  sets R_USER_LIBS to a different location during R startup (-check your configuration).

Sticking with in $HOME/R/x86_64-pc-linux-gnu-library/3.X can be desirable
if you have already lots of packages in this location, you want them upgraded/installed there.
I have lots of Bioconductor packages in there, and I don't want them to download again, some of these packages download huge "Omics" datasets when used.
Maybe the partition where /usr/local/lib/R resides has too little disk space or is on a slow drive.

Body:
I just updated to R (3.4.1 "Single Candle") on my Linux Mint 18.1 Cinnamon machine and I attempted to install a package. R returned the following:

> install.packages('ggplot2')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("ggplot2") :
  'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead?  (y/n) y
Would you like to create a personal library
NA
to install packages into?  (y/n) y
Error in install.packages("ggplot2") : unable to create ‘NA’

I have encountered the 'lib not writable' output before but typically it offers a solution like this one:

Would you like to create a personal library
~/R/x86_64-pc-linux-gnu-library/3.4
to install packages into?  (y/n) y

Any ideas why the personal library is suggesting NA? Is there a way to manually override this?