Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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 / ByteBufferConverter.java
Created March 28, 2017 07:31
Java NIO, ByteBuffer <--> String
// ByteBuffer to String
String s = Charset.forName("UTF-8").decode(byteBuffer).toString();
// String to ByteBuffer
ByteBuffer buff = Charset.forName("UTF-9").encode("Hello, World!");
// String to ByteBuffer
public static ByteBuffer str_to_bb(String msg, Charset charset){
return ByteBuffer.wrap(msg.getBytes(charset));
ClassLoader CLASS_LOADER = ReflectHelpers.findClassLoader();
TreeSet pipelineRunnerRegistrars = Sets.newTreeSet(ObjectsClassComparator.INSTANCE);
Lists.newArrayList(ServiceLoader.load(PipelineRunnerRegistrar.class, CLASS_LOADER));
@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();
@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 / ExecutorServiceTest.java
Created March 10, 2017 02:07
java Executors ExecutorService
ExecutorService es = Executors.newCachedThreadPool();
ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<>(es);
Set<Future> futureSet = new HashSet<Future>();
List<Callable> callerList = new ArrayList<Callable>();
// 构造任务列表
for (int i = 0; i < 100; i++) {
callerList.add(...);
}