使用java编写spark程序,代码如下:
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
public class SimpleApp {
public static void main(String[] args) {
SparkConf conf = new SparkConf().setAppName("wordCount").setMaster("local");
JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<String> input = sc.textFile("/bigdata/softwares/spark-2.1.0-bin-hadoop2.7/testdata/a.txt");
System.out.println();
Long bCount = input.filter(new Function<String,Boolean>(){
public Boolean call(String s){return s.contains("yes");}
}).count();
Long cCount = input.filter(new Function<String,Boolean>(){
public Boolean call(String s){return s.contains("ywq");}
}).count();
System.out.println("yes:"+bCount+" ywq:"+cCount+" all?");
// sc.stop();
}
}
Run Code Online (Sandbox Code Playgroud)
Pom如下?
<dependencies>
<dependency> <!-- Spark dependency -->
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> …Run Code Online (Sandbox Code Playgroud) 我有一个给定的方法Direction calculateDirection(Vector3 from, Vector3 to)
,其中Vector3包含X,Y,Z.
该方法的目的是计算直线方向,它不能是对角线,它只能是X_LEFT或X_RIGHT,或Z_LEFT,或Z_RIGHT或TOP或DOWN.
所以我开始得到补偿:
Direction calculateDirection(Vector3 from, Vector3 to) {
int x = from.getX() - to.getX();
int y = from.getY() - to.getY();
int z = from.getZ() - to.getZ();
}
Run Code Online (Sandbox Code Playgroud)
所以要知道方向,其中一个变量不应该是0,如果有多个变量不是0,我需要返回null或抛出一个exception.
要做到这一点,我需要执行一些if,elseif但我对此感到不舒服,我觉得可以有一个更简单的数学解决方案.
例如,如果x不是0那么我可以检查它是否为负,如果是,那么它X_LEFT.
我想补充一点,X,Y,Z可以是-1,0或1.
我需要创建一个假位图图像来测试(JUnit 测试)我的个人添加和获取自定义 LinkedList 的方法,但 Bitmap.createBitmap 返回错误:
java.lang.RuntimeException:未模拟 android.graphics.Bitmap 中的方法 createBitmap。
这是我的 JUnitTest 的代码:
public class TicketsIteratorTest {
Bitmap img_Bmp;
TicketsIterator<Bitmap> TicketsList = new TicketsIterator();
/*
* Test for the add e get methods, check if the element just insert it's the same of the one just extract.
*/
@Test
public void Add_n_Get() throws Exception {
int i = 0, numIMG = 100;
Bitmap[] IMG_Generated;
IMG_Generated = new Bitmap[numIMG];
// Generate numIMG of imagine to insert into the Iterator and it …Run Code Online (Sandbox Code Playgroud) 我有一个对象,我想检查此对象或嵌套字段是否为空.我想打印这个neted字段,但我应该检查某个级别是否有null,否则我将获得空指针异常.
我知道我可以这样做:
if( object != null && object.A != null && object.A.B != null && object.A.B.C != null && object.A.B.C.D != null) { doSomething( object.A.B.C.D);}
Run Code Online (Sandbox Code Playgroud)
但它太久了.你知道更好的检查方法吗?
我正在使用apply函数更新数据框。
但是现在我需要使用此功能修改多列,
这是我的示例代码:
def update_row(row):
listy = [1,2,3]
return listy
dp_data_df[['A', 'P','Y']] = dp_data_df.apply(update_row, axis=1)
Run Code Online (Sandbox Code Playgroud)
它抛出以下错误:
ValueError: shape mismatch: value array of shape (10,) could not be broadcast to indexing result of shape (3,10)
Run Code Online (Sandbox Code Playgroud)
提前致谢。
您好我正在尝试在python中打印字符串列表,但仍然显示我这个错误."列表索引必须是整数或切片,而不是str"
码:
Features ['entity_number',
'type',
'programs',
'name',
'title',
'addresses']
Run Code Online (Sandbox Code Playgroud)
所以在这里我只想在'name'下显示数据.
有人可以帮我解决这个问题.. 在这里输入图像描述