Java Code Examples for org.apache.hadoop.hbase.client.HConnection#getTable()

The following examples show how to use org.apache.hadoop.hbase.client.HConnection#getTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreatHbaseAdapter.java    From opensoc-streaming with Apache License 2.0 6 votes vote down vote up
@Override
public boolean initializeAdapter() {

	// Initialize HBase Table
	Configuration conf = null;
	conf = HBaseConfiguration.create();
	conf.set("hbase.zookeeper.quorum", _quorum);
	conf.set("hbase.zookeeper.property.clientPort", _port);

	try {
		LOGGER.debug("=======Connecting to HBASE===========");
		LOGGER.debug("=======ZOOKEEPER = "
				+ conf.get("hbase.zookeeper.quorum"));
		HConnection connection = HConnectionManager.createConnection(conf);
		table = connection.getTable(_tableName);
		return true;
	} catch (IOException e) {
		// TODO Auto-generated catch block
		LOGGER.debug("=======Unable to Connect to HBASE===========");
		e.printStackTrace();
	}

	return false;
}
 
Example 2
Source File: CIFHbaseAdapter.java    From opensoc-streaming with Apache License 2.0 6 votes vote down vote up
@Override
public boolean initializeAdapter() {

	// Initialize HBase Table
	Configuration conf = null;
	conf = HBaseConfiguration.create();
	conf.set("hbase.zookeeper.quorum", _quorum);
	conf.set("hbase.zookeeper.property.clientPort", _port);

	try {
		LOGGER.debug("=======Connecting to HBASE===========");
		LOGGER.debug("=======ZOOKEEPER = "
				+ conf.get("hbase.zookeeper.quorum"));
		HConnection connection = HConnectionManager.createConnection(conf);
		table = connection.getTable(_tableName);
		return true;
	} catch (IOException e) {
		// TODO Auto-generated catch block
		LOGGER.debug("=======Unable to Connect to HBASE===========");
		e.printStackTrace();
	}

	return false;
}
 
Example 3
Source File: GridTableHBaseBenchmark.java    From Kylin with Apache License 2.0 6 votes vote down vote up
private static void fullScan(HConnection conn, boolean[] hits, Stats stats) throws IOException {
    HTableInterface table = conn.getTable(TEST_TABLE);
    try {
        stats.markStart();

        Scan scan = new Scan();
        scan.addFamily(CF);
        ResultScanner scanner = table.getScanner(scan);
        int i = 0;
        for (Result r : scanner) {
            if (hits[i])
                stats.consume(r);
            dot(i, N_ROWS);
            i++;
        }

        stats.markEnd();
    } finally {
        IOUtils.closeQuietly(table);
    }
}
 
Example 4
Source File: CubeSegmentTupleIterator.java    From Kylin with Apache License 2.0 6 votes vote down vote up
public CubeSegmentTupleIterator(CubeSegment cubeSeg, Collection<HBaseKeyRange> keyRanges, HConnection conn, Collection<TblColRef> dimensions, TupleFilter filter, Collection<TblColRef> groupBy, Collection<RowValueDecoder> rowValueDecoders, StorageContext context) {
    this.cube = cubeSeg.getCubeInstance();
    this.cubeSeg = cubeSeg;
    this.dimensions = dimensions;
    this.filter = filter;
    this.groupBy = groupBy;
    this.rowValueDecoders = rowValueDecoders;
    this.context = context;
    this.tableName = cubeSeg.getStorageLocationIdentifier();
    this.rowKeyDecoder = new RowKeyDecoder(this.cubeSeg);
    this.scanCount = 0;

    try {
        this.table = conn.getTable(tableName);
    } catch (Throwable t) {
        throw new StorageException("Error when open connection to table " + tableName, t);
    }
    this.rangeIterator = keyRanges.iterator();
    scanNextRange();
}
 
Example 5
Source File: HBasePutAppender.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void init() throws IOException {
  super.init();

  HBaseTablespace space = (HBaseTablespace) TablespaceManager.get(uri);
  HConnection hconn = space.getConnection();
  htable = hconn.getTable(columnMapping.getHbaseTableName());
  htable.setAutoFlushTo(false);
  htable.setWriteBufferSize(5 * 1024 * 1024);
}
 
Example 6
Source File: TestHBaseDAO.java    From DistributedCrawler with Apache License 2.0 5 votes vote down vote up
@Test
public void testPut() throws IOException {
	Configuration configuration = HBaseConfiguration.create();
	HConnection connection = HConnectionManager.createConnection(configuration);
	 HTableInterface table = connection.getTable("page");
	 // use the table as needed, for a single operation and a single thread
	 Put put = new Put("2".getBytes());
	 put.add("content".getBytes(), null, "我吃包子".getBytes());
	 put.add("title".getBytes(), null, "吃包子".getBytes());
	 put.add("url".getBytes(), null, "http://www.sina.com.cn".getBytes());
	 table.put(put);
	 table.close();
	 connection.close();
}
 
Example 7
Source File: HConnectionController.java    From recsys-offline with Apache License 2.0 5 votes vote down vote up
public HTableInterface getHTableInterface(String tableName) {
    HConnection con = getHConnection();
    try {
       return con.getTable(tableName);
    } catch (IOException e) {
        logger.error("Cannot to get HTableInterface.", e.getCause());
    }
    return null;
}
 
Example 8
Source File: WhoisHBaseAdapter.java    From opensoc-streaming with Apache License 2.0 5 votes vote down vote up
public boolean initializeAdapter() {
	Configuration conf = null;
	conf = HBaseConfiguration.create();
	conf.set("hbase.zookeeper.quorum", _quorum);
	conf.set("hbase.zookeeper.property.clientPort", _port);
	conf.set("zookeeper.session.timeout", "20");
	conf.set("hbase.rpc.timeout", "20");
	conf.set("zookeeper.recovery.retry", "1");
	conf.set("zookeeper.recovery.retry.intervalmill", "1");

	try {

		LOG.trace("[OpenSOC] Connecting to HBase");
		LOG.trace("[OpenSOC] ZOOKEEPER = "
				+ conf.get("hbase.zookeeper.quorum"));

		LOG.trace("[OpenSOC] CONNECTING TO HBASE WITH: " + conf);

		HConnection connection = HConnectionManager.createConnection(conf);

		LOG.trace("[OpenSOC] CONNECTED TO HBASE");

		table = connection.getTable(_table_name);

		LOG.trace("--------CONNECTED TO TABLE: " + table);

		JSONObject tester = enrich("cisco.com");

		if (tester.keySet().size() == 0)
			throw new IOException(
					"Either HBASE is misconfigured or whois table is missing");

		return true;
	} catch (IOException e) {
		e.printStackTrace();
	}

	return false;

}
 
Example 9
Source File: HBaseClientKVIterator.java    From Kylin with Apache License 2.0 5 votes vote down vote up
public HBaseClientKVIterator(HConnection hconn, String tableName, byte[] family, byte[] qualifier) throws IOException {
    this.family = family;
    this.qualifier = qualifier;

    this.table = hconn.getTable(tableName);
    this.scanner = table.getScanner(family, qualifier);
    this.iterator = scanner.iterator();
}
 
Example 10
Source File: GridTableHBaseBenchmark.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private static void testColumnScan(HConnection conn, List<Pair<Integer, Integer>> colScans) throws IOException {
    Stats stats = new Stats("COLUMN_SCAN");

    HTableInterface table = conn.getTable(TEST_TABLE);
    try {
        stats.markStart();

        int nLogicCols = colScans.size();
        int nLogicRows = colScans.get(0).getSecond() - colScans.get(0).getFirst();
        
        Scan[] scans = new Scan[nLogicCols];
        ResultScanner[] scanners = new ResultScanner[nLogicCols];
        for (int i = 0; i < nLogicCols; i++) {
            scans[i] = new Scan();
            scans[i].addFamily(CF);
            scanners[i] = table.getScanner(scans[i]);
        }
        for (int i = 0; i < nLogicRows; i++) {
            for (int c = 0; c < nLogicCols; c++) {
                Result r = scanners[c].next();
                stats.consume(r);
            }
            dot(i, nLogicRows);
        }
        
        stats.markEnd();
    } finally {
        IOUtils.closeQuietly(table);
    }
}
 
Example 11
Source File: HBaseUtils.java    From hadoop-arch-book with Apache License 2.0 5 votes vote down vote up
public static void populateValidationRules(HConnection connection, ValidationRules rules) throws Exception {
  HTableInterface table = connection.getTable(HBaseTableMetaModel.profileCacheTableName);

  try {
    Put put = new Put(HBaseTableMetaModel.validationRulesRowKey);
    put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.validationRulesRowKey, Bytes.toBytes(rules.getJSONObject().toString()));
    table.put(put);
  } finally {
    table.close();
  }
}
 
Example 12
Source File: HBaseUtils.java    From hadoop-arch-book with Apache License 2.0 5 votes vote down vote up
public static void populateUserProfile(HConnection connection, UserProfile userProfile) throws Exception {
  HTableInterface table = connection.getTable(HBaseTableMetaModel.profileCacheTableName);

  try {
    Put put = new Put(convertKeyToRowKey(HBaseTableMetaModel.profileCacheTableName, userProfile.userId));
    put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.profileCacheJsonColumn, Bytes.toBytes(userProfile.getJSONObject().toString()));
    put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.profileCacheTsColumn, Bytes.toBytes(System.currentTimeMillis()));
    table.put(put);
  } finally {
    table.close();
  }
}
 
Example 13
Source File: PingHBaseCLI.java    From Kylin with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    String metadataUrl = args[0];
    String hbaseTable = args[1];

    System.out.println("Hello friend.");

    Configuration hconf = HadoopUtil.newHBaseConfiguration(metadataUrl);
    if (User.isHBaseSecurityEnabled(hconf)) {
        try {
            System.out.println("--------------Getting kerberos credential for user " + UserGroupInformation.getCurrentUser().getUserName());
            TokenUtil.obtainAndCacheToken(hconf, UserGroupInformation.getCurrentUser());
        } catch (InterruptedException e) {
            System.out.println("--------------Error while getting kerberos credential for user " + UserGroupInformation.getCurrentUser().getUserName());
        }
    }

    Scan scan = new Scan();
    int limit = 20;

    HConnection conn = null;
    HTableInterface table = null;
    ResultScanner scanner = null;
    try {
        conn = HConnectionManager.createConnection(hconf);
        table = conn.getTable(hbaseTable);
        scanner = table.getScanner(scan);
        int count = 0;
        for (Result r : scanner) {
            byte[] rowkey = r.getRow();
            System.out.println(Bytes.toStringBinary(rowkey));
            count++;
            if (count == limit)
                break;
        }
    } finally {
        if (scanner != null) {
            scanner.close();
        }
        if (table != null) {
            table.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}
 
Example 14
Source File: HBaseRowDigestTest.java    From Kylin with Apache License 2.0 4 votes vote down vote up
@Test
public static void test() throws IOException {
    String hbaseUrl = "hbase"; // use hbase-site.xml on classpath
    HConnection conn = null;
    HTableInterface table = null;
    try {
        conn = HBaseConnection.get(hbaseUrl);
        table = conn.getTable("KYLIN_II_YTYWP3CQGJ");
        ResultScanner scanner = table.getScanner(CF, QN);
        StringBuffer sb = new StringBuffer();
        while (true) {
            Result r = scanner.next();
            if (r == null)
                break;

            Cell[] cells = r.rawCells();
            Cell c = cells[0];

            k.set(c.getRowArray(), c.getRowOffset(), c.getRowLength());
            v.set(c.getValueArray(), c.getValueOffset(), c.getValueLength());

            byte[] row = k.copyBytes();
            byte[] value = v.copyBytes();
            //                byte[] row = r.getRow();
            //                byte[] value = r.getValue(CF, QN);
            //
            sb.append("row length: " + row.length + "\r\n");
            sb.append(BytesUtil.toReadableText(row) + "\r\n");
            sb.append("value length: " + value.length + "\r\n");
            sb.append(BytesUtil.toReadableText(value) + "\r\n");
        }
        System.out.println(sb.toString());
        FileUtils.writeStringToFile(new File("/Users/honma/Desktop/a3"), sb.toString());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (table != null)
            table.close();
        if (conn != null)
            conn.close();
    }

}
 
Example 15
Source File: EndpointTupleIterator.java    From Kylin with Apache License 2.0 4 votes vote down vote up
public EndpointTupleIterator(IISegment segment, TupleFilter rootFilter, Collection<TblColRef> groupBy, List<FunctionDesc> measures, StorageContext context, HConnection conn) throws Throwable {

        String tableName = segment.getStorageLocationIdentifier();
        table = conn.getTable(tableName);
        factTableName = segment.getIIDesc().getFactTableName();

        if (rootFilter == null) {
            rootFilter = ConstantTupleFilter.TRUE;
        }

        if (groupBy == null) {
            groupBy = Sets.newHashSet();
        }

        if (measures == null) {
            measures = Lists.newArrayList();
        }

        //this method will change measures
        rewriteMeasureParameters(measures, segment.getColumns());

        this.seg = segment;
        this.context = context;
        this.measures = measures;

        this.columns = segment.getColumns();
        this.columnNames = getColumnNames(columns);

        this.tupleInfo = buildTupleInfo();
        this.tableRecordInfo = new TableRecordInfo(this.seg);

        this.pushedDownRowType = CoprocessorRowType.fromTableRecordInfo(tableRecordInfo, this.columns);
        this.pushedDownFilter = CoprocessorFilter.fromFilter(this.seg, rootFilter);

        for (TblColRef column : this.pushedDownFilter.getUnstrictlyFilteredColumns()) {
            groupBy.add(column);
        }

        this.pushedDownProjector = CoprocessorProjector.makeForEndpoint(tableRecordInfo, groupBy);
        this.pushedDownAggregators = EndpointAggregators.fromFunctions(tableRecordInfo, measures);

        IIProtos.IIRequest endpointRequest = prepareRequest();
        regionResponsesIterator = getResults(endpointRequest, table);

        if (this.regionResponsesIterator.hasNext()) {
            this.tupleIterator = new SingleRegionTupleIterator(this.regionResponsesIterator.next());
        } else {
            this.tupleIterator = ITupleIterator.EMPTY_TUPLE_ITERATOR;
        }
    }
 
Example 16
Source File: GridTableHBaseBenchmark.java    From Kylin with Apache License 2.0 4 votes vote down vote up
private static void prepareData(HConnection conn) throws IOException {
    HTableInterface table = conn.getTable(TEST_TABLE);

    try {
        // check how many rows existing
        int nRows = 0;
        Scan scan = new Scan();
        scan.setFilter(new KeyOnlyFilter());
        ResultScanner scanner = table.getScanner(scan);
        for (Result r : scanner) {
            r.getRow(); // nothing to do
            nRows++;
        }

        if (nRows > 0) {
            System.out.println(nRows + " existing rows");
            if (nRows != N_ROWS)
                throw new IOException("Expect " + N_ROWS + " rows but it is not");
            return;
        }

        // insert rows into empty table
        System.out.println("Writing " + N_ROWS + " rows to " + TEST_TABLE);
        long nBytes = 0;
        for (int i = 0; i < N_ROWS; i++) {
            byte[] rowkey = Bytes.toBytes(i);
            Put put = new Put(rowkey);
            byte[] cell = randomBytes();
            put.add(CF, QN, cell);
            table.put(put);
            nBytes += cell.length;
            dot(i, N_ROWS);
        }
        System.out.println();
        System.out.println("Written " + N_ROWS + " rows, " + nBytes + " bytes");

    } finally {
        IOUtils.closeQuietly(table);
    }

}
 
Example 17
Source File: GridTableHBaseBenchmark.java    From Kylin with Apache License 2.0 4 votes vote down vote up
private static void jumpScan(HConnection conn, boolean[] hits, Stats stats) throws IOException {

        final int jumpThreshold = 6; // compensate for Scan() overhead, totally by experience

        HTableInterface table = conn.getTable(TEST_TABLE);
        try {

            stats.markStart();

            int i = 0;
            while (i < N_ROWS) {
                int start, end;
                for (start = i; start < N_ROWS; start++) {
                    if (hits[start])
                        break;
                }
                for (end = start + 1; end < N_ROWS; end++) {
                    boolean isEnd = true;
                    for (int j = 0; j < jumpThreshold && end + j < N_ROWS; j++)
                        if (hits[end + j])
                            isEnd = false;
                    if (isEnd)
                        break;
                }

                if (start < N_ROWS) {
                    Scan scan = new Scan();
                    scan.setStartRow(Bytes.toBytes(start));
                    scan.setStopRow(Bytes.toBytes(end));
                    scan.addFamily(CF);
                    ResultScanner scanner = table.getScanner(scan);
                    i = start;
                    for (Result r : scanner) {
                        stats.consume(r);
                        dot(i, N_ROWS);
                        i++;
                    }
                }
                i = end;
            }

            stats.markEnd();

        } finally {
            IOUtils.closeQuietly(table);
        }
    }
 
Example 18
Source File: HTableFactory.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public HTableInterface getTable(byte[] tableName, HConnection connection, ExecutorService pool) throws IOException {
    return connection.getTable(tableName, pool);
}