Skip to content

Instantly share code, notes, and snippets.

@qingniufly
qingniufly / gist:fbb6165e17e095e59ff7b3120aca314f
Created February 13, 2017 08:48 — forked from Bouke/gist:10454272
Install FreeTDS, unixODBC and pyodbc on OS X

First, install the following libraries:

$ brew install unixodbc
$ brew install freetds --with-unixodbc

FreeTDS should already work now, without configuration:

$ tsql -S [IP or hostname] -U [username] -P [password]
locale is "en_US.UTF-8"

locale charset is "UTF-8"

@qingniufly
qingniufly / parquet_spark.scala
Created March 1, 2017 10:10
spark read and write parquet files
// read
val filePath = "file:///path/to/spark/examples/src/main/resources/users.parquet"
val users = sqlContext.read.load(filePath)
// write
val personJsonFile = "file:///path/to/spark/examples/src/main/resources/people.json"
val personDF = sqlContext.read.json(personJsonFile)
personDF.select("name", "age").write.format("parquet").save("file:///tmp/new_people.parquet")
@qingniufly
qingniufly / shell-example.sh
Created March 10, 2017 02:32
shell, sed, awk
# 循环
for i in {1..100}; do echo $i; done
for i in $(seq 1 100); do echo $i; done
for ((i = 0; i < 100; i++)); do echo $i; done
# sed 读取文件 a,b行的内容
sed -n '5,10p' test.txt
# 删除空行
sed '/^$/d' file
@qingniufly
qingniufly / ReflectHelpers.java
Created March 15, 2017 07:00
ClassLoader, Reflect
public class ReflectHelpers {
public static ClassLoader findClassLoader() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if(classLoader == null) {
classLoader = ReflectHelpers.class.getClassLoader();
}
if(classLoader == null) {
classLoader = ClassLoader.getSystemClassLoader();
ClassLoader CLASS_LOADER = ReflectHelpers.findClassLoader();
TreeSet pipelineRunnerRegistrars = Sets.newTreeSet(ObjectsClassComparator.INSTANCE);
Lists.newArrayList(ServiceLoader.load(PipelineRunnerRegistrar.class, CLASS_LOADER));
@qingniufly
qingniufly / CopyFile.java
Created March 28, 2017 07:42
Java NIO file ops
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyFile {
public static void main(String[] args) {
String infname = "/tmp/in.txt";
String outfname = "/tmp/out.txt";
FileInputStream fis = new FileInputStream(infname);
FileOutputStream fos = new FileOutputStream(outfname);
@qingniufly
qingniufly / NIOEchoServer.java
Last active March 29, 2017 09:09 — forked from komamitsu/gist:1528682
NIO Echo Server
package com.komamitsu;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
@qingniufly
qingniufly / git-tag-delete-local-and-remote.sh
Created June 22, 2017 10:20 — forked from mobilemind/git-tag-delete-local-and-remote.sh
how to delete a git tag locally and remote
# delete local tag '12345'
git tag -d 12345
# delete remote tag '12345' (eg, GitHub version too)
git push origin :refs/tags/12345
# alternative approach
git push --delete origin tagName
git tag -d tagName
@qingniufly
qingniufly / file_path_test.sh
Created June 27, 2017 05:20 — forked from feng-ming/file_path_test.sh
unix shell 判断目录/文件是否存在, 以及是否有读写权限。
shell判断文件,目录是否存在或者具有权限
#!/bin/sh
Path="/var/log/httpd/"
File="/var/log/httpd/access.log"
#这里的-x 参数判断$Path是否存在并且是否具有可执行权限
if [ ! -x "$Path"]; then
mkdir "$Path"
fi
@qingniufly
qingniufly / sparkdemo.scala
Created August 23, 2017 04:29 — forked from timvw/sparkdemo.scala
SparkSQL and CTE for increased readability
val df = spark.read.text(inputFile)
df.createOrReplaceTempView("data")
val query =
"""
| WITH loglevel AS (SELECT SPLIT(value, ' ')[0] AS level FROM data WHERE LENGTH(value) > 0),
| levelcount AS (SELECT level, COUNT(*) as count FROM loglevel GROUP BY level)
| SELECT level, count FROM levelcount ORDER BY count DESC
""".stripMargin