定义一个带有两个输入的过程same_structure.True
如果列表具有相同的结构,则应输出
,False
否则.在以下情况下,两个值p和q具有相同的结构:
Neither p or q is a list.
Both p and q are lists, they have the same number of elements, and each
element of p has the same structure as the corresponding element of q.
Run Code Online (Sandbox Code Playgroud)
编辑:为了使图片清晰,以下是预期的输出
same_structure([1, 0, 1], [2, 1, 2])
---> True
same_structure([1, [0], 1], [2, 5, 3])
---> False
same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['d', 'e']]]])
---> True
same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['de']]]])
---> …
Run Code Online (Sandbox Code Playgroud) 嗨,我有一个DataFrame,我需要转换为JavaRDD并返回到DataFrame我有以下代码
DataFrame sourceFrame = hiveContext.read().format("orc").load("/path/to/orc/file");
//I do order by in above sourceFrame and then I convert it into JavaRDD
JavaRDD<Row> modifiedRDD = sourceFrame.toJavaRDD().map(new Function<Row,Row>({
public Row call(Row row) throws Exception {
if(row != null) {
//updated row by creating new Row
return RowFactory.create(updateRow);
}
return null;
});
//now I convert above JavaRDD<Row> into DataFrame using the following
DataFrame modifiedFrame = sqlContext.createDataFrame(modifiedRDD,schema);
Run Code Online (Sandbox Code Playgroud)
sourceFrame
modifiedFrame
当我调用sourceFrame.show()
输出时,模式是相同的我看到每个列都有相应的值而且没有列是空的但是当我调用时modifiedFrame.show()
我看到所有的列值被合并到第一列值中,例如假设源DataFrame有3列,如下所示
_col1 _col2 _col3
ABC 10 DEF
GHI 20 JKL
Run Code Online (Sandbox Code Playgroud)
当我打印从JavaRDD转换的modifiedFrame时,它按以下顺序显示
_col1 _col2 …
Run Code Online (Sandbox Code Playgroud) 我遇到了这个Java程序,它以意想不到的方式运行.以下程序计算int数组中元素对之间的差异.
import java.util.*;
public class SetTest
{
public static void main(String[] args)
{
int vals[] = {786,678,567,456,
345,234,123,012};
Set<Integer> diffs = new HashSet<Integer>();
for(int i=0; i < vals.length ; i++)
for(int j = i; j < vals.length; j++)
diffs.add(vals[i] - vals[j]);
System.out.print(diffs.size());
}
}
Run Code Online (Sandbox Code Playgroud)
如果我们分析它似乎设置大小应该是8,这是数组的大小.但是,如果你运行这个程序,它打印14.发生了什么?任何的想法?
先感谢您.
答:这种奇怪的行为发生是因为如果我们将数组改为12则数组中的012变为八进制,然后按预期打印8.
课程:永远不要用零填充整数文字.
嗨我打算参加千年游戏设计挑战赛.我是游戏开发的新手.我非常了解Java/Scala.
我在想是否有人可以分享建议/了解他们作为游戏开发者的经验.我该如何开始?是否有任何开源工具可以让你快速移动?使用Java或Scala开发游戏哪个更好?
提前致谢.
您好我想通过JDBC在DB2,Sybase,MySQL等多个数据库上创建表.现在我需要使用文本文件创建此表,例如data.txt,其中包含数据空间分隔值.例如
CustName OrderNo PhoneNo
XYZ 230 123456789
ABC 450 879641238
Run Code Online (Sandbox Code Playgroud)
现在这个data.txt包含数千个记录空间分隔值.我需要使用java io逐行解析这个文件,并为每个记录执行sql insert查询.
我发现有LOAD DATA INFILE sql命令.是否有任何JDBC驱动程序支持此命令?如果不是什么应该是解决这个问题的最有效的快速方法.
请指导.提前致谢.
嗨,我有测试程序,在这个路径上将文件加载到hdfs user/user1/data/app/type/file.gz
现在这个测试程序由多个用户多次运行.所以我想将文件权限设置为rwx,以便任何人都可以删除此文件.我有以下代码
fs.setPermission(new Path("user/user1/data"),new FsPermission(FsAction.ALL,FsAction.ALL,FsAction.ALL))
Run Code Online (Sandbox Code Playgroud)
上面的行给drwxrwxrwx
了所有目录,但是对于file.gz它给出了许可,-rw-r--r--
为什么呢?由于这个原因,除了我之外的另一个用户无法通过测试程序删除此文件.我可以通过测试程序删除文件,因为我有充分的感情.
请指导.我是Hadoop的新手.提前致谢.
最近我被问到一个问题,在一个单独的链表中我们如何在一次迭代中进入列表的中间位置.
A --> B --> C --> D (even nodes)
Run Code Online (Sandbox Code Playgroud)
为此,它应该返回指向B的地址
A --> B --> C (odd nodes)
Run Code Online (Sandbox Code Playgroud)
对此,它也应该返回指向B的地址
有一个解决方案,两个指针一个移动一次,其他移动两次,但它似乎没有在这里工作
LinkedList p1,p2;
while(p2.next != null)
{
p1 = p1.next;
p2 = p2.next.next;
}
System.out.print("middle of the node" + p1.data); //This does not give accurate result in odd and even
Run Code Online (Sandbox Code Playgroud)
如果有人之前做过这个,请帮忙.
我遇到了以下程序,它表现得出乎意料.
public class ShiftProgram
{
public static void main(String[] args)
{
int i = 0;
while(-1 << i != 0)
i++;
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我们考虑这个程序输出,当它达到32时,循环条件应该返回false并终止,它应该打印32.
如果你运行这个程序,它不会打印任何东西,但会进入无限循环.有什么想法吗?先感谢您.
嗨,我是 Java 多线程的新手。我有成千上万个需要执行的计划任务/线程。我正在使用以下代码
ScheduledExecutorSerivce scheduleService = Executors.newScheduledThreadPool(90);
Map<Interger,Interger> loginMap = new HashMap<>();//contain login time of scheduled threads
for(int i = 0; i < taskCount ; i++) {
scheduleService.schedule(new MyCallableWorker(),loginMap.get(i),TimeUnit.SECONDS)
}
scheduleService.shutdown();
//do I need to call the following because I dont know any timeout value or above shutDown() is enough
while(!scheduleService.isTerminated()) {
}
Run Code Online (Sandbox Code Playgroud)
另外请让我知道线程池的理想计数应该是多少。我已设置为 90,但我想要可以根据需要增长的池,但看起来 ScheduleExecutorService 没有这样的 API。请提前指导谢谢。
嗨我有Spark作业,它对ORC数据进行一些处理,并使用Spark 1.4.0中引入的DataFrameWriter save()API存储ORC数据.我有以下代码使用重型shuffle内存.如何优化以下代码?它有什么问题吗?它正如预期的那样工作正常,因为GC停顿并且随机播放大量数据而导致内存问题,从而导致速度变慢.请指导我是Spark新手.提前致谢.
JavaRDD<Row> updatedDsqlRDD = orderedFrame.toJavaRDD().coalesce(1, false).map(new Function<Row, Row>() {
@Override
public Row call(Row row) throws Exception {
List<Object> rowAsList;
Row row1 = null;
if (row != null) {
rowAsList = iterate(JavaConversions.seqAsJavaList(row.toSeq()));
row1 = RowFactory.create(rowAsList.toArray());
}
return row1;
}
}).union(modifiedRDD);
DataFrame updatedDataFrame = hiveContext.createDataFrame(updatedDsqlRDD,renamedSourceFrame.schema());
updatedDataFrame.write().mode(SaveMode.Append).format("orc").partitionBy("entity", "date").save("baseTable");
Run Code Online (Sandbox Code Playgroud)
编辑:根据建议我尝试将上面的代码转换为以下使用mapPartitionsWithIndex
()但我仍然看到数据改组它比上面的代码更好,但它仍然失败通过命中GC限制并抛出OOM或进入GC暂停很长时间和超时和YARN会杀死遗嘱执行人.我使用spark.storage.memoryFraction为0.5和spark.shuffle.memoryFraction为0.4我尝试使用默认值并更改了许多组合没有任何帮助请指导
JavaRDD<Row> indexedRdd = sourceRdd.cache().mapPartitionsWithIndex(new Function2<Integer, Iterator<Row>, Iterator<Row>>() {
@Override
public Iterator<Row> call(Integer ind, Iterator<Row> rowIterator) throws Exception {
List<Row> rowList = new ArrayList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next(); …
Run Code Online (Sandbox Code Playgroud)