Created
August 25, 2009 10:56
-
-
Save andreisavu/174636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tested with hbase 0.19.3 and jython 2.2.1 | |
import java.lang | |
from org.apache.hadoop.hbase import HBaseConfiguration, HTableDescriptor, HColumnDescriptor, HConstants | |
from org.apache.hadoop.hbase.client import HBaseAdmin, HTable | |
from org.apache.hadoop.hbase.io import BatchUpdate, Cell, RowResult | |
# First get a conf object. This will read in the configuration | |
# that is out in your hbase-*.xml files such as location of the | |
# hbase master node. | |
conf = HBaseConfiguration() | |
# Create a table named 'test' that has two column families, | |
# one named 'content, and the other 'anchor'. The colons | |
# are required for column family names. | |
tablename = "test" | |
desc = HTableDescriptor(tablename) | |
desc.addFamily(HColumnDescriptor("content:")) | |
desc.addFamily(HColumnDescriptor("anchor:")) | |
admin = HBaseAdmin(conf) | |
# Drop and recreate if it exists | |
if admin.tableExists(tablename): | |
admin.disableTable(tablename) | |
admin.deleteTable(tablename) | |
admin.createTable(desc) | |
tables = admin.listTables() | |
table = HTable(conf, tablename) | |
# Add content to 'column:' on a row named 'row_x' | |
row = 'row_x' | |
update = BatchUpdate(row) | |
update.put('content:', 'some content') | |
table.commit(update) | |
# Now fetch the content just added, returns a byte[] | |
data_row = table.get(row, "content:") | |
data = java.lang.String(data.value, "UTF8") | |
print "The fetched row contains the value '%s'" % data | |
# Delete the table. | |
admin.disableTable(desc.getName()) | |
admin.deleteTable(desc.getName()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# | |
#/** | |
# * Copyright 2007 The Apache Software Foundation | |
# * | |
# * Licensed to the Apache Software Foundation (ASF) under one | |
# * or more contributor license agreements. See the NOTICE file | |
# * distributed with this work for additional information | |
# * regarding copyright ownership. The ASF licenses this file | |
# * to you under the Apache License, Version 2.0 (the | |
# * "License"); you may not use this file except in compliance | |
# * with the License. You may obtain a copy of the License at | |
# * | |
# * http://www.apache.org/licenses/LICENSE-2.0 | |
# * | |
# * Unless required by applicable law or agreed to in writing, software | |
# * distributed under the License is distributed on an "AS IS" BASIS, | |
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# * See the License for the specific language governing permissions and | |
# * limitations under the License. | |
# */ | |
# | |
# The hbase command script. Based on the hadoop command script putting | |
# in hbase classes, libs and configurations ahead of hadoop's. | |
# | |
# TODO: Narrow the amount of duplicated code. | |
# | |
# Environment Variables: | |
# | |
# JAVA_HOME The java implementation to use. Overrides JAVA_HOME. | |
# | |
# HBASE_CLASSPATH Extra Java CLASSPATH entries. | |
# | |
# HBASE_HEAPSIZE The maximum amount of heap to use, in MB. | |
# Default is 1000. | |
# | |
# HBASE_OPTS Extra Java runtime options. | |
# | |
# HBASE_CONF_DIR Alternate conf dir. Default is ${HBASE_HOME}/conf. | |
# | |
# HBASE_ROOT_LOGGER The root appender. Default is INFO,console | |
# | |
bin=`dirname "$0"` | |
bin=`cd "$bin"; pwd` | |
# This will set HBASE_HOME, etc. | |
. "$bin"/hbase-config.sh | |
cygwin=false | |
case "`uname`" in | |
CYGWIN*) cygwin=true;; | |
esac | |
# if no args specified, show usage | |
if [ $# = 0 ]; then | |
echo "Usage: hbase <command>" | |
echo "where <command> is one of:" | |
echo " shell run the HBase shell" | |
echo " master run an HBase HMaster node" | |
echo " regionserver run an HBase HRegionServer node" | |
echo " rest run an HBase REST server" | |
echo " thrift run an HBase Thrift server" | |
echo " zookeeper run a Zookeeper server" | |
echo " migrate upgrade an hbase.rootdir" | |
echo " jython jython shell" | |
echo " or" | |
echo " CLASSNAME run the class named CLASSNAME" | |
echo "Most commands print help when invoked w/o parameters." | |
exit 1 | |
fi | |
# get arguments | |
COMMAND=$1 | |
shift | |
# Source the hbase-env.sh. Will have JAVA_HOME defined. | |
if [ -f "${HBASE_CONF_DIR}/hbase-env.sh" ]; then | |
. "${HBASE_CONF_DIR}/hbase-env.sh" | |
fi | |
# some Java parameters | |
if [ "$JAVA_HOME" != "" ]; then | |
#echo "run java in $JAVA_HOME" | |
JAVA_HOME=$JAVA_HOME | |
fi | |
if [ "$JAVA_HOME" = "" ]; then | |
echo "Error: JAVA_HOME is not set." | |
exit 1 | |
fi | |
JAVA=$JAVA_HOME/bin/java | |
JAVA_HEAP_MAX=-Xmx1000m | |
# check envvars which might override default args | |
if [ "$HBASE_HEAPSIZE" != "" ]; then | |
#echo "run with heapsize $HBASE_HEAPSIZE" | |
JAVA_HEAP_MAX="-Xmx""$HBASE_HEAPSIZE""m" | |
#echo $JAVA_HEAP_MAX | |
fi | |
# so that filenames w/ spaces are handled correctly in loops below | |
IFS= | |
# CLASSPATH initially contains $HBASE_CONF_DIR | |
CLASSPATH="${HBASE_CONF_DIR}" | |
CLASSPATH=${CLASSPATH}:$JAVA_HOME/lib/tools.jar | |
# for developers, add hbase classes to CLASSPATH | |
if [ -d "$HBASE_HOME/build/classes" ]; then | |
CLASSPATH=${CLASSPATH}:$HBASE_HOME/build/classes | |
fi | |
if [ -d "$HBASE_HOME/build/test" ]; then | |
CLASSPATH=${CLASSPATH}:$HBASE_HOME/build/test | |
fi | |
if [ -d "$HBASE_HOME/build/webapps" ]; then | |
CLASSPATH=${CLASSPATH}:$HBASE_HOME/build | |
fi | |
# For releases, add hbase & webapps to CLASSPATH | |
# Webapps must come first else it messes up Jetty | |
if [ -d "$HBASE_HOME/webapps" ]; then | |
CLASSPATH=${CLASSPATH}:$HBASE_HOME | |
fi | |
for f in $HBASE_HOME/hbase*.jar; do | |
if [ -f $f ]; then | |
CLASSPATH=${CLASSPATH}:$f; | |
fi | |
done | |
# Add libs to CLASSPATH | |
for f in $HBASE_HOME/lib/*.jar; do | |
CLASSPATH=${CLASSPATH}:$f; | |
done | |
for f in $HBASE_HOME/lib/jsp-2.1/*.jar; do | |
CLASSPATH=${CLASSPATH}:$f; | |
done | |
# add user-specified CLASSPATH last | |
if [ "$HBASE_CLASSPATH" != "" ]; then | |
CLASSPATH=${CLASSPATH}:${HBASE_CLASSPATH} | |
fi | |
# default log directory & file | |
if [ "$HBASE_LOG_DIR" = "" ]; then | |
HBASE_LOG_DIR="$HBASE_HOME/logs" | |
fi | |
if [ "$HBASE_LOGFILE" = "" ]; then | |
HBASE_LOGFILE='hbase.log' | |
fi | |
# cygwin path translation | |
if $cygwin; then | |
CLASSPATH=`cygpath -p -w "$CLASSPATH"` | |
HBASE_HOME=`cygpath -d "$HBASE_HOME"` | |
HBASE_LOG_DIR=`cygpath -d "$HBASE_LOG_DIR"` | |
fi | |
# setup 'java.library.path' for native-hadoop code if necessary | |
JAVA_LIBRARY_PATH='' | |
if [ -d "${HBASE_HOME}/build/native" -o -d "${HBASE_HOME}/lib/native" ]; then | |
JAVA_PLATFORM=`CLASSPATH=${CLASSPATH} ${JAVA} org.apache.hadoop.util.PlatformName | sed -e "s/ /_/g"` | |
if [ -d "$HBASE_HOME/build/native" ]; then | |
JAVA_LIBRARY_PATH=${HBASE_HOME}/build/native/${JAVA_PLATFORM}/lib | |
fi | |
if [ -d "${HBASE_HOME}/lib/native" ]; then | |
if [ "x$JAVA_LIBRARY_PATH" != "x" ]; then | |
JAVA_LIBRARY_PATH=${JAVA_LIBRARY_PATH}:${HBASE_HOME}/lib/native/${JAVA_PLATFORM} | |
else | |
JAVA_LIBRARY_PATH=${HBASE_HOME}/lib/native/${JAVA_PLATFORM} | |
fi | |
fi | |
fi | |
# cygwin path translation | |
if $cygwin; then | |
JAVA_LIBRARY_PATH=`cygpath -p "$JAVA_LIBRARY_PATH"` | |
fi | |
# restore ordinary behaviour | |
unset IFS | |
# figure out which class to run | |
if [ "$COMMAND" = "shell" ] ; then | |
CLASS="org.jruby.Main ${HBASE_HOME}/bin/hirb.rb" | |
elif [ "$COMMAND" = "master" ] ; then | |
CLASS='org.apache.hadoop.hbase.master.HMaster' | |
elif [ "$COMMAND" = "regionserver" ] ; then | |
CLASS='org.apache.hadoop.hbase.regionserver.HRegionServer' | |
elif [ "$COMMAND" = "rest" ] ; then | |
CLASS='org.apache.hadoop.hbase.rest.Dispatcher' | |
elif [ "$COMMAND" = "thrift" ] ; then | |
CLASS='org.apache.hadoop.hbase.thrift.ThriftServer' | |
elif [ "$COMMAND" = "migrate" ] ; then | |
CLASS='org.apache.hadoop.hbase.util.Migrate' | |
elif [ "$COMMAND" = "zookeeper" ] ; then | |
CLASS='org.apache.hadoop.hbase.zookeeper.HQuorumPeer' | |
elif [ "$COMMAND" = "jython" ] ; then | |
if [ "$JYTHON_HOME" = "" ]; then | |
echo "Error: JYTHON_HOME is not set." | |
exit 1 | |
fi | |
CLASS='org.python.util.jython' | |
HBASE_OPTS="$HBASE_OPTS -Dpython.path=$JYTHON_HOME" | |
CLASSPATH=$CLASSPATH:$JYTHON_HOME/jython.jar | |
else | |
CLASS=$COMMAND | |
fi | |
# Have JVM dump heap if we run out of memory. Files will be 'launch directory' | |
# and are named like the following: java_pid21612.hprof. Apparently it doesn't | |
# 'cost' to have this flag enabled. Its a 1.6 flag only. See: | |
# http://blogs.sun.com/alanb/entry/outofmemoryerror_looks_a_bit_better | |
HBASE_OPTS="$HBASE_OPTS -Dhbase.log.dir=$HBASE_LOG_DIR" | |
HBASE_OPTS="$HBASE_OPTS -Dhbase.log.file=$HBASE_LOGFILE" | |
HBASE_OPTS="$HBASE_OPTS -Dhbase.home.dir=$HBASE_HOME" | |
HBASE_OPTS="$HBASE_OPTS -Dhbase.id.str=$HBASE_IDENT_STRING" | |
HBASE_OPTS="$HBASE_OPTS -Dhbase.root.logger=${HBASE_ROOT_LOGGER:-INFO,console}" | |
if [ "x$JAVA_LIBRARY_PATH" != "x" ]; then | |
HBASE_OPTS="$HBASE_OPTS -Djava.library.path=$JAVA_LIBRARY_PATH" | |
fi | |
# run it | |
exec "$JAVA" $JAVA_HEAP_MAX $HBASE_OPTS -classpath "$CLASSPATH" $CLASS "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getting error when running
sh hbase-with-jython.sh jython