Java Code Examples for org.apache.hadoop.hbase.client.HTableInterface#put()

The following examples show how to use org.apache.hadoop.hbase.client.HTableInterface#put() . 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: UserService.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Override
public void updateUser(UserDetails user) {
    HTableInterface htable = null;
    try {
        byte[] userAuthorities = serialize(user.getAuthorities());
        htable = HBaseConnection.get(hbaseUrl).getTable(userTableName);
        Put put = new Put(Bytes.toBytes(user.getUsername()));
        put.add(Bytes.toBytes(USER_AUTHORITY_FAMILY), Bytes.toBytes(USER_AUTHORITY_COLUMN), userAuthorities);

        htable.put(put);
        htable.flushCommits();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(htable);
    }
}
 
Example 2
Source File: HBaseMetricSendClient.java    From jstorm with Apache License 2.0 6 votes vote down vote up
protected boolean add(KVSerializable item, String tableName) {
    long size = 1L;

    Put put = new Put(item.getKey());
    HTableInterface table = getHTableInterface(tableName);

    //put.setWriteToWAL(false);
    put.add(CF, V_DATA, item.getValue());
    try {
        table.put(put);
        table.flushCommits();
        succCounter.update(size);
        hbaseSendTps.update(size);
    } catch (Throwable ex) {
        logger.error("Error", ex);
        failedCounter.update(size);
        return false;
    } finally {
        closeTable(table);
    }
    return true;
}
 
Example 3
Source File: HBaseResourceStore.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Override
protected void putResourceImpl(String resPath, InputStream content, long ts) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(content, bout);
    bout.close();

    HTableInterface table = getConnection().getTable(getAllInOneTableName());
    try {
        byte[] row = Bytes.toBytes(resPath);
        Put put = buildPut(resPath, ts, row, bout.toByteArray(), table);

        table.put(put);
        table.flushCommits();
    } finally {
        IOUtils.closeQuietly(table);
    }
}
 
Example 4
Source File: HBaseMetricSendClient.java    From jstorm with Apache License 2.0 6 votes vote down vote up
protected boolean batchAdd(Collection<KVSerializable> items, String tableName) {
    int size = items.size();
    List<Put> batch = new ArrayList<>(size);
    HTableInterface table = getHTableInterface(tableName);
    for (KVSerializable v : items) {
        byte[] rowKey = v.getKey();
        Put put = new Put(rowKey);
        put.add(CF, V_DATA, v.getValue());
        batch.add(put);
    }
    try {
        table.put(batch);
        table.flushCommits();
        succCounter.update(size);
        hbaseSendTps.update(size);
    } catch (Throwable ex) {
        logger.error("Error", ex);
        failedCounter.update(size);
        return false;
    } finally {
        closeTable(table);
    }
    return true;
}
 
Example 5
Source File: QueryService.java    From Kylin with Apache License 2.0 6 votes vote down vote up
public void saveQuery(final String creator, final Query query) throws IOException {
    List<Query> queries = getQueries(creator);
    queries.add(query);
    Query[] queryArray = new Query[queries.size()];

    byte[] bytes = querySerializer.serialize(queries.toArray(queryArray));
    HTableInterface htable = null;
    try {
        htable = HBaseConnection.get(hbaseUrl).getTable(userTableName);
        Put put = new Put(Bytes.toBytes(creator));
        put.add(Bytes.toBytes(USER_QUERY_FAMILY), Bytes.toBytes(USER_QUERY_COLUMN), bytes);

        htable.put(put);
        htable.flushCommits();
    } finally {
        IOUtils.closeQuietly(htable);
    }
}
 
Example 6
Source File: QueryService.java    From Kylin with Apache License 2.0 5 votes vote down vote up
public void removeQuery(final String creator, final String id) throws IOException {
    List<Query> queries = getQueries(creator);
    Iterator<Query> queryIter = queries.iterator();

    boolean changed = false;
    while (queryIter.hasNext()) {
        Query temp = queryIter.next();
        if (temp.getId().equals(id)) {
            queryIter.remove();
            changed = true;
            break;
        }
    }

    if (!changed) {
        return;
    }

    Query[] queryArray = new Query[queries.size()];
    byte[] bytes = querySerializer.serialize(queries.toArray(queryArray));
    HTableInterface htable = null;
    try {
        htable = HBaseConnection.get(hbaseUrl).getTable(userTableName);
        Put put = new Put(Bytes.toBytes(creator));
        put.add(Bytes.toBytes(USER_QUERY_FAMILY), Bytes.toBytes(USER_QUERY_COLUMN), bytes);

        htable.put(put);
        htable.flushCommits();
    } finally {
        IOUtils.closeQuietly(htable);
    }
}
 
Example 7
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 8
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 9
Source File: BaseDao.java    From zxl with Apache License 2.0 5 votes vote down vote up
public final void put(List<E> entityList) {
	HTableInterface hTableInterface = getHTableInterface();
	try {
		List<Put> puts = buildPutList(entityList);
		hTableInterface.put(puts);
	} catch (Exception cause) {
		throw new RuntimeException(cause);
	} finally {
		closeHTableInterface(hTableInterface);
	}
}
 
Example 10
Source File: HBaseBackedTransactionLogger.java    From hbase-secondary-index with GNU General Public License v3.0 5 votes vote down vote up
public void setStatusForTransaction(final long transactionId, final TransactionStatus status) {
    Put update = new Put(getRow(transactionId));
    update.add(INFO_FAMILY, STATUS_QUALIFIER, HConstants.LATEST_TIMESTAMP, Bytes.toBytes(status.name()));

    HTableInterface table = getTable();
    try {
        table.put(update);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        putTable(table);
    }
}
 
Example 11
Source File: IndexedRegion.java    From hbase-secondary-index with GNU General Public License v3.0 5 votes vote down vote up
private void updateIndex(final IndexSpecification indexSpec, final Put put,
    final NavigableMap<byte[], byte[]> newColumnValues,
    final SortedMap<byte[], byte[]> oldColumnValues) throws IOException {
  Delete indexDelete = makeDeleteToRemoveOldIndexEntry(indexSpec,
      put.getRow(), oldColumnValues);
  Put indexPut = makeIndexUpdate(indexSpec, put.getRow(), newColumnValues);

  HTableInterface indexTable = getIndexTable(indexSpec);
  try {
    if (indexDelete != null
        && !Bytes.equals(indexDelete.getRow(), indexPut.getRow())) {
      // Only do the delete if the row changed. This way we save the put after
      // delete issues in HBASE-2256
      LOG.debug("Deleting old index row ["
          + Bytes.toString(indexDelete.getRow()) + "]. New row is ["
          + Bytes.toString(indexPut.getRow()) + "].");
      indexTable.delete(indexDelete);
    } else if (indexDelete != null) {
      LOG.debug("Skipping deleting index row ["
          + Bytes.toString(indexDelete.getRow())
          + "] because it has not changed.");
    }
    indexTable.put(indexPut);
  } finally {
    putTable(indexTable);
  }
}
 
Example 12
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 13
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
private void putData(HTableInterface table, byte[] rowkey) throws IOException {
    Put put = new Put(rowkey);
    put.add(TEST_TABLE_CF.getBytes(), "c1".getBytes(), rowkey);
    table.put(put);
}
 
Example 14
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
private void putData2(HTableInterface table, byte[] rowkey) throws IOException {
    Put put = new Put(rowkey);
    put.addColumn(TEST_TABLE_CF.getBytes(), "c1".getBytes(), rowkey);
    put.addColumn(TEST_TABLE_CF2.getBytes(), "c1".getBytes(), rowkey);
    table.put(put);
}
 
Example 15
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
private void putData(HTableInterface table, byte[] rowkey) throws IOException {
    Put put = new Put(rowkey);
    put.addColumn(TEST_TABLE_CF.getBytes(), "c1".getBytes(), rowkey);
    table.put(put);
}
 
Example 16
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
private void putData2(HTableInterface table, byte[] rowkey) throws IOException {
    Put put = new Put(rowkey);
    put.add(TEST_TABLE_CF.getBytes(), "c1".getBytes(), rowkey);
    put.add(TEST_TABLE_CF2.getBytes(), "c1".getBytes(), rowkey);
    table.put(put);
}
 
Example 17
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
private void putData(HTableInterface table, byte[] rowkey) throws IOException {
    Put put = new Put(rowkey);
    put.add(TEST_TABLE_CF.getBytes(), "c1".getBytes(), rowkey);
    table.put(put);
}
 
Example 18
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 19
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
private void putData2(HTableInterface table, byte[] rowkey) throws IOException {
    Put put = new Put(rowkey);
    put.addColumn(TEST_TABLE_CF.getBytes(), "c1".getBytes(), rowkey);
    put.addColumn(TEST_TABLE_CF2.getBytes(), "c1".getBytes(), rowkey);
    table.put(put);
}
 
Example 20
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
void saveRegionCountForTime(HTableInterface stateTable, byte[] timeBytes, int count) throws IOException {
  Put put = new Put(makeTimeRegionCountKey(timeBytes));
  put.add(FAMILY, REGION_TIME_COL, Bytes.toBytes(count));
  stateTable.put(put);
}