2015年12月28日星期一

HDFS的用户权限问题和用户代理

用户代理机制:link.
HDFS用户权限管理:link.

因为下面设置环境变量解决了我的HDFS访问权限问题,所以针对用户代理机制和用户权限管理我没有进一步深入研究。

设置HADOOP_USER_NAME环境比较重要,将环境变量设置为Hadoop的超级用户(启动Namenode进程时的用户)便可以在集群外访问HDFS(在没有安全配置的情况下)。
export HADOOP_USER_NAME=hadoop
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop

HDFS更改文件所有者和权限:
hdfs dfs -chown -R username:group /filepath
hdfs dfs -chmod ...

2015年12月24日星期四

Read Python code in Source Insight

First, download the Python.CLF file in this website: link.

In Source Insight, click Options -> Preferences... and import Python.CLF.

Then in Options -> Document Options..., click 'Add Type...' and add Python Source File. Settings are shown below:

Finish.

2015年12月22日星期二

生成应用商店的用户数据文件

Select with concat:
hive> select concat(device_id, ',', appid, ',', apk_url) from download_log where device_id is not NULL;
hive> select concat(device_id, ',', package_name) from detail_log where device_id is not NULL;
Above data is too large to save.

For the huge number of detail log, I group them first:
hive> select device_id, package_name, count(package_name) rank from detail_log where length(device_id) > 0 and length(package_name) > 0 group by device_id, package_name order by rank desc;

If you want to split each entry by comma instead of tab, you can use awk (assume that your filename is test):
$ cat test | awk '{print $1“,"$2","$3}' > test_commma

For the download log, also I group them:
hive> select device_id, appid, count(appid) rank from download_log where length(device_id) > 0 group by device_id, appid order by rank desc;

Save results into local file:
insert overwrite local directory 'your_path'
row format delimited
fields terminated by '\t'
stored as textfile
select * from table where ...;

Spark MLlib中的数据类型

Please see this doc.

Spark

Please see my doc.

The official document is a good reference for you.

Maybe these links are useful: link1, link2, link3, link4.

Here is a blog about big data.

Sublime Text使用教程

Please see thie doc.

2015年12月17日星期四

IntelliJ IDEA分别搭建基于maven和sbt的Scala项目

About the settings and keyboard shortcuts in IntelliJ, please see this doc.

About how to build Spark apps in IntelliJ, please see this doc.

2015年12月16日星期三

Scala中this.type的使用

一直有一个疑问没搞懂,有必要在这里发上一帖,参见我提的问题和回答:链接

问题如下:
I defined two classes:
class A { def method1 = this }
class B extends A { def method2 = this }

val b = new B
Then I checked the type of b.method1:
scala> b.method1.getClass
res29: Class[_ <: A] = class B
In this case, I cannot use b.method1.method2:
scala> b.method1.method2
<console>:11: error: value method2 is not a member of A
              b.method1.method2
So I have to define A and B like this:
class A { def method1: this.type = this } 
class B extends A { def method2: this.type = this } 

val b = new B
Now I check the type of b.method1:
scala> b.method1.getClass
res31: Class[_ <: B] = class B
Here b.method1.method2 works:
scala> b.method1.method2
res32: b.type = B@4ff3ac
My question here is what does it mean by saying Class[_ <: A] = class B and Class[_ <: B] = class B? And why does the first doesn't work as Class[_ <: A] = class B seems to say that it's also class B?

Reactormonk很好地解答了我的疑问:
Let's split the expression Class[_ <: A] = class B. The first part, Class[_ <: A] tells you what the compiler knows at compile time, that b.method1 returns something of type Class[_ <: A]. The second part, class B mentions that you have a class B as value, but the compiler doesn't know that. That's runtime information.
this.type specializes the method for subclasses, where the type inferred from a plain thisdoesn't.

2015年12月14日星期一

git中ssh密钥的配置

Windows:
首先安装GitTortoiseGit两个软件,windows中使用TortoiseGit图形化界面进行git操作。
安装Git时Adjusting your PATH environment选择Use Git from the Windows Command Prompt,Configuring the line ending conversions选择Checkout Windows-style, commit Unix-style line endings。(Windows中的换行是CRLF,Linux中的换行是LF)

打开TortoiseGit中的PuTTYgen,点击"Generate"按钮,鼠标在上图的空白地方来回移动直到进度条完毕,就会自动生一个随机的key,在"Key passphrase"和"Confirm passphrase"的输入框中输入自己的密码。复制"Public key for pasting into OpenSSH authorized_keys file"下文本框中以ssh-rsa开头的所有内容,添加到github的公钥ssh key中。点击Save private key保存私钥,后缀名为ppk。

选择本地的文件夹,右键选择Git Clone...,输入url,在Load Putty Key中选择刚刚保存的ppk文件,OK。现在可以使用TortoiseGit来clone项目了。

如果你需要使用命令行,那还需要配置一下git的ssh密钥:
打开git安装目录下的git-bash.exe,键入命令:ssh-keygen -t rsa -C "xxx@email.com"
提醒你输入key的名称,直接回车使用默认名称。在C:\Users\$USER_NAME\.ssh中会生成id_rsa和id_rsa.pub,将id_rsa.pub文件中的内容保存到github中。

Linux:
安装Git。
cd ~/.ssh
ssh-keygen -t rsa -C "your-email-address"
将id_rsa.pub中的内容复制粘贴,添加到github的公钥中。
git config --global user.name "your-name"
git config --global user.email your-name@example.com

2015年12月13日星期日

C和C++项目中的extern "C" {}

参见这篇帖子

关于C语言中的extern,一个例子:
1.c文件如下:
#include <stdio.h>

int fun1(int i)
{
    return i + 1;
}

void main()
{
    int i;
    extern int j;
    extern int fun2(int i);

    printf("%d\n", fun2(j));
}

2.c文件如下:
extern int fun1(int i);

int j = 3;

int fun2(int i)
{
    return fun1(i) + 2;
}

编译运行:
$ gcc -c 1.c 2.c
$ gcc 1.o 2.o -o 1
$ ./1
输出结果为6.

关于C调用C++、C++调用C,请参考上面帖子链接中的例子。

对于C和C++的混合编译,参考这个帖子
一个例子:
$ g++ -c cpp2c.cpp -o cpp2c.o
$ gcc -c csayhello.c -o csayhello.o
$ gcc cpp2c.o csayhello.o -lstdc++ -o cpp2c
注意到,在最后链接的时候指定 C++ 标准库是必须的,这是因为我们用的是 gcc 而不是 g++ 调用的链接器。最后如果使用的是 g++ 的话,C++ 标准库默认会被链接。

2015年12月11日星期五

2015年12月7日星期一

Scala

Here are my own slides of learning Scala: link1 (Chinese), link2 (English).

This link is a Chinese tutorial of Scala.

See "A Tour of Scala" for more information.

Here are two video tutorials of Scala: link1, link2.

MacOS下Sublime Text搭建LaTeX编写环境

准备软件:
1、Sublime Text: 链接
2、BasicTeX: 链接
3、Skim: 链接

安装步骤:
1、安装Sublime Text,安装好后安装Package Control和LaTeXTools插件,参考之前的帖子
2、安装BasicTeX和Skim。Skim安装完成后,在选项中设置同步如下:

3、打开终端,运行以下命令:
sudo tlmgr update --self
sudo tlmgr install latexmk
4、LaTeXTools配置
在Sublime Text -> Preferences -> Package Settings -> LatexTools中选择Reconfigure LaTeXTools and migrate settings,然后再选择Sublime Text -> Preferences -> Package Settings -> LatexTools -> Settings - User,在builder_settings中增加如下配置:
"program" : "xelatex",
"command" : ["latexmk", "-cd", "-e", "$pdflatex = 'xelatex -interaction=nonstopmode -synctex=1 %S %O'", "-f", "-pdf"],
如图所示:


否则会出现 Errors:/usr/local/texlive/2015/texmf-dist/tex/latex/fontspec/fontspec.sty:43: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! [ }] 错误。
5、编辑你的tex文件,command + b编译,done。
6、反向搜索
在Skim中按Command + Shift + Click,你就会回到对应于点击位置的tex文件中。

2015年12月5日星期六

Mac找不到用于储存的钥匙串login解决方法

打开Finder,选择菜单栏前往,按住option键,进入资源库,删除资源库中Keychains文件夹中的内容,重启。
折腾了很久搜到了这个解决方法,Mac也有不靠谱的时候。

2015年12月4日星期五

关于移位操作

在计算机中,正数是用原码表示的,负数是用补码表示的。第一位是符号位,0表示正,1表示负。
举个例子,-1在计算机中的表示为1111 1111 1111 1111 1111 1111 1111 1111,由1的反码再加1得到的。

移位操作:左移<< 右移>> 无符号右移>>>
左移无论对于正数还是负数都是乘以2的次幂。

这里主要谈右移:
正数的右移左边补0,所以是简单的除以2的次幂。
负数的右移左边补1,举例:
-1:无论右移多少都是全1,所以-1右移多少位还是-1
-5>>1:1111 1111 1111 1111 1111 1111 1111 1011,右移1位变成1111 1111 1111 1111 1111 1111 1111 1101,所以-5右移1位是-3

无符号右移:无论负数还是正数前面都补0,所以无符号右移的结果一定不会是负数。