Tuesday, August 30, 2016

Use tab characters in text files in IntelliJ

IntelliJ keeps replacing tab characters with spaces and I'm not sure it's its default or I've configured it as such before.

Anyway to change to use tab characters in text files in IntelliJ, do as follows:

IntelliJ IDEA -> Preferences... -> Code Style -> General -> Default Indent Options -> Use tab character

Friday, August 26, 2016

Backup a database in MySQL

To backup a database in MySQL, use the following command:

mysqldump -u trust -p db_trust > trust.sql

Backup a table in MySQL

To backup a table in MySQL, use the following command:

mysqldump -u trust -p db_trust message > message.sql

Reference:
http://stackoverflow.com/questions/6682916/how-to-take-backup-of-a-single-table-in-the-mysql-database

Error: Cannot find module 'express'

If you encounter the following error:

$ node server.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/izeye/IdeaProjects/samples-reactjs/samples/docs/tutorial/server.js:3:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
$

install `express` manually as follows:

npm install express

or use `package.json`'s `dependencies` as follows:

{
  "name": "tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "dependencies": {
    "body-parser": "^1.4.3",
    "express": "^4.4.5"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC"
}

and run `npm install`.

Thursday, August 25, 2016

Delete documents having a specific field with a null value in Elasticsearch

To delete documents having a specific field with a null value in Elasticsearch, install `delete-by-query` as follows:

./bin/plugin install delete-by-query

and restart the Elasticsearch.

Now you can delete them as follows:

curl -XDELETE http://localhost:9200/answer/_query?pretty -d '
{
  "query": {
    "constant_score": {
      "filter": {
        "missing": {
          "field" : "body"
        }
      }
    }
  }
}'

References:
https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugins-delete-by-query.html
https://www.elastic.co/guide/en/elasticsearch/plugins/current/delete-by-query-usage.html

Search documents having a specific field with a null value in Elasticsearch

To search documents having a specific field with a null value in Elasticsearch, do as follows:

curl http://localhost:9200/answer/_search?pretty -d '
{
  "query": {
    "constant_score": {
      "filter": {
        "missing": {
          "field" : "body"
        }
      }
    }
  }
}'

Reference:
https://www.elastic.co/guide/en/elasticsearch/guide/current/_dealing_with_null_values.html

Wednesday, August 24, 2016

Tuesday, August 23, 2016

Sync from local directory to remote directory with rsync

To sync from local directory to remote directory with rsync, do as follows:

rsync -avr . 1.2.3.4::R/home/izeye/workspaces/izeye/logs/

Saturday, August 20, 2016

Autowire beans to a non-bean object in Spring framework

To autowire beans to a non-bean object in Spring framework, do as follows:

@Autowired
private ApplicationContext applicationContext;

@Bean
public List<AnswerEngine> answerEngines() {
AutowireCapableBeanFactory factory =
this.applicationContext.getAutowireCapableBeanFactory();
List<AppProperties.AnswerEngineSpec> answerEngineSpecs =
this.properties.getAnswerEngineSpecs();
Collections.sort(
answerEngineSpecs,
(o1, o2) -> Integer.compare(o1.getEngineOrder(), o2.getEngineOrder()));
List<AnswerEngine> answerEngines = new ArrayList<>();
for (AppProperties.AnswerEngineSpec spec : answerEngineSpecs) {
AnswerEngine answerEngine =
(AnswerEngine) ClassUtils.createInstance(spec.getEngineClass());
factory.autowireBean(answerEngine);
answerEngines.add(answerEngine);
}
return answerEngines;
}

Note that getting `AutowireCapableBeanFactory` from `ApplicationContext` and invoking `autowireBean()`.

Apply a plugin to some sub-projects in Gradle

To apply a plugin to some sub-projects in Gradle, do as follows:

configure(subprojects.findAll {it.name == 'ask-anything-api' || it.name == 'ask-anything-answer-module-ua-analyzer'}) {
    apply plugin: 'checkstyle'

    checkstyle {
        toolVersion = '7.0'
        configFile = rootProject.file("config/checkstyle/checkstyle.xml")
        configProperties = [
                'headerLocation': 'config/checkstyle/checkstyle-header.txt',
                'suppressionsLocation': 'config/checkstyle/checkstyle-suppressions.xml'
        ]
    }
}

Thursday, August 18, 2016

Use Octotree with GitHub Enterprise

To use Octotree with GitHub Enterprise, add your GitHub Enterprise URL (say `https://github.ctb.com/`) to `GitHub Enterprise URLs`.

You need a personal access token for GitHub, so go to the following link (Replace `github.ctb.com` with your domain):

https://github.ctb.com/settings/tokens/new

and create one with the following option:

repo  Full control of private repositories

Provide the token to `Site access token`.

Reference:
https://github.com/buunguyen/octotree#access-token

Install Octotree for easy navigation in GitHub

To install Octotree which helps navigation in GitHub, go to chrome web store and add `Octotree` to Chrome.

You can navigate a GitHub repository with a tree view and download any file.

Reference:
https://github.com/buunguyen/octotree

Change tab size for views in GitHub

To change tab size for views in GitHub, use `ts` parameter as follows:

https://github.com/izeye/ask-anything/blob/master/src/main/java/com/ctb/askanything/Application.java?ts=2

You can change tab size for a repository by adding a `.editorconfig` file as follows:

[*.{java,js,html}]
indent_style = tab
indent_size = 2

Now all `.java`, `.js`, `.html` files use 2 spaces for tab.

References:
https://github.com/tiimgreen/github-cheat-sheet#adjust-tab-space
http://stackoverflow.com/questions/8833953/how-to-change-tab-size-on-github

Install Elasticsearch head plugin

To install Elasticsearch head plugin, do as follows:

./bin/plugin install mobz/elasticsearch-head

Restarting Elasticsearch is not necessary.

Reference:
https://github.com/mobz/elasticsearch-head

Thursday, August 11, 2016

Synchronize a Git repository to another Git repository automatically

To synchronize a Git repository to another Git repository automatically, create a script called `sync2bitbucket.sh` as follows:

cd /home/izeye/workspaces/izeye/ask-anything
git pull >> /home/izeye/workspaces/izeye/ask-anything/sync2bitbucket.log 2>&1
git push bitbucket >> /home/izeye/workspaces/izeye/ask-anything/sync2bitbucket.log 2>&1

and add an execution permission to it as follows:

chmod +x sync2bitbucket.sh

Add a job to Cron as follows:

crontab -e

* * * * * /home/izeye/workspaces/izeye/ask-anything/sync2bitbucket.sh

`git push` to Bitbucket with SSH key

To `git push` to Bitbucket with SSH key, create an SSH key as follows:

ssh-keygen -t rsa -b 4096 -C "izeye@naver.com"

Add `~/.ssh/id_rsa.pub` to your Bitbucket settings as follows:

Bitbucket settings -> SSH keys -> Add key

Change the remote URL as follows:

git remote set-url bitbucket git+ssh://git@bitbucket.org/ctb-return/ask-anything.git

and now you can `git push` as follows:

git push bitbucket

References:
https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
http://stackoverflow.com/questions/8588768/git-push-username-password-how-to-avoid

Monday, August 8, 2016

Gradle jacocoTestReport SKIPPED

If you run `jacocoTestReport`, it will be skipped as follows:

$ ./gradlew clean jacocoTestReport
:clean                                                                          
:compileJava                                                                                                                                                                
:processResources
:classes
:jacocoTestReport SKIPPED

BUILD SUCCESSFUL

Total time: 9.444 secs

This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html
$

Do `test` first as follows:

$ ./gradlew clean test jacocoTestReport
:clean                                                                          
:compileJava                                                                                                                                                                
:processResources
:classes
:compileTestJava                                                                                                                    
:processTestResources UP-TO-DATE
:testClasses
:test                                                        
objc[7046]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
:jacocoTestReport                        
             
BUILD SUCCESSFUL

Total time: 29.017 secs

This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html
$

Reference:
http://stackoverflow.com/questions/20032366/running-jacocoreport

Change Jenkins port

To change Jenkins port, do as follows:

java -jar jenkins.war --httpPort=18080

Reference:
https://wiki.jenkins-ci.org/display/JENKINS/Starting+and+Accessing+Jenkins

Remove Elasticsearch license error

When you run Elasticsearch, you might encounter the following error:

[2016-08-08 18:23:17,625][ERROR][license.plugin.core      ] [Icarus]
#
# License will expire on [Sunday, September 04, 2016]. If you have a new license, please update it.
# Otherwise, please reach out to your support contact.
#
# Commercial plugins operate with reduced functionality on license expiration:
# - marvel
#  - The agent will stop collecting cluster and indices metrics
#  - The agent will stop automatically cleaning indices older than [marvel.history.duration]

If you don't have a license for Marvel, remove `marvel-agent` and `license` plugins from Elasticsearch as follows:

$ ./bin/plugin remove marvel-agent
-> Removing marvel-agent...
Removed marvel-agent
$ ./bin/plugin remove license
-> Removing license...
Removed license
ize@lunch19-VirtualBox:~/programs/elasticsearch-2.3.5$

and remove `marvel` plugin from Kibana as follows:

$ ./bin/kibana plugin --remove marvel
Removing marvel...
$

Now starting again should clear the error.

Reference:
https://www.elastic.co/guide/en/marvel/current/installing-marvel.html

Fix Elasticsearch max file descriptors warning

You might encounter the following warning when you run Elasticsearch:

[2016-08-08 13:48:17,065][WARN ][env                      ] [Michael Nowman] max file descriptors [4096] for elasticsearch process likely too low, consider increasing to at least [65536]

Check your max file descriptors as follows:

$ ulimit -n
1024
$ ulimit -Hn
4096
$

Change the value as follows:

$ sudo vi /etc/security/limits.conf

izeye     soft    nofile  65536
izeye     hard    nofile  65536

Check again as follows:

$ ulimit -n
65536
$ ulimit -Hn
65536
$

The warning should disappear now.

Reference:
http://stackoverflow.com/questions/21515463/how-to-increase-maximum-file-open-limit-ulimit-in-ubuntu

Tuesday, August 2, 2016

java.lang.ClassNotFoundException: com.puppycrawl.tools.checkstyle.CheckStyleTask

When using Gradle 2.0, the following error occurred:

java.lang.ClassNotFoundException: com.puppycrawl.tools.checkstyle.CheckStyleTask

Upgrading to Gradle 2.14.1 solves the issue.

Reference:
https://github.com/checkstyle/checkstyle/issues/2107