A tricky thing in C++ – Will a member function call by using a raw nullptr crash? Maybe no.

As a Java/Python developer for about 10 years, in my mind, invoking a member function from a raw nullptr, there should have always exceptions, as the following shows.

Maybe you think why I discuss about this naive question. Since recently I found, it’s may not be true for C++ and it depends on how the C++ class defines.

Take a look at the following code written by C++ and execution results.

Surprisingly, the code was executed correctly! It’s tested by VC 2019, GCC 4.8.4/7.3.

I never noticed this but after more time thinking, you may understand the reason. It’s because there is no member field de-referenced in C++ class of “A”.

Let me explain it simply. The function A::hello() will be renamed (there is a full set of naming convention) and compiled into a kind of a C function. And the parameter of pointer of “this” will be added into the function (not that simple, but you can understanding like that). So A::hello() will be compiled to a function like _ZN1A5helloEv(A* this). When you call “a->hello()”, actually, it looks like “_ZN1A5helloEv(a)”. Calling a C function will not cause segment fault, but de-referencing will.

The following code snippet will give you better understanding.

In OOP, this kind of class “A” is not useful. No member fields means a object has no properties. For this case, you should use static function and invoking the function as a class function.

But this tricky behavior is good for us to understand how C++ language works.

How to get the OpenJDK binary tar/zip file?

Since Oracle updated their license for Java 8, a lot of companies started officially maintaining OpenJDK, such as Redhat, Amazon, Alibaba and etc. It’s a good time to switch to OpenJDK.

Unlike Oracle, Redhat doesn’t provide the portable tar/zip of pre-built OpenJDK Linux edition. The zip files are only for Windows. Redhat recommends you to use the offical RPM package to install. Generally, it’s better solution but sometimes it’s very inconvenience, especially when you don’t have the permission but you want to have a quick deployment.

What to do? Docker could help!

  • Pull the image of the CentOS you need. In my case, I used a CentOS 7.7 image.

  • Start the container

  • Install the OpenJDK you need, such as OpenJDK8 or OpenJDK11. For example,

  • Find where the JDK is. In my case, it’s /usr/lib/jvm/java-11-openjdk-11.0.5.10-0.el7_7.x86_64

  • Make a tar file of that folder
  • Copy this tar file out to your local.

By this way, you can get the compatible tar/zip file out. Usually, the dependencies should be fulfilled.

Make a Docker Image for Future OpenJFX 8 Build

Here is the Dockerfile.

Then type in the following commands

The whole image is about 2.2GB and pushing takes some time to finish. When it’s done, everyone can get it from the docker hub.

Build OpenJFX 8 in CentOS 6.7 Docker Container

OpenJDK 8 doesn’t include OpenJFX by default. And to make OpenJFX work on old Linux distribution, such as CentOS 6.7, you can not simply follow the official instructions. This article will talk about how to build OpenJFX 8 in CentOS 6.7 docker container.

Start docker container

With docker, it’s extremely easy to get CentOS 6.7 environment.

Instructions

Yum Install Packages


Install GCC/G++ 6, Python 2.7, Ruby 2.4

Reference is https://www.softwarecollections.org/en/scls/rhscl/devtoolset-6/.


Install Gradle, CMake, Ant

Install gradle 4.8 and ant 1.10, update $PATH.


Sync OpenJFX 8 source code

Build without Webkit

Skip the samples and scene-builder build – “vim rt/build.gradle” and comment on the following lines

Start Gradle build.


when the build is done, the javafxsdk-overlay.zip will be generated.


Build with Webkit

With WebKit, the compilation will become extremely slow.

Before doing the steps of “Build without Webkit”, do the following extra steps.

Copy the gradle.properties from the template and enable the Webkit compiling

Enable “COMPILE_WEBKIT“, “BUILD_JAVADOC“, “BUILD_SRC_ZIP“, And specify COMPILE_TARGETS as “linux”

Update buildSrc/linux.gradle a little bit.


Update the symbolic link of /usr/bin/python2

Do the steps of “Build without Webkit“.

 

写个Hadoop Map-Reduce分析NCDC的天气数据

我是Hadoop小白,写本文是总结笔记,怕之后忘了。

搭个Hadoop Cluster

因为手头有几个Raspberry Pi 3的板子,就花了点时间搭了一个小的Hadoop Cluster的集群,四个Slave with Raspbian, Master server在一个虚拟机里安装了Ubuntu 18.04。关于如何配置,IBM上的这篇Blog讲得非常详细。

NCDC的天气数据

在《Hadoop Definitive Guide》上讲到使用NCDC(National Climatic Data Center)的天气数据来分析每年最高的气温。这本书在Github上有1901年和1902年的数据,但如果你想试试更大的数据集就需要自己下载了。

这里有一个Shell Script可以帮你从NCDC的FTP上下载天气数据。你只需要给出起始和终止年份。

下载之后,天气数据会分到不同的zip文件里,但如果上传到HDFS上还要把它放到一个文件里。下面这段Python代码就做这个事情。

要注意的就是,这段代码要运行在天气的Zip同一个文件夹里。

我下载了1901年到1935年的数据,大约400多M,使用hadoop fs的命令上传到HDFS上,

然后就可以写一个简单的Map – Reduce了。

因为HDFS一个Block是128M,所以400M需要4个Block。

Map-Reduce

首先是Mapper,其实就是分析天气文件的每一行,从里面取出温度。

Reducer则是从前面得到的年份和温度的信息汇总在一些,求出当年里最高的气温。

这个过程很像Java 8里引入的Stream。

之后,还要实现一个Job来执行这个Map-Reduce任务。

这个程序需要两个路径,一是NCDC的数据文件,一是结果的输出路径。Job还要需要明确Mapper和Recuder的类名。

生成Jar文件后,使用下面的命令行运行Map-Reduce任务

结果会被发到output4文件夹里,几分钟后,就可以看到结果了。

好了,Hadoop里的HelloWorld算是完成了!

References

 

[VS Code Tips] How to Make AssertJ Be Static Imported in VS Code

After installing the famous Java plugin written by RedHat, VS Code will become into a powerful Java IDE, which is far beyond other editors, such as Sublime, Notepad++.

Today’s tip will show you how to static import AssertJ in VS Code. AssertJ helps you to write assertions in unit testing. Compared to Hamcrest, someone, like me, think AssertJ’s API and syntax make more sense.

First, you need to introduce it. In maven, it’s very straightforward. Simply define a new dependency in pom.xml.

Next step is to update a configuration “java.completion.favoriteStaticMembers“. Otherwise, assertThat() will be the one in Hamcrest in the auto-completion. Remove “org.junit.Assert.*” from the user configuration and add “org.assertj.core.api.Assertions.*” instead.

Now, AssertJ will be statically imported into your unit testing.

Use Atlassian Confluence API to Copy and Create New Pages

For team management, we have a Confluence space to do weekly reporting. In that space, usually, one page is for a week and each page has the same template. So to copy these pages becomes a very boring work. No worries, there are Confluence APIs to help.

Installation and Connection to Confluence

Very simple and pip will help to finish everything.

After installation, use the following code to connect to Confluence.

Copy and Create New Page

Before creating new page, you need to get the body content of the template page.

Pay attention to the expand=’body.storage’. Without it, the returned page only contains the basic information, no body HTML.

Now you have the body and the rest work is a just for-loop.

Also you can use confluence.update_page() to update some existing pages.

References