Java Code Examples for com.google.api.services.bigquery.model.TableRow#get()

The following examples show how to use com.google.api.services.bigquery.model.TableRow#get() . 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: BigQueryConverters.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code String} using Key/Value style formatting.
 *
 * @param formatTemplate a String with bracketed keys to apply "I am a {key}"
 * @param row is a TableRow object which is used to supply key:values to the template
 *
 * <p> Extracts TableRow fields and applies values to the formatTemplate.
 * ie. formatStringTemplate("I am {key}"{"key": "formatted"}) -> "I am formatted"
 */
public static String formatStringTemplate(String formatTemplate, TableRow row) {
    // Key/Value Map used to replace values in template
    Map<String, String> values = new HashMap<>();

    // Put all column/value pairs into key/value map
    Set<String> rowKeys = row.keySet();
    for (String rowKey : rowKeys) {
      // Only String types can be used in comparison
      if(row.get(rowKey) instanceof String) {
        values.put(rowKey, (String) row.get(rowKey));
      }
    }
    // Substitute any templated values in the template
    String result = StringSubstitutor.replace(formatTemplate, values, "{", "}");
    return result;
}
 
Example 2
Source File: BigQueryConverters.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Return a formatted String Using Key/Value Style formatting
 * from the TableRow applied to the Format Template.
 * ie. formatStringTemplate("I am {key}"{"key": "formatted"}) -> "I am formatted"
 */
public static String formatStringTemplate(String formatTemplate, TableRow row) {
    // Key/Value Map used to replace values in template
    Map<String, String> values = new HashMap<>();

    // Put all column/value pairs into key/value map
    Set<String> rowKeys = row.keySet();
    for (String rowKey : rowKeys) {
      // Only String types can be used in comparison
      if(row.get(rowKey) instanceof String) {
        values.put(rowKey, (String) row.get(rowKey));
      }
    }
    // Substitute any templated values in the template
    String result = StringSubstitutor.replace(formatTemplate, values, "{", "}");
    return result;
}
 
Example 3
Source File: IndexerPipelineUtils.java    From dataflow-opinion-analysis with Apache License 2.0 6 votes vote down vote up
public static String[] extractRedditPostMetaFields(TableRow post) {
	String[] result = new String[METAFIELDS_REDDIT_NUM_FIELDS];
	
	String domain = post.get("domain").toString();
	if (!domain.startsWith("self.")) {
		result[METAFIELDS_REDDIT_EXTERNALLINK_IDX] = post.get("url").toString();
		result[METAFIELDS_REDDIT_DOMAIN_IDX] = domain;
	} else {
		result[METAFIELDS_REDDIT_EXTERNALLINK_IDX] = METAFIELDS_VALUE_UNVAILABLE;
		result[METAFIELDS_REDDIT_DOMAIN_IDX] = METAFIELDS_VALUE_UNVAILABLE;
	}
	Object oSubreddit = post.get("subreddit");
	if (oSubreddit != null)
		result[METAFIELDS_REDDIT_SUBREDDIT_IDX] = oSubreddit.toString();
	else 
		result[METAFIELDS_REDDIT_SUBREDDIT_IDX] = METAFIELDS_VALUE_UNVAILABLE;
		
	result[METAFIELDS_REDDIT_SCORE_IDX] = post.get("score").toString();
	result[METAFIELDS_REDDIT_POSTID_IDX] = extractPostIdFromRedditPost(post);
	
	return result;
}
 
Example 4
Source File: TopWikipediaSessions.java    From beam with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  TableRow row = c.element();
  int timestamp;
  // TODO(BEAM-5390): Avoid this workaround.
  try {
    timestamp = ((BigDecimal) row.get("timestamp")).intValue();
  } catch (ClassCastException e) {
    timestamp = ((Integer) row.get("timestamp")).intValue();
  }
  String userName = (String) row.get("contributor_username");
  if (userName != null) {
    // Sets the implicit timestamp field to be used in windowing.
    c.outputWithTimestamp(userName, new Instant(timestamp * 1000L));
  }
}
 
Example 5
Source File: IndexerPipelineUtils.java    From dataflow-opinion-analysis with Apache License 2.0 5 votes vote down vote up
public static String getTableRowStringFieldIfNotNull(TableRow r, String field) {
	Object value = r.get(field);
	if (value != null)
		return value.toString();
	else
		return null;
}
 
Example 6
Source File: FilterExamples.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  TableRow row = c.element();
  Integer month;
  month = (Integer) row.get("month");
  if (month.equals(this.monthFilter)) {
    c.output(row);
  }
}
 
Example 7
Source File: CombinePerKeyExamples.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  TableRow row = c.element();
  String playName = (String) row.get("corpus");
  String word = (String) row.get("word");
  if (word.length() >= MIN_WORD_LENGTH) {
    c.output(KV.of(word, playName));
  } else {
    // Track how many smaller words we're not including. This information will be
    // visible in the Monitoring UI.
    smallerWords.inc();
  }
}
 
Example 8
Source File: JoinExamples.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  TableRow row = c.element();
  String countryCode = (String) row.get("ActionGeo_CountryCode");
  String sqlDate = (String) row.get("SQLDATE");
  String actor1Name = (String) row.get("Actor1Name");
  String sourceUrl = (String) row.get("SOURCEURL");
  String eventInfo = "Date: " + sqlDate + ", Actor1: " + actor1Name + ", url: " + sourceUrl;
  c.output(KV.of(countryCode, eventInfo));
}
 
Example 9
Source File: JoinExamples.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  TableRow row = c.element();
  String countryCode = (String) row.get("FIPSCC");
  String countryName = (String) row.get("HumanName");
  c.output(KV.of(countryCode, countryName));
}
 
Example 10
Source File: BigQueryTornadoes.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  TableRow row = c.element();
  if ((Boolean) row.get("tornado")) {
    c.output(Integer.parseInt((String) row.get("month")));
  }
}
 
Example 11
Source File: JoinExamples.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(ProcessContext c) {
	TableRow row = c.element();
	String countryCode = (String) row.get("ActionGeo_CountryCode");
	String sqlDate = (String) row.get("SQLDATE");
	String actor1Name = (String) row.get("Actor1Name");
	String sourceUrl = (String) row.get("SOURCEURL");
	String eventInfo = "Date: " + sqlDate + ", Actor1: " + actor1Name + ", url: " + sourceUrl;
	c.output(KV.of(countryCode, eventInfo));
}
 
Example 12
Source File: JoinExamples.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(ProcessContext c) {
	TableRow row = c.element();
	String countryCode = (String) row.get("FIPSCC");
	String countryName = (String) row.get("HumanName");
	c.output(KV.of(countryCode, countryName));
}
 
Example 13
Source File: IndexerPipelineUtils.java    From dataflow-opinion-analysis with Apache License 2.0 4 votes vote down vote up
public static String[] extractRedditCommentMetaFields(TableRow comment) {
	
	String[] result = new String[METAFIELDS_REDDIT_NUM_FIELDS];
	
	result[METAFIELDS_REDDIT_EXTERNALLINK_IDX] = METAFIELDS_VALUE_UNVAILABLE;
	result[METAFIELDS_REDDIT_DOMAIN_IDX] = METAFIELDS_VALUE_UNVAILABLE;
	
	Object oSubreddit = comment.get("subreddit");
	if (oSubreddit != null)
		result[METAFIELDS_REDDIT_SUBREDDIT_IDX] = oSubreddit.toString();
	else
		result[METAFIELDS_REDDIT_SUBREDDIT_IDX] = METAFIELDS_VALUE_UNVAILABLE;
	
	result[METAFIELDS_REDDIT_SCORE_IDX] = comment.get("score").toString();
	result[METAFIELDS_REDDIT_POSTID_IDX] = extractPostIdFromRedditComment(comment);
	
	return result;
	
}
 
Example 14
Source File: IndexerPipelineUtils.java    From dataflow-opinion-analysis with Apache License 2.0 4 votes vote down vote up
public static String extractPostIdFromRedditPost(TableRow post) {
	return "t3_" + (String) post.get("id");
}
 
Example 15
Source File: IndexerPipelineUtils.java    From dataflow-opinion-analysis with Apache License 2.0 4 votes vote down vote up
public static String extractPostIdFromRedditComment(TableRow comment) {
	return (String) comment.get("link_id");
	// link_id in comments is already in the format t3_<postId>
}
 
Example 16
Source File: BigQueryIOStorageReadTableRowIT.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public KV<String, String> apply(TableRow input) {
  CharSequence sampleString = (CharSequence) input.get("sample_string");
  String key = sampleString != null ? sampleString.toString() : "null";
  return KV.of(key, BigQueryHelpers.toJsonString(input));
}